Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 6674    Accepted Submission(s): 3112

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:  3572 3416 3081 3491 1533 
 
 

一个证明需要反向更新的例子:

这个图从1到6明显最的流量为3+3=6,但如果不用反向更新,由于用BFS首先找到一条路径1-2-3-6,然后1-2,2-3,3-6这几条边被更新成了0,就再也找不到增广路径到6了,而1-5-3的剩余的5个流量就无路可走了,用反向更新会再在3-2这里加一条流量为3的边,这时候1-5-3的5流量就可以通过这条边到2再到4再到6了,这就是反向更新的需要。

参考:http://blog.csdn.net/huanglianzheng/article/details/5754057

网络最大流入门模板题:

ford fulkerson 标号法:

 //750MS    4216K    1444 B    C++
#include<iostream>
#include<queue>
#define INF 0x7ffffff
#define N 1005
using namespace std;
int g[N][N],flow[N]; //记录流量fij, 和每次的调整值flow[e]
int father[N],vis[N]; //记录父节点 和 标记已遍历节点
int maxflow,n,m;
inline int Min(int a,int b)
{
return a<b?a:b;
}
void ford_fulkerson(int s,int e)
{
queue<int>Q;
maxflow=;
while(){ //调整到无增广链为止
memset(flow,,sizeof(flow));
memset(vis,,sizeof(vis));
flow[s]=INF;
Q.push(s);
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int v=;v<=n;v++){
if(!vis[v] && g[u][v]>){
vis[v]=;
father[v]=u;
Q.push(v);
flow[v]=Min(flow[u],g[u][v]);
}
}
if(flow[e]>){
while(!Q.empty()) Q.pop();
break;
}
}
if(flow[e]==) break;
for(int i=e;i!=s;i=father[i]){
g[father[i]][i]-=flow[e];
g[i][father[i]]+=flow[e];
}
maxflow+=flow[e];
}
}
int main(void)
{
int t,k=;
int a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(g,,sizeof(g));
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a][b]+=c;
}
ford_fulkerson(,n);
printf("Case %d: %d\n",k++,maxflow);
}
return ;
}

hdu 3549 Flow Problem (网络最大流)的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  3. HDU 3549 Flow Problem (最大流ISAP)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  4. hdu 3549 Flow Problem(最大流模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...

  5. HDU 3549 Flow Problem(最大流模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. #include< ...

  6. 题解报告:hdu 3549 Flow Problem(最大流入门)

    Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your t ...

  7. hdu 3549 Flow Problem 【最大流】

    其实还是不是很懂dinic----- 抄了一个模板--- http://www.cnblogs.com/naturepengchen/articles/4403408.html 先放在这里--- #i ...

  8. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  9. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

随机推荐

  1. C#中访问私有成员--反射

    首先我必须承认访问一个类的私有成员不是什么好做法.大家也都知道私有成员在外部是不能被访问的.而一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员访问,可以套用下面这种非常好的方式 ...

  2. 2017Noip普及组游记

    Day0 一天都基本在休息,早上信心赛,大家都是400整. 下午一群人窝在教室里打三国杀. Day1:Before Contest 早上大约十点到了试场,在考提高组,不能进. 喝了一杯咖啡去除早起的身 ...

  3. 【费元星】crt 无法上传文件,总是显示盾牌表示-完美解决

    将如下内容保存到文件中,已.bat 结尾 taskkill /f /im explorer.exeattrib -s -r -h "%userprofile%\AppData\Local\i ...

  4. 说说NSCache优于NSDictionary的几点

    1.NSCache可以提供自动删减缓存功能,而且保证线程安全,与字典不同,不会拷贝键.2.NSCache可以设置缓存上限,限制对象个数和总缓存开销.定义了删除缓存对象的时机.这个机制只对NSCache ...

  5. cf#516C. Oh Those Palindromes(最多回文子串的字符串排列方式,字典序)

    http://codeforces.com/contest/1064/problem/C 题意:给出一个字符串,要求重新排列这个字符串,是他的回文子串数量最多并输出这个字符串. 题解:字典序排列的字符 ...

  6. .NET MVC和.NET WEB api混用时注意事项

    1.同时配置了mvc路由和api路由时,mvc路由无法访问(调用所有mvc路由全部404错误) 在Global.asax中,需注意路由注册的顺序,将api路由注册放在最后: 即将 void Appli ...

  7. HDU - 6438(贪心+思维)

    链接:HDU - 6438 题意:给出 n ,表示 n 天.给出 n 个数,a[i] 表示第 i 天,物品的价格是多少.每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少 ...

  8. 【Python 开发】第二篇 :Python安装

    一.python3.x安装 1)由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. 官网:https://www.python.o ...

  9. idea clion编译器

    RNMV64P0LA-eyJsaWNlbnNlSWQiOiJSTk1WNjRQMExBIiwibGljZW5zZWVOYW1lIjoiY24gdHUiLCJhc3NpZ25lZU5hbWUiOiIiL ...

  10. [leetcode-718-Maximum Length of Repeated Subarray]

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...