poj1273 Drainage Ditches(最大流入门 EK+Dinic模板)
2014-09-10 23:15
295 查看
Drainage Ditches
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.
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
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 56697 | Accepted: 21784 |
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.
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/* 第一道网络流题目。事实证明,北大暑期课没有白上,理解了思想去看代码会很容易理解 今天晚上敲了三四遍,最后自己敲出来。 加油!!! Time:2014-9-10 23:15 */ #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #include<climits> using namespace std; const int MAX=205; int N,M; int map[MAX][MAX]; int Edmonds_Karp(int s,int e){ int flow[MAX][MAX],pre[MAX]; int path[MAX]; int u,v,maxflow=0; queue<int >q; memset(flow,0,sizeof(flow)); while(1){ q.push(s); memset(path,0,sizeof(path)); path[s]=INT_MAX; while(!q.empty()){ u=q.front();q.pop(); for(v=1;v<=M;v++){ if(!path[v]&&map[u][v]>flow[u][v]){ q.push(v); path[v]=min(path[u],map[u][v]-flow[u][v]); pre[v]=u; } } } if(path[e]==0)break; for(u=e;u!=s;u=pre[u]){ flow[pre[u]][u]+=path[e]; flow[u][pre[u]]-=path[e]; } maxflow+=path[e]; } return maxflow; } void solve(){ while(scanf("%d%d",&N,&M)!=EOF){ int u,v,w; memset(map,0,sizeof(map)); for(int i=0;i<N;i++){ scanf("%d%d%d",&u,&v,&w); map[u][v]+=w;//注意重边 } int ans=Edmonds_Karp(1,M); printf("%d\n",ans); } } int main(){ //freopen("poj1273.txt","r",stdin); solve(); return 0; }/* 最大流 dinic 模板 加油!!! Time:2014-9-14 15:08 */ #include<cstdio> #include<cstring> #include<climits> #include<algorithm> #include<queue> using namespace std; #define INF INT_MAX const int MAX=205; int N,M; int map[MAX][MAX]; int d[MAX]; bool BFS(){ queue<int >q; memset(d,-1,sizeof(d)); d[1]=0;//将起点为 0 q.push(1);//将起点入队 while(!q.empty()){ int u=q.front();q.pop(); for(int v=1;v<=N;v++){ if(map[u][v]&&d[v]==-1){ d[v]=d[u]+1; q.push(v); } } } return d !=-1; } int DFS(int u,int cur_flow){ if(u==N) return cur_flow; int temp=cur_flow; for(int v=1;v<=N;v++){ if(map[u][v]>0&&d[u]+1==d[v]){ int flow=DFS(v,min(temp,map[u][v]));//可以通过的最大流,即所有通道里边的最小流 map[u][v]-=flow; map[v][u]+=flow; temp-=flow; } } return cur_flow-temp; } int Dinic(){ int max_flow=0; while(BFS()){ int temp; while(temp=DFS(1,INF)){//起点为1 max_flow+=temp; } } return max_flow; } void solve(){ while(scanf("%d%d",&M,&N)!=EOF){ memset(map,0,sizeof(map)); int u,v,w; for(int i=0;i<M;i++){ scanf("%d%d%d",&u,&v,&w); map[u][v]+=w;//去重 } printf("%d\n",Dinic()); } } int main(){ solve(); return 0; }
相关文章推荐
- poj 1273 Drainage Ditches【最大流入门】
- poj 1273 EK最大流入门题
- 最大流入门 之 poj 1273
- POJ 1273 最大流入门题 Edmonds_Karp算法
- poj 1273 Drainage Ditches 最大流入门题
- POJ-1273 Drainage Ditches(最大流入门)
- POJ 1273 || HDU 1532 Drainage Ditches ,最大流入门题
- NYOJ 323 && HDU 1532 && POJ 1273 Drainage Ditches (网络流之最大流入门)
- poj 1273 Drainage Ditches(网络流入门之最大流)
- 网络流 最大流问题入门选作 (POJ1273,BZOJ1711)
- 最大流入门 poj 1273 && hdu 3549
- POJ 1273 Drainage Ditches 最大流入门题
- poj1273 Drainage Ditches(最大流入门)
- Poj 1273 Drainage Ditches【最大流入门题----Ford-Fulkerson】
- poj 1273 Drainage Ditches(最大流入门)
- POJ 1273 Drainage Ditches(最大流入门)
- POJ 1273 Drainage Ditches(最大流入门)
- 最大流入门POJ 1273 Edmonds-Karp
- POJ 1273 Drainage Ditches 最大流入门题
- poj 1273 Drainage Ditches 【最大流入门 dinic算法】