POJ 1273 Drainage Ditches 网络流 ISAP

来源:互联网 发布:畅捷通软件介绍 编辑:程序博客网 时间:2024/06/05 14:36

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

5 41 2 401 4 202 4 202 3 303 4 10

Sample Output

50

Source


传送门

题目大意是,,很多水管,然后巴拉巴拉。。。

总之求最大流裸题!($

不得不说很不喜欢poj,hdu这种,,,虽然有些题目只能在它们上面做。

首先poj不能用bits/stdc++.h这个头文件。。。每题CE似乎必然。

然后poj的题目都是多组测试数据。。。靠,有什么意义吗??


事实上的话,,我这题真的脑餐好多地方。。。

改了很久!后来发现,,反向边的容量应该是0,,

还有就是更新最大流的时候手贱了,+=写成==。。笔记本的问题。。


网络流上,ISAP的时间复杂度最多O(V^2*E)。。实际上远不到这个把。。

由于这题范围小,边不多,其实是可以用EK水过去的。。

对于没有练过ISAP的我来说,,还是写一发把。。

自己看的模板是这个blog:

http://blog.csdn.net/u013368721/article/details/30490255

哦提一句,学会了一个新姿势。。

!x,x=0返回1.

~x,x>=0返回1.


//#include<bits/stdc++.h>#include<cstdio>#include<cstring> using namespace std;int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}    while (ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}const int N=205;int n,m,Ecnt;int pre[N],cur[N],num[N],Q[N],d[N];struct Edge{int next,to,C;}E[N<<1]; int head[N];void add(int x,int y,int z){E[Ecnt].next=head[x];E[Ecnt].to=y;E[Ecnt].C=z;head[x]=Ecnt++;}void BFS(int source,int sink){memset(num,0,sizeof(num));memset(d,255,sizeof(d));d[sink]=0;num[0]=1;int Head=0,tail=0;Q[tail++]=sink;while (Head!=tail){int u=Q[Head++];for (int i=head[u];~i;i=E[i].next){int j=E[i].to;if (~d[j]) continue;d[j]=d[u]+1;num[d[j]]++;Q[tail++]=j;}}}int Isap(int source,int sink){memcpy(cur,head,sizeof(cur));BFS(source,sink);int flow=0,u;u=pre[source]=source;while (d[sink]<n+1){if (u==sink){int f=999999,neck;for (int i=source;i!=sink;i=E[cur[i]].to)if (f>E[cur[i]].C){f=E[cur[i]].C;neck=i;}for (int i=source;i!=sink;i=E[cur[i]].to)E[cur[i]].C-=f,E[cur[i]^1].C+=f;flow+=f;u=neck; }int j;for (j=cur[u];~j;j=E[j].next)if (d[E[j].to]+1==d[u] && E[j].C)break;if (~j){cur[u]=j;pre[E[j].to]=u;u=E[j].to;}else{num[d[u]]--;if (!num[d[u]]) break;int mind=n+1;for (int i=head[u];~i;i=E[i].next)if (E[i].C && mind>d[E[i].to]){cur[u]=i;mind=d[E[i].to];}d[u]=mind+1;num[d[u]]++;u=pre[u];}}return flow;}void solve(){memset(head,255,sizeof(head));int x,y,z;Ecnt=0;for (int i=0;i<m;i++){x=read(),y=read(),z=read();add(x,y,z);add(y,x,0); //opposite side}printf("%d\n",Isap(1,n));}int main(){while (~scanf("%d%d",&m,&n))solve();return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 保研联系导师后怎么办 特别害怕和导师交流怎么办 面试工资要少了怎么办 一面工资要低了怎么办 家乐卡到期本金还一半怎么办 新三板公司没有资不抵债没钱怎么办 户户通智能卡坏了怎么办 秦岭云无法回看怎么办 身份证在火车站丢了怎么办 到火车站发现身份证丢了怎么办 广电宽带太慢了怎么办 车有后雷达想装前置雷达怎么办 现代朗动油耗大怎么办 雷达线雕头里有水怎么办 上古卷轴5免疫死亡奴役怎么办 dw手表时针不动了怎么办 雷达陶瓷表壳摔坏了怎么办 雷达表盘摔坏了怎么办 雷达测速60超了怎么办 卡西欧手表电池没电了怎么办 审稿人让引用他的文章怎么办 考二建未从事该行业满两年怎么办 2档换3档离合器怎么办 高铁车票丢了怎么办 事业编制调动原单位不同意怎么办 想去铁路上工作怎么办 房产权50年以后怎么办 在香港手机没电怎么办 学校官网登陆忘记密码怎么办 网上申报学校忘记密码怎么办 专转本想换专业怎么办 发生工伤没有平均公资怎么办? 单招过了不想去怎么办 22岁了还想复读怎么办 父母不让我读大专了怎么办 专升本考试失利怎么办 高考复读一年后失败了怎么办? 医保住院超过30万怎么办 北京医保超过2万怎么办 工伤公司垫付医疗费没法报销怎么办 司法考试照片耳朵露不出来怎么办