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
解题思路:Dicic实现,即每个阶段先进行1次bfs给图分层,然后在该图上进行1次或多次寻找增广路,如果当前层次图中已找不到增广路,就重新给图分层,然后继续找增广路,只要t的level小于0就说明当前网络已达到最大流。时间复杂度大概为O(|E||V|2)。
AC代码一(312ms):
 #include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;//int cnt=1;
struct edge{ int to, cap; size_t rev;
edge(int _to, int _cap, size_t _rev) :to(_to), cap(_cap), rev(_rev){}//构造函数,初始化结构体变量
};//指向节点to,边容量是cap,rev是记录为当前邻接点to反向边的编号
vector<edge> G[maxn];//邻接表,G[i][j]表示节点i连接的第j条边包含的所有信息
int t,n,m,x,y,c,level[maxn];//level数组在bfs时为分层图所用
void add_edge(int from,int to,int cap){//向图中增加一条从s到t容量为cap的边
G[from].push_back(edge( to, cap, G[to].size() ));
G[to].push_back(edge( from, , G[from].size() - ));//关键:反向建边,初始流量为0
}
//bfs给图分层次
void bfs(int s){
memset(level,-,sizeof(level));//刚开始每个节点的层次置为-1
queue<int> que;//队列实现bfs
level[s]=;//源点s为第0层
que.push(s);
while(!que.empty()){//给图分层
int v=que.front();que.pop();
for(size_t i=;i<G[v].size();++i){//遍历节点v与之相连的每条边
edge &e=G[v][i];//取出与节点v相连的第i条边
if(e.cap>&&level[e.to]<){//如果边残余流量大于0,且节点e.to还未分层
level[e.to]=level[v]+;//节点e.to的层次为指向它的节点v所在层次数加1
que.push(e.to);
}
}
}
}
//dfs寻找增广路,寻找当前图中s-->t的一条增广路
int dfs(int v,int t,int f){//v->t(t为汇点),当前增广路径上的最小剩余流量为f
if(v==t)return f;//到达汇点t
for(size_t i=;i<G[v].size();++i){//遍历节点v与之相连的每条边
edge &e=G[v][i];//取出与节点v相连的第i条边
if(e.cap> && level[v]<level[e.to]){//如果该边残流量大于0,且邻接点e.to是v的下一级,就增广下去
//cout<<"当前遍历到的点v:"<<v<<",邻接点to:"<<e.to<<endl;
int d=dfs(e.to,t,min(f,e.cap));//维护当前增广路上最小的剩余流量f
if(d>){//若f>0,说明找到了一条增广路
//cout<<v<<"-->"<<e.to<<",边容量为:"<<e.cap<<endl;
e.cap-=d;//正向边流量减去f
G[e.to][e.rev].cap+=d;//反向边流量加上f
return d;//沿着增广路回溯到源点s,不会在途中去深搜其他点,返回当前增广路上的最小剩余流量
}
}
}
return ;//否则说明没有增广路,返回0
}
//Dinic算法实现最大流,每个阶段执行完一次bfs分层之后,只需查找当前层次图中是否还增广路即可
int max_flow(int s,int t){
int flow=;
while(){
bfs(s);//每个阶段先bfs将图分层标记
if(level[t]<)return flow;
//如果分层之后,汇点t的层次小于0,即未被分层,说明再无增广路,则直接返回当前最大流量
int f=dfs(s,t,INF);
//cout<<"第"<<cnt++<<"次的最小剩余容量为:"<<f<<endl;
while(f>){//在该层次图中找到增广路
flow+=f;//累加到最大流中
f=dfs(s,t,INF);//继续找该层次图中是否还有增广路,直到f为0,
//cout<<"第"<<cnt++<<"次的最小剩余容量为:"<<f<<endl;
}
}
}
int main(){
while(~scanf("%d",&t)){
for(int cas=;cas<=t;++cas){
scanf("%d%d",&n,&m);//cnt=1;
for(int i=;i<=n;++i)G[i].clear();
while(m--){
scanf("%d%d%d",&x,&y,&c);
add_edge(x,y,c);
}
printf("Case %d: %d\n",cas,max_flow(,n));
}
}
return ;
}

