博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu------(3549)Flow Problem(最大流(水体))
阅读量:7062 次
发布时间:2019-06-28

本文共 2040 字,大约阅读时间需要 6 分钟。

Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 8203    Accepted Submission(s): 3817

Problem Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
 

 

Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
 

 

Output
For each test cases, you should output the maximum flow from source 1 to sink N.
 

 

Sample Input
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
 

 

Sample Output
Case 1: 1 Case 2: 2
 

 

Author
HyperHexagon
 

 

Source
 
代码:
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 const int inf=0x3f3f3f3f; 7 int mat[30][30]; 8 int dist[31]; 9 int n,m;10 int min(int a,int b)11 {12 return a
q;17 q.push(st);18 dist[st]=0;19 int t;20 while(!q.empty()){21 t=q.front();22 q.pop();23 for(int i=1;i<=n;i++){24 if(dist[i]<0&&mat[t][i]>0){25 dist[i]=dist[t]+1;26 if(i==to)return 1;27 q.push(i);28 }29 }30 }31 return 0;32 }33 int dfs(int st,int to,int flow)34 {35 int tem;36 if(st==to||flow==0) return flow;37 for(int i=1;i<=n;i++){38 if((dist[i]==dist[st]+1)&&mat[st][i]>0&&(tem=dfs(i,to,min(mat[st][i],flow))))39 {40 mat[st][i]-=tem;41 mat[i][st]+=tem;42 return tem;43 }44 }45 return 0;46 }47 int Dinic(int st,int en)48 {49 int ans=0;50 while(bfs(st,en))51 ans+=dfs(st,en,inf);52 return ans;53 }54 int main(){55 int cas,i,a,b,c;56 scanf("%d",&cas);57 for(i=1;i<=cas;i++){58 scanf("%d%d",&n,&m);59 memset(mat,0,sizeof(mat));60 while(m--){61 scanf("%d%d%d",&a,&b,&c);62 mat[a][b]+=c;63 }64 printf("Case %d: %d\n",i,Dinic(1,n));65 }66 return 0;67 }
View Code

 

转载地址:http://mynll.baihongyu.com/

你可能感兴趣的文章
Linux中如何让进程(或正在运行的程序)到后台运行?[zz]
查看>>
ZendGuardLoader安装
查看>>
floyd算法&迪杰斯特拉算法
查看>>
[CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
查看>>
加入强调语气,使用<strong>和<em>标签
查看>>
How Spring Boot Autoconfiguration Magic Works--转
查看>>
Android 最简单的SD卡文件遍历程序
查看>>
[转] 32位 PL/SQL Develope r如何连接64位的Oracle 图解
查看>>
ArcGIS Engine开发之旅03--ArcGIS Engine中的控件
查看>>
sparkR 跑通的函数
查看>>
jQ效果:jQuery之插件开发短信发送倒计时功能
查看>>
aar
查看>>
(第9篇)大数据的的超级应用——数据挖掘-推荐系统
查看>>
Solr In Action 中文版 第一章(四、五)
查看>>
[GIT]
查看>>
Node.js 0.8.22 稳定版发布
查看>>
VI 你不知道的事
查看>>
loj 1030概率dp
查看>>
Expression.Bind()方法的应用
查看>>
追溯ASP.NET发展史
查看>>