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

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

Flow Problem

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

Total Submission(s): 12860    Accepted Submission(s): 6128

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
 

 

Recommend
zhengfeng   |   We have carefully selected several similar problems for you:            
 
同hdu 1532,也是最大流模板题,不过先输入水沟的顶点数,再输入水沟数。
 
题意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速,本题就是让你求出最大流速,无疑要运用到求最大流了。题中N为水沟的顶点数,M为水沟数,接下来Si,Ei,Ci分别是水沟的起点,终点以及其容量。求源点1到终点M的最大流速。注意重边
 
附上代码:
 
#include 
#include
#include
#include
#define INF 1e9#define xmin(a,b) a
q; CL(pre,0); CL(vis,false); vis[1]=true; q.push(1); while(!q.empty()) { cur=q.front(); q.pop(); if(cur==n) return true; for(int i=1; i<=n; i++) { if(!vis[i] && mat[cur][i]) { vis[i]=true; pre[i]=cur; q.push(i); } } } return false;}int max_flow(){ int ans=0; while(1) { if(!BFS()) return ans; int Min=INF; for(int i=n; i!=1; i=pre[i]) Min=xmin(Min,mat[pre[i]][i]); for(int i=n; i!=1; i=pre[i]) { mat[pre[i]][i]-=Min; mat[i][pre[i]]+=Min; } ans+=Min; }}int main(){ int i,j,T,Case; scanf("%d",&T); for(Case=1;Case<=T;Case++) { scanf("%d%d",&n,&m); CL(mat,0); int a,b,c; while(m--) { scanf("%d%d%d",&a,&b,&c); mat[a][b]+=c; } printf("Case %d: ",Case); printf("%d\n",max_flow()); } return 0;}

 

转载于:https://www.cnblogs.com/pshw/p/5681177.html

你可能感兴趣的文章
量化策略回测DCCV2
查看>>
mongodb查询优化
查看>>
五步git操作搞定Github中fork的项目与原作者同步
查看>>
git 删除远程分支
查看>>
删远端分支报错remote refs do not exist或git: refusing to delete the current branch解决方法
查看>>
python multiprocessing遇到Can’t pickle instancemethod问题
查看>>
APP真机测试及发布
查看>>
通知机制 (Notifications)
查看>>
10 Things You Need To Know About Cocoa Auto Layout
查看>>
一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
查看>>
iOS 如何放大按钮点击热区
查看>>
ios设备唯一标识获取策略
查看>>
获取推送通知的DeviceToken
查看>>
Could not find a storyboard named 'Main' in bundle NSBundle
查看>>
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Swift code into Object-C 出现 ***-swift have not found this file 的问题
查看>>