POJ_3498
对于任何一点来讲,限制的是跳走的企鹅的数量,如果这个点不是终点,那么必然跳过来的企鹅都会跳走,因此实际上限制的是经过这个点的企鹅的数量,这样通过拆点来限制经过点的企鹅的数量就可以了。最后暴力一点,枚举每个点作为终点并判断一下就可以了。
#include#include<string.h> #include #define MAXD 210 #define MAXM 40410 #define INF 0x3f3f3f3f int N, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM]; int S, T, d[MAXD], q[MAXD], work[MAXD], list[MAXD], L, TOT; double D; struct Stone {double x, y;int n, m; }stone[MAXD]; double sqr(double x) {return x * x; } void init() {int i, j;TOT = 0;scanf("%d%lf", &N, &D);for(i = 0; i < N; i ++)scanf("%lf%lf%d%d", &stone[i].x, &stone[i].y, &stone[i].n, &stone[i].m), TOT += stone[i].n; } void add(int x, int y, int z) {v[e] = y, flow[e] = z;next[e] = first[x], first[x] = e ++; } void build(int t) {int i, j;S = 2 * N, T = t;memset(first, -1, sizeof(first[0]) * (N << 1 | 1)), e = 0;for(i = 0; i < N; i ++)add(i, i + N, stone[i].m), add(i + N, i, 0);for(i = 0; i < N; i ++)for(j = i + 1; j < N; j ++)if(sqr(D) >= sqr(stone[i].x - stone[j].x) + sqr(stone[i].y - stone[j].y))add(i + N, j, INF), add(j, i + N, 0), add(j + N, i, INF), add(i, j + N, 0);for(i = 0; i < N; i ++)if(stone[i].n)add(S, i, stone[i].n), add(i, S, 0); } int bfs() {int i, j, rear = 0;memset(d, -1, sizeof(d[0]) * (N << 1 | 1));d[S] = 0, q[rear ++] = S;for(i = 0; i < rear; i ++)for(j = first[q[i]]; j != -1; j = next[j])if(flow[j] && d[v[j]] == -1){d[v[j]] = d[q[i]] + 1, q[rear ++] = v[j];if(v[j] == T) return 1;}return 0; } int dfs(int cur, int a) {if(cur == T) return a;for(int &i = work[cur]; i != -1; i = next[i])if(flow[i] && d[v[i]] == d[cur] + 1)if(int t = dfs(v[i], std::min(a, flow[i]))){flow[i] -= t, flow[i ^ 1] += t;return t;}return 0; } int dinic() {int ans = 0, t;while(bfs()){memcpy(work, first, sizeof(first[0]) * (N << 1 | 1));while(t = dfs(S, INF))ans += t;}return ans; } void solve() {int i;L = 0;for(i = 0; i < N; i ++){build(i);if(dinic() == TOT)list[L ++] = i;}if(L == 0)printf("-1 ");else{printf("%d", list[0]);for(i = 1; i < L; i ++) printf(" %d", list[i]);printf(" ");} } int main() {int t;scanf("%d", &t);while(t --){init();solve();}return 0; }