hdu 3549 Flow Problem(EK)
2015-08-03 21:27
302 查看
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 10403 Accepted Submission(s): 4901
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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)
[align=left]Output[/align]
For each test cases, you should output the maximum flow from source 1 to sink N.
[align=left]Sample Input[/align]
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
[align=left]Sample Output[/align]
Case 1: 1 Case 2: 2
[align=left]Author[/align]
HyperHexagon
[align=left]Source[/align]
HyperHexagon's Summer Gift (Original tasks)
#include<stdio.h> #include<iostream> #include<string.h> #include<queue> using namespace std; int map[20][20],pre[20],vis[20]; int n,m; int bfs(int start,int end){ int i,t; memset(pre,-1,sizeof(pre)); memset(vis,0,sizeof(vis)); queue <int> q; while(!q.empty()) q.pop(); q.push(start); vis[start]=1; while(!q.empty()){ t=q.front(); if(t==end) return 1; q.pop(); for(i=0;i<=n+1;i++){ if(!vis[i]&&map[t][i]){ vis[i]=1; pre[i]=t; q.push(i); } } } return 0; } int maxflow(int start,int end){ int v,temp,ans=0; while(bfs(start,end)){ temp=0x3f3f3f3f; v=end; while(pre[v]!=-1){ temp=min(temp,map[pre[v]][v]); v=pre[v]; } ans+=temp; v=end; while(pre[v]!=-1){ map[pre[v]][v]-=temp; map[v][pre[v]]+=temp; v=pre[v]; } } return ans; } int main(){ int t,i,u,v,w,Case=1; scanf("%d",&t); while(t--){ memset(map,0,sizeof(map)); scanf("%d%d",&n,&m); for(i=0;i<m;i++){ scanf("%d%d%d",&u,&v,&w); map[u][v]+=w; } printf("Case %d: %d\n",Case++,maxflow(1,n)); } return 0; }
相关文章推荐
- UICntrol
- 如何处理海量数据(长文)
- HDOJ 找单词 2082【母函数】
- CodeForces 543d Road Improvement(巧妙地树形dp)
- HDU1.2.5 find your present (2)
- Java并发编程:volatile关键字解析
- [综合面试] 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- SQL Server中的联合主键、聚集索引、非聚集索引、mysql 联合索引
- [leedcode 200] Number of Islands
- 通过 Mesos、Docker 和 Go,使用 300 行代码创建一个分布式系统
- 宏的总结
- Install MariaDB Galera Cluster 10.0.20
- 在web项目中把文件放在什么地方可以直接根据url访问
- jQuery Cropper 使用说明
- 实例讲解,set,multiset,map,multimap关联容器
- POJ 1459 Power Network (最大流+多源多汇问题+入门题)
- hdu-5340 Three Palindromes(Manacher 算法)
- 帮Facebook把用户做到7亿的那个人,告诉你如何实现用户增长
- HDU - 2546 饭卡
- heap和stack的差别