hdoj--3549--Flow Problem(最大流)
来源:互联网 发布:软件测试的理论知识 编辑:程序博客网 时间:2024/06/05 02:12
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 11733 Accepted Submission(s): 5565
Total Submission(s): 11733 Accepted Submission(s): 5565
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)
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
23 21 2 12 3 13 31 2 12 3 11 3 1
Sample Output
Case 1: 1Case 2: 2
Author
HyperHexagon
Source
HyperHexagon's Summer Gift (Original tasks)
Recommend
zhengfeng | We have carefully selected several similar problems for you: 1532 3572 3416 3081 3491
数组大小也是一门学问啊
#include<stdio.h>#include<string.h>#include<queue>#include<vector>#include<algorithm>using namespace std;#define MAX 200+10#define INF 10000000+10struct node{int u,v,cap,flow,next;}edge[2100];int head[MAX],cur[MAX];int vis[MAX],dis[MAX],m,n,top;void init(){top=0;memset(head,-1,sizeof(head));}void add(int a,int b,int c){node E1={a,b,c,0,head[a]};edge[top]=E1;head[a]=top++;node E2={b,a,0,0,head[b]};edge[top]=E2;head[b]=top++;}bool bfs(int s,int e){memset(vis,0,sizeof(vis));memset(dis,-1,sizeof(dis));queue<int>q;while(!q.empty()) q.pop();q.push(s);vis[s]=1;dis[s]=0;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){node E=edge[i];if(!vis[E.v]&&E.cap>E.flow){vis[E.v]=1;dis[E.v]=dis[E.u]+1;if(E.v==e)return true;q.push(E.v);}}}return false;}int dfs(int x,int a,int e){if(x==e||a==0) return a;int flow=0,f;for(int i=cur[x];i!=-1;i=edge[i].next){node& E=edge[i];if(dis[x]+1==dis[E.v]&&(f=dfs(E.v,min(a,E.cap-E.flow),e))>0){E.flow+=f;flow+=f;edge[i^1].flow-=f;a-=f;if(a==0) break;}}return flow;}int MAXflow(int s,int e){int flow=0;while(bfs(s,e)){memcpy(cur,head,sizeof(head));flow+=dfs(s,INF,e);}return flow;}void getmap(){int a,b,c;while(m--){scanf("%d%d%d",&a,&b,&c);add(a,b,c);}}int main(){int t;int k=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);init();getmap();printf("Case %d: %d\n",k++,MAXflow(1,n));}return 0;}
0 0
- HDOJ Flow Problem 3549(最大流)
- hdoj--3549--Flow Problem(最大流)
- HDOJ 3549 Flow Problem(最大流)
- hdoj 3549 Flow Problem 【最大流】
- HDOJ-3549 Flow Problem (最大流模板)
- HDOJ 题目3549 Flow Problem(最大流)
- hdoj 3549 Flow Problem---最大流---Edmond Karp算法
- hdoj Flow Problem 3549 (最大流入门)
- hdu - 3549 - Flow Problem(最大流)
- HDU 3549 Flow Problem(最大流)
- HDU 3549 Flow Problem(最大流)
- HDU 3549 Flow Problem (最大流)
- hdu 3549 Flow Problem(最大流)
- hdoj 3549 Flow Problem
- hdoj 3549 Flow Problem 【最大流入门 dinic算法】
- HDOJ-3549 Flow Problem(网络流)
- HDOJ 3549 Flow Problem(network flow)
- HDU 3549Flow Problem(网络流之最大流)
- CocoaPods安装出现的错误
- cron表达式使用
- Win7下PhoneGap配置及简单示例
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
- rtmp+fms rtmp 视频发布环境
- hdoj--3549--Flow Problem(最大流)
- Java IDE _MyEclipse 快速返回键
- android:ToolBar详解
- Valgrind:failed to start tool 'memcheck' for platform 'x86-linux': No such file or directory
- TCP/IP模型各个层次的功能和协议
- thinkphp—模块化设计
- Android安全策略
- kali下中文输入安装!
- Adobe 最终还是接受了Flash将寿终正寝的这个事实