【問題描述】
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.?
【輸入】
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.
【輸出】
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
【算法分析】
裸的最大流,采用最短增廣路算法的Dinic算法可以通過所有測試數據。
【程序代碼】
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define maxn 10000 5 #define inf 1000000 6 using namespace std; 7 int n,m,x,y,z; 8 struct graph{ 9 int start,end,pointsize,tot; 10 int c[maxn<<1],to[maxn<<1],next[maxn<<1],head[maxn],dis[maxn],nowhead[maxn]; 11 void clear(){ 12 tot=1; 13 memset(c,0,sizeof(c)); 14 memset(to,0,sizeof(to)); 15 memset(next,0,sizeof(next)); 16 memset(head,0,sizeof(head)); 17 memset(dis,0,sizeof(dis)); 18 memset(nowhead,0,sizeof(nowhead)); 19 } 20 void addedge(int a,int b,int l){ 21 c[++tot]=l;to[tot]=b;next[tot]=head[a];head[a]=tot; 22 c[++tot]=0;to[tot]=a;next[tot]=head[b];head[b]=tot; 23 } 24 int q[maxn],ql,qr; 25 bool BFS(){ 26 for (int i=1;i<=pointsize;++i) nowhead[i]=head[i],dis[i]=0; 27 ql=1; qr=0; q[++qr]=end; 28 while (ql<=qr){ 29 for (int k=q[ql++],p=head[k];p;p=next[p]) 30 if(c[p^1]&&!dis[to[p]]&&to[p]!=end) dis[q[++qr]=to[p]]=dis[k]+1; 31 } 32 return dis[start]; 33 } 34 int DFS(int k,int maxflow){ 35 if (k==end) return maxflow; 36 int flow=0,tflow; 37 for (int&p=nowhead[k];p&&maxflow;p=next[p]) 38 if(c[p]&&dis[to[p]]+1==dis[k]&&(tflow=DFS(to[p],min(maxflow,c[p])))) 39 c[p]-=tflow,c[p^1]+=tflow,maxflow-=tflow,flow+=tflow; 40 return flow; 41 } 42 int dinic(int a,int b){ 43 int flow=0; 44 start=a; end=b; 45 while (BFS()) flow+=DFS(a,inf); 46 return flow; 47 } 48 graph(){ 49 tot=1; 50 } 51 } G; 52 int main(){ 53 while (scanf("%d%d",&n,&m)!=EOF){ 54 G.clear(); 55 G.pointsize=m; 56 for (int i=1;i<=n;++i){ 57 scanf("%d%d%d",&x,&y,&z); 58 G.addedge(x,y,z); 59 } 60 printf("%d\n",G.dinic(1,m)); 61 } 62 return 0; 63 }
用C++中vector和queue實現(參考《算法藝術入門經典訓練指南》):?
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<vector> 5 #include<queue> 6 using namespace std; 7 const int maxn=10010; 8 const int INF=1e8; 9 struct Edge{ 10 int from,to,cap,flow; 11 }; 12 struct Dinic{ 13 int n,m,s,t; 14 vector<Edge> edges; 15 vector<int> G[maxn]; 16 void AddEdge(int from,int to,int cap){ 17 edges.push_back((Edge){from,to,cap,0}); 18 edges.push_back((Edge){to,from,0,0}); 19 m=edges.size(); 20 G[from].push_back(m-2); 21 G[to].push_back(m-1); 22 } 23 bool vis[maxn]; 24 int d[maxn],cur[maxn]; 25 bool BFS(){ 26 memset(vis,0,sizeof(vis)); 27 queue<int> Q; 28 Q.push(s); d[s]=0; vis[s]=1; 29 while (!Q.empty()){ 30 int x=Q.front(); Q.pop(); 31 for (int i=0;i<G[x].size();i++){ 32 Edge &e=edges[G[x][i]]; 33 if (!vis[e.to]&&e.cap>e.flow){ 34 vis[e.to]=1; 35 d[e.to]=d[x]+1; 36 Q.push(e.to); 37 } 38 } 39 } 40 return vis[t]; 41 } 42 int DFS(int x,int a){ 43 if (x==t||a==0) return a; 44 int flow=0,f; 45 for (int &i=cur[x];i<G[x].size();i++){ 46 Edge &e=edges[G[x][i]]; 47 if (d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){ 48 e.flow+=f; 49 edges[G[x][i]^1].flow-=f; 50 flow+=f; a-=f; 51 if (a==0) break; 52 } 53 } 54 return flow; 55 } 56 int Maxflow(int ss,int tt){ 57 int flow=0; 58 s=ss; t=tt; 59 while (BFS()){ 60 memset(cur,0,sizeof(cur)); 61 flow+=DFS(s,INF); 62 } 63 return flow; 64 } 65 }Graph; 66 int main(){ 67 int nn,mm,from,to,cap; 68 while (scanf("%d%d",&nn,&mm)!=EOF){ 69 Graph.edges.clear(); 70 memset(Graph.G,0,sizeof(Graph.G)); 71 for (int i=1;i<=nn;++i){ 72 scanf("%d%d%d",&from,&to,&cap); 73 Graph.AddEdge(from,to,cap); 74 } 75 printf("%d\n",Graph.Maxflow(1,mm)); 76 } 77 return 0; 78 }
?
聲明:本博文為博主原創博文,未經允許請勿轉載。