SGU_438
之所以会有时间的差别,一方面的原因在于过的石头的数量,另一方面的原因就在于石头的承重能力有限。为了体现石头的承重能力,可以将第i个石头拆成i和i'两个点,然后连一条i->i'的边,容量为第i个石头的承重,这样就可以体现出一秒内站在第i个石头上的人不会超过石头的承重。为了计算时间,可以将每个石头再按时间的顺序拆成若干个点建图,然后枚举时间的增长,每次将图补全一层,直到最大流大于或等于M为止。
#include<stdio.h> #include<string.h> #include<algorithm> #define MAXN 60 #define MAXD 10010 #define MAXM 2060010 #define INF 0x3f3f3f3f int N, M, D, W, ANS, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM]; int S, T, d[MAXD], q[MAXM], work[MAXD]; struct stone { int x, y, c; }s[MAXN]; void init() { int i; for(i = 1; i <= N; i ++) scanf("%d%d%d", &s[i].x, &s[i].y, &s[i].c); } void add(int x, int y, int z) { v[e] = y, flow[e] = z; next[e] = first[x], first[x] = e ++; } int sqr(int x) { return x * x; } int Near(int x1, int y1, int x2, int y2) { return sqr(x2 - x1) + sqr(y2 - y1) <= D * D; } void build(int cur) { int i, j, k, x, y; for(i = 1; i <= N; i ++) { x = 2 * cur * N + i, y = x + N; add(x, y, s[i].c), add(y, x, 0); if(s[i].y <= D) add(S, x, M), add(x, S, 0); if(W - s[i].y <= D) add(y, T, M), add(T, y, 0); } if(cur) { for(i = 1; i <= N; i ++) { x = 2 * cur * N + i; for(j = 1; j <= N; j ++) if(Near(s[j].x, s[j].y, s[i].x, s[i].y)) { y = (2 * cur - 1) * N + j; add(y, x, M), add(x, y, 0); } } } } int bfs() { int i, j, rear = 0; memset(d, -1, sizeof(d[0]) * (T + 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; int t; for(int &i = work[cur]; i != -1; i = next[i]) if(flow[i] && d[v[i]] == d[cur] + 1) if(t = dfs(v[i], std::min(a, flow[i]))) { flow[i] -= t, flow[i ^ 1] += t; return t; } return 0; } void dinic() { int t; while(bfs()) { memcpy(work, first, sizeof(first[0]) * (T + 1)); while(t = dfs(S, INF)) ANS += t; } } void solve() { int i, j, k; if(D >= W) { printf("1\n"); return ; } if(N == 0) { printf("IMPOSSIBLE\n"); return ; } S = 0, T = 2 * (N + M) * N + 1; memset(first, -1, sizeof(first[0]) * (T + 1)); e = ANS = 0; for(i = 0; i < N + M; i ++) { build(i); dinic(); if(ANS >= M) break; } if(i == N + M) printf("IMPOSSIBLE\n"); else printf("%d\n", i + 2); } int main() { while(scanf("%d%d%d%d", &N, &M, &D, &W) == 4) { init(); solve(); } return 0; }