标签:nal center include enter please with amp sea turn
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5636 Accepted Submission(s): 2289
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 200 + 5, maxm = 20000 + 5, inf = 0x3f3f3f3f; struct Edge { int to, cap, flow, next; } edge[maxm << 2]; int tot, head[maxn << 1], que[maxn << 1], dep[maxn << 1], cur[maxn << 1], sta[maxn << 1]; void init() { tot = 2; memset(head, -1, sizeof head); } void addedge(int u, int v, int w, int rw = 0) { edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0; edge[tot].next = head[u]; head[u] = tot ++; edge[tot].to = u; edge[tot].cap = rw; edge[tot].flow = 0; edge[tot].next = head[v]; head[v] = tot ++; } bool bfs(int s, int t, int n) { int front = 0, tail = 0; memset(dep, -1, sizeof dep[0] * (n + 1)); dep[s] = 0; que[tail ++] = s; while(front < tail) { int u = que[front ++]; for(int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; if(edge[i].cap > edge[i].flow && dep[v] == -1) { dep[v] = dep[u] + 1; if(v == t) return true; que[tail ++] = v; } } } return false; } int dinic(int s,int t, int n) { int maxflow = 0; while(bfs(s, t, n)) { for(int i = 0; i <= n; i ++) cur[i] = head[i]; int u = s, tail = 0; while(cur[s] != -1) { if(u == t) { int tp = inf; for(int i = tail - 1; i >= 0; i --) tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow); maxflow += tp; for(int i = tail - 1; i >= 0; i --) { edge[sta[i]].flow += tp; edge[sta[i] ^ 1].flow -= tp; if(edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i; } u = edge[sta[tail] ^ 1].to; } else if(cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) { sta[tail ++] = cur[u]; u = edge[cur[u]].to; } else { while(u != s && cur[u] == -1) u = edge[sta[-- tail] ^ 1].to; cur[u] = edge[cur[u]].next; } } } return maxflow; } int main() { int n, m, s, t, u, v, cost; while(~scanf("%d %d", &n, &m)) { init(); scanf("%d %d", &s, &t); for(int i = 1; i <= n; i ++) { scanf("%d", &cost); addedge(i, n + i, cost); } for(int i = 1; i <= m; i ++) { scanf("%d %d", &u, &v); addedge(u + n, v, inf); addedge(v + n, u, inf); } printf("%d\n", dinic(s, t + n, 2 * n)); } return 0; }
标签:nal center include enter please with amp sea turn
原文地址:https://www.cnblogs.com/bianjunting/p/11403372.html