AC代码二(93ms):当前弧优化Dinic算法。

 #include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
struct edge{ int to,cap;size_t rev;
edge(int _to, int _cap, size_t _rev):to(_to),cap(_cap),rev(_rev){}
};
int T,n,m,x,y,c,level[maxn];queue<int> que;vector<edge> G[maxn];size_t curfir[maxn];
void add_edge(int from,int to,int cap){
G[from].push_back(edge(to,cap,G[to].size()));
G[to].push_back(edge(from,,G[from].size()-));
}
bool bfs(int s,int t){
memset(level,-,sizeof(level));
while(!que.empty())que.pop();
level[s]=;
que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
for(size_t i=;i<G[v].size();++i){
edge &e=G[v][i];
if(e.cap>&&level[e.to]<){
level[e.to]=level[v]+;
que.push(e.to);
}
}
}
return level[t]<?false:true;
}
int dfs(int v,int t,int f){
if(v==t)return f;
for(size_t &i=curfir[v];i<G[v].size();++i){
edge &e=G[v][i];
if(e.cap>&&(level[v]+==level[e.to])){
int d=dfs(e.to,t,min(f,e.cap));
if(d>){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int max_flow(int s,int t){
int f,flow=;
while(bfs(s,t)){
memset(curfir,,sizeof(curfir));
while((f=dfs(s,t,INF))>)flow+=f;
}
return flow;
}
int main(){
while(~scanf("%d",&T)){
for(int cas=;cas<=T;++cas){
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)G[i].clear();
while(m--){
scanf("%d%d%d",&x,&y,&c);
add_edge(x,y,c);
}
printf("Case %d: %d\n",cas,max_flow(,n));
}
}
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

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

  3. hdu 3549 Flow Problem

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

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

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

  5. hdu 3549 Flow Problem (网络最大流)

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

  6. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  7. HDU 3549 Flow Problem 网络流(最大流) FF EK

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

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

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

  9. hdu 3549 Flow Problem (Dinic)

    Flow ProblemTime Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

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

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

随机推荐

  1. BZOJ 2115: [Wc2011] Xor DFS + 线性基

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MB Description Input 第一行包含两个整数N和 M, 表示该无向图中 ...

  2. HDU 6068 Classic Quotation KMP+DP

    Classic Quotation Problem Description When online chatting, we can save what somebody said to form h ...

  3. Apache Flink 1.5.1 Released

    Apache Flink: Apache Flink 1.5.1 Released http://flink.apache.org/news/2018/07/12/release-1.5.1.html ...

  4. linux内存操作--ioremap和mmap

    最近在做视频输出相关的东西,对于预留给framebuffer的内存使用不是很清楚,现在找到一些资料整理一下,以备使用.if (想看使用方法)  goto   使用方法: 对于一个系统来讲,会有很多的外 ...

  5. GPS格式标准

    GPS接收机串行通信标准摘要 参考NMEA-0183 美国国家海洋电子协会(NMEA—The NationalMarine Electronics Association) 为了在不同的GPS导航设备 ...

  6. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  7. CodeForces161D: Distance in Tree(树分治)

    A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a ...

  8. macbook pro 自带和用户后装的jdk的路径

    苹果系统已经包含完整的J2SE,其中就有JDK和JVM(苹果叫VM).当然如果要升级JDK,那当然要自己下载安装了. 在MAC系统中,jdk的安装路径与windows不同,默认目录是:/System/ ...

  9. Watir: 应用Watir-Webdriver 访问需要证书的网站情况

    #Suppose we will access an SVN net require 'watir-webdriver' b = Watir::Browser.new :chrome b.goto ' ...

  10. ASP.NET Core:WebAppCoreAngular

    ylbtech-ASP.NET Core:WebAppCoreAngular 1.返回顶部 1. 2. 3. 4. 5. 6. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部   ...