URAL_1277
拆点之后做最大流即可,但要注意S和F在同一个位置的情况。
#include#include #include #define MAXD 210#define MAXM 40410#define INF 0x3f3f3f3fint a[MAXD], N, M, S, F, K, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM];int d[MAXD], q[MAXD], work[MAXD];void add(int x, int y, int z){ v[e] = y, flow[e] = z; next[e] = first[x], first[x] = e ++;}void init(){ int i, x, y; scanf("%d%d%d%d", &N, &M, &S, &F); for(i = 1; i <= N; i ++) scanf("%d", &a[i]); memset(first + 1, -1, sizeof(first[0]) * N << 1); e = 0; for(i = 0; i < M; i ++) { scanf("%d%d", &x, &y); add(x + N, y, INF), add(y, x + N, 0); add(y + N, x, INF), add(x, y + N, 0); } for(i = 1; i <= N; i ++) add(i, i + N, a[i]), add(i + N, i, 0);}int bfs(){ int i, j, rear = 0; memset(d + 1, -1, sizeof(d[0]) * N << 1); d[S + N] = 0, q[rear ++] = S + N; 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] == F) return 1; } //printf(">>> %d %d %d %d\n", S, first[S], flow[7], d[7]); return 0;}int dfs(int cur, int a){ if(cur == F) 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 + 1, first + 1, sizeof(first[0]) * N << 1); while(t = dfs(S + N, INF)) ans += t; } return ans;}void solve(){ printf("%s\n", dinic() <= K ? "YES" : "NO");}int main(){ while(scanf("%d", &K) == 1) { init(); if(S == F) printf("NO\n"); else solve(); } return 0;}