Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 66508 | Accepted: 25687 |
Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
//毕竟"我神"的模板 #include <queue> #include <vector> #include <cstdio> #include <cstring> #define Type int const int MAXNODE=210; const int MAXEDGE=100010; using namespace std; const Type INF=0x3f3f3f3f; struct Edge { int u, v, next; Type cap, flow; Edge() {} Edge(int u, int v, Type cap, Type flow, int next): u(u), v(v), cap(cap), flow(flow), next(next){} }; struct EK { int n, m, s, t; Edge edges[MAXEDGE]; int head[MAXNODE], pre[MAXNODE]; bool vis[MAXNODE]; vector<int> cut; void init(int n, int c) { this->n=n; //this->c=c; memset(head, -1, sizeof(head)); m=0; for(int i=0; i<c; i++) { int a, b, d; scanf("%d%d%d", &a, &b, &d); AddEdge(a, b, d); } } void AddEdge(int u, int v, Type cap) { edges[m]=Edge(u, v, cap, 0, head[u]); head[u]=m++; edges[m]=Edge(v, u, 0, 0, head[v]); head[v]=m++; } bool bfs() { queue<int> Q; memset(vis, 0, sizeof(vis)); vis[s]=1; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u]; i!=-1; i=edges[i].next) { Edge &e=edges[i]; if(!vis[e.v] && e.cap>e.flow) { vis[e.v]=true; pre[e.v]=i; if(e.v==t) return true; Q.push(e.v); } } } return false; } Type Augment() { int u=t; Type flow=INF; while(u!=s) { Edge &e=edges[pre[u]]; flow=min(flow, e.cap-e.flow); u=e.u; } u=t; while(u!=s) { edges[pre[u]].flow+=flow; edges[pre[u]^1].flow-=flow; u=edges[pre[u]].u; } return flow; } Type Maxflow(int s, int t) { this->s=s; this->t=t; Type flow=0; while(bfs()) flow+=Augment(); return flow; } void Mincut() { cut.clear(); for(int i=0; i<m; i+=2) { if(vis[edges[i].u] &&!vis[edges[i].v]&&edges[i].cap) cut.push_back(i); } } }ek; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { ek.init(m, n); printf("%d ", ek.Maxflow(1, m)); } return 0; }
isap(最短增光路):
#include <queue> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAXNODE = 210; const int MAXEDGE = 2100; typedef int Type; const Type INF=0x3f3f3f3f; struct Edge { int u, v, next; Type cap, flow; Edge() {} Edge(int u, int v, Type cap, Type flow, int next): u(u), v(v), cap(cap), flow(flow), next(next) {} }; struct ISAP { int n, m, s, t; Edge edges[MAXEDGE]; int head[MAXNODE], p[MAXNODE], num[MAXNODE], cur[MAXNODE], d[MAXNODE]; bool vis[MAXNODE]; void init(int n, int c) { this->n=n; memset(head, -1, sizeof(head)); m=0; for(int i=0; i<c; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); AddEdge(a, b, c); } } void AddEdge(int u, int v, Type cap) { edges[m]=Edge(u, v, cap, 0, head[u]); head[u]=m++; edges[m]=Edge(v, u, 0, 0, head[v]); head[v]=m++; } bool Bfs() { memset(vis, 0, sizeof(vis)); queue<int> Q; for(int i=1; i<=n; i++) d[i]=INF; d[t]=0; vis[t]=1; Q.push(t); while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u]; i!=-1; i=edges[i].next){ Edge &e=edges[i ^ 1]; if(!vis[e.u] && e.cap>e.flow) { vis[e.u]=true; d[e.u]=d[u]+1; Q.push(e.u); } } } return vis[s]; } Type Augment() { int u=t; Type flow=INF; while(u!=s){ Edge &e=edges[p[u]]; flow=min(flow, e.cap-e.flow); u=edges[p[u]].u; } u=t; while(u!=s) { edges[p[u]].flow+=flow; edges[p[u] ^ 1].flow-=flow; u=edges[p[u]].u; } return flow; } Type Maxflow(int s, int t) { this->s=s; this->t=t; Type flow=0; Bfs(); //如果s-->t走不通 ; if(d[s]>n) return 0; memset(num, 0, sizeof(num)); for(int i=1; i<=n; i++) cur[i]=head[i]; for(int i=1; i<=n; i++) if(d[i]<INF) num[d[i]]++; int u=s; while(d[s]<=n) { if(u==t){ flow+=Augment(); u=s; } bool ok=false; for(int i=cur[u]; i!=-1; i=edges[i].next) { Edge &e=edges[i]; if(e.cap>e.flow&&d[u]==d[e.v]+1){ ok=true; p[e.v]=i; //点v由第i条边增光得到; cur[u]=i; //尝试得到第i条边; u=e.v; break; } } //如果没找到下一个点, 表示u-->t的最短路要变长了, 或者没路可走了。 if(!ok){ //找寻u到下一个点的最短路; int Min=n-1; for(int i=head[u]; i!=-1; i=edges[i].next){ Edge &e=edges[i]; if(e.cap>e.flow) Min=min(Min, d[e.v]); } if(--num[d[u]]==0) break; num[d[u]=Min+1]++; cur[u]=head[u]; //返回前一个点 ; if(u!=s) u=edges[p[u]].u; } } return flow; } }isap; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { isap.init(m, n); int Result=isap.Maxflow(1, m); printf("%d ", Result); } return 0; }
Dinic
#include <queue> #include <cstdio> #include <vector> #include <cstring> using namespace std; typedef int Type; const int MAXNODE = 210; const int MAXEDGE = 10010; const Type INF = 0x3f3f3f3f; struct Edge { int u, v, next; Type cap, flow; Edge() {} Edge(int u, int v, Type cap, Type flow, int next): u(u), v(v), cap(cap), flow(flow), next(next) {} }; struct Dinic { int n, m, s, t; Edge edges[MAXEDGE]; int head[MAXNODE]; int cur[MAXNODE], d[MAXNODE]; bool vis[MAXNODE]; vector<int> cut; void init(int n, int h) { this->n=n; memset(head, -1, sizeof(head)); m=0; for(int i=0; i<h; i++) { int u, v, cap; scanf("%d%d%d", &u, &v, &cap); addEdge(u, v, cap); } } void addEdge(int u, int v, Type cap) { edges[m]=Edge(u, v, cap, 0, head[u]); head[u]=m++; edges[m]=Edge(v, u, 0, 0, head[v]); head[v]=m++; } //bfs构建层次图; bool bfs() { memset(vis, 0, sizeof(vis)); queue<int> Q; Q.push(s); d[s]=0; vis[s]=1; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u]; i!=-1; i=edges[i].next) { Edge &e=edges[i]; if(!vis[e.v] && e.cap > e.flow) { vis[e.v]=true; d[e.v]=d[u]+1; Q.push(e.v); } } } return vis[t]; } Type dfs(int u, Type a) { if(u==t || a==0) return a; Type flow=0, f; for(int i=cur[u]; ~i; i=edges[i].next) { Edge &e=edges[i]; if(d[u]+1==d[e.v] && (f=dfs(e.v, min(a, e.cap-e.flow))) > 0) { e.flow += f; edges[i ^ 1].flow -= f; flow += f; a -= f; if(a==0) break; } } return flow; } Type maxFlow(int s, int t) { this->s=s; this->t=t; Type flow=0; while(bfs()) { //cur[i]; 表示第i个点已经增广到哪一条边了, 相当于一个剪枝; for(int i=0; i<n; i++) cur[i]=head[i]; flow += dfs(s, INF); } return flow; } }dinic; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { dinic.init(m, n); int result=dinic.maxFlow(1, m); printf("%d " , result); } return 0; }