Drainage Ditches(最大流)
2015-11-04 19:08
316 查看
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
代码2:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 64044 | Accepted: 24718 |
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 代码:醉醉的超时。。。入门题。两种方法: 代码1:
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #define mem(x,y) memset(x,y,sizeof(x)) #include<queue> using namespace std; typedef long long LL; const int INF=0x3f3f3f3f; const int MAXN=210; int map[MAXN][MAXN]; queue<int>dl; int vis[MAXN],pre[MAXN]; int N; bool bfs(int s,int e){ mem(vis,0); mem(pre,0); while(!dl.empty())dl.pop(); vis[s]=1;dl.push(s); int a; while(!dl.empty()){ a=dl.front();dl.pop(); if(a==e)return true; for(int i=1;i<=N;i++){ if(!vis[i]&&map[a][i]){ vis[i]=1; dl.push(i); pre[i]=a; } } } return false; } LL maxflow(int s,int e){ LL flow=0; while(bfs(s,e)){ int r=e; int temp=INF; while(r!=s){ temp=min(temp,map[pre[r]][r]); r=pre[r]; } r=e; while(r!=s){ map[pre[r]][r]-=temp; map[r][pre[r]]+=temp; r=pre[r];//这句话不能少。。 } flow+=temp; } return flow; } int main(){ int M; while(~scanf("%d%d",&M,&N)){ mem(map,0); int u,v,w; while(M--){ scanf("%d%d%d",&u,&v,&w); map[u][v]+=w; } printf("%I64d\n",maxflow(1,N)); } return 0; }
代码2:
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #define mem(x,y) memset(x,y,sizeof(x)) using namespace std; const int INF=0x3f3f3f3f; typedef long long LL; const int MAXN=210; const int MAXM=2020; int head[MAXM]; int vis[MAXN],dis[MAXN]; int edgnum; struct Node{ int from,to,next,cup,flow; }; Node edg[MAXM]; queue<int>dl; void initial(){ mem(head,-1);edgnum=0; } void add(int u,int v,int w){ Node E={u,v,head[u],w,0}; edg[edgnum]=E; head[u]=edgnum++; E={v,u,head[v],0,0}; edg[edgnum]=E; head[v]=edgnum++; } bool bfs(int s,int e){ mem(vis,0);mem(dis,-1); while(!dl.empty())dl.pop(); vis[s]=1;dis[s]=0;dl.push(s); while(!dl.empty()){ int u=dl.front();dl.pop(); for(int i=head[u];i!=-1;i=edg[i].next){ Node v=edg[i]; if(!vis[v.to]&&v.cup>v.flow){//应该是> vis[v.to]=1; dis[v.to]=dis[u]+1; if(v.to==e)return true; dl.push(v.to); } } } return false; } int dfs(int x,int la,int e){ if(x==e||la==0)return la; int temp; LL flow=0; for(int i=head[x];i!=-1;i=edg[i].next){ Node &v=edg[i]; if(dis[v.to]==dis[x]+1&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>0){//这里也应该要> v.flow+=temp; edg[i^1].flow-=temp; la-=temp; flow+=temp; if(la==0)break;//这个要判断 } } return flow; } LL maxflow(int s,int e){ LL flow=0; while(bfs(s,e)){ flow+=dfs(s,INF,e); } return flow; } int main(){ int N,M; while(~scanf("%d%d",&N,&M)){ initial(); int u,v,w; while(N--){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); } printf("%I64d\n",maxflow(1,M)); } return 0; }
相关文章推荐
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
- saiku - JCR在saiku中的运用原理
- git main branch 提交远程仓库失败
- 使用AIDL实现进程间通信
- Daily Scrumming* 2015.11.4(Day 16)
- process launch failed: Security
- 实战阿里云-实战Fail2Ban之v0.9.3
- 使用mail命令发邮件
- maven3+项目管理命令总结 (错误解决:Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2)
- linux系统监控、诊断工具摘录top IO wait lsof
- httpclient提示Cookie rejected: violates RFC 2109: domain must start with a dot
- 人工智能研究
- Daily Scrum (2015/11/3)
- 多线程wait()和sleep()以及InterruptedException异常
- 1014. Waiting in Line (30)
- 1014. Waiting in Line (30)
- failure during conversion to COFF
- adb remount 失败:remount failed: Operation not permitted
- CONTAINING_RECORD宏的详细解释
- Adobe Air 获取启动参数