hdu1532

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8043    Accepted Submission(s): 3756

Problem 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 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
 
Sample Output
50

程序:

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define M 100005
#define inf 999999999
#pragma comment(linker, "/STACK:1024000000,1024000000")//可以有效的防止爆栈
int min(int a,int b)
{
return a<b?a:b;
}
struct st
{
int u,v,next,w;
}edge[M*2];
int head[M],t,dis[M],q[M],work[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)//有向边
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
edge[t].u=v;
edge[t].v=u;
edge[t].w=0;
edge[t].next=head[v];
head[v]=t++;
}
/*void add(int u,int v,int w)//无向边
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
edge[t].u=v;
edge[t].v=u;
edge[t].w=w;
edge[t].next=head[v];
head[v]=t++;
}*/
int bfs(int S,int T)
{
int rear=0;
memset(dis,-1,sizeof(dis));
dis[S]=0;
q[rear++]=S;
for(int i=0;i<rear;i++)
{
for(int j=head[q[i]];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w&&dis[v]==-1)
{
dis[v]=dis[q[i]]+1;
q[rear++]=v;
if(v==T)
return 1;
}
}
}
return 0;
}
int dfs(int cur,int a,int T)
{
if(cur==T)
return a;
for(int &i=work[cur];i!=-1;i=edge[i].next)//一定要这样写否则会超时
{
int v=edge[i].v;
if(edge[i].w&&dis[v]==dis[cur]+1)
{
int tt=dfs(v,min(a,edge[i].w),T);
if(tt)
{
edge[i].w-=tt;
edge[i^1].w+=tt;
return tt;
}
} }
return 0;
}
int Dinic(int S,int T)
{
int ans=0;
while(bfs(S,T))
{
memcpy(work,head,sizeof(head));
while(int tt=dfs(S,inf,T))
ans+=tt;
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&m,&n)!=-1)
{
init();
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
//add(b,a,c);
}
int ans=Dinic(1,n);
printf("%d\n",ans);
}
}

最新文章

  1. 反应器(Reactor)和主动器(Proactor)
  2. 软件工程(C编码实践篇)学习心得
  3. 【08-23】redis学习笔记
  4. await之后的线程问题
  5. HTML 学习笔记 CSS3(过度 transition)
  6. html页面 代码 编写的 一些 基本素养 约定 知识点
  7. 在ubuntu14.04 64位中使用jd-gui
  8. JAVA $ JSP
  9. Linux忘记rootpassword
  10. 为什么Intent传递对象的时候必须要将对象序列化呢?
  11. myeclipse一些快捷键 错了或者没说到补充下
  12. CTF---隐写术入门第二题 小苹果
  13. 利用zabbix api添加、删除、禁用主机
  14. Shell脚本之grep
  15. mybatis-generator 代码自动生成工具包
  16. Java字符串与数组
  17. ibatis.net 循环
  18. Jenkins二 安装gitlab及其使用
  19. cpu选型
  20. 涂抹mysql笔记-数据备份和恢复

热门文章

  1. smo算法
  2. Android XmlPullParser 笔记
  3. if、for、while、do 等语句自占一行
  4. Xenocode Postbuild 2010 for .NET 混淆工具的详细使用步骤【转】
  5. shiro 解决 跨域(仅端口不同) 登陆 问题
  6. 【R markdown】rmysql乱码问题
  7. VS2010属性表的建立与灵活运用
  8. linux数据盘分区以及格式化
  9. [spring] org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljav 解决
  10. mybatis由浅入深day01_ 7输入映射(7.1传递pojo的包装对象_7.2#{}与${}_7.3传递简单类型_7.4传递pojo对象_7.5传递hashmap)