链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4280
源点是West, 汇点是East, 用Dinic带入求就好了
代码:要用c++提交
#pragma comment(linker, "/STACK:102400000,102400000") ///手动开大栈区 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<stack> using namespace std; #define N 100005 #define INF 0x3fffffff struct node {int v, flow, next;} a[N<<1]; int Layer[N], Head[N], cnt, n, m; void Init() { cnt = 0; memset(Head, -1, sizeof(Head)); } void Add(int u, int v, int flow) { a[cnt].v = v; a[cnt].flow = flow; a[cnt].next = Head[u]; Head[u] = cnt++; } bool BFS(int Start, int End) { queue<int>Q; Q.push(Start); memset(Layer, 0, sizeof(Layer)); Layer[Start] = 1; while(Q.size()) { int u = Q.front(); Q.pop(); if(u==End) return true; for(int i=Head[u]; i!=-1; i=a[i].next) { int v = a[i].v; if(!Layer[v] && a[i].flow) { Layer[v] = Layer[u] + 1; Q.push(v); } } } return false; } int DFS(int u, int MaxFlow, int End) { if(u==End) return MaxFlow; int uflow=0; for(int i=Head[u]; i!=-1; i=a[i].next) { int v = a[i].v; if(Layer[v]==Layer[u]+1 && a[i].flow) { int flow = min(a[i].flow, MaxFlow-uflow); flow = DFS(v, flow, End); a[i].flow -= flow; a[i^1].flow += flow; uflow += flow; if(uflow==MaxFlow) break; } } if(uflow==0) Layer[u] = 0; return uflow; } int Dinic(int Start, int End) { int MaxFlow=0; while(BFS(Start, End)==true) MaxFlow += DFS(Start, INF, End); return MaxFlow; } int main() { int t; scanf("%d", &t); while(t--) { int i, u, v, flow, East=-INF, West=INF, Start, End, x, y; scanf("%d%d", &n, &m); Init(); for(i=1; i<=n; i++) { scanf("%d%d", &x, &y); if(West > x) { West = x; Start = i; } if(East < x) { East = x; End = i; } } for(i=1; i<=m; i++) { scanf("%d%d%d", &u, &v, &flow); Add(u, v, flow); Add(v, u, flow); } printf("%d\n", Dinic(Start, End)); } return 0; }
勿忘初心