HDU 3549 Flow Problem 最大流 最小增广路 EK算法 传说中的入门算法
2013-06-25 16:39
363 查看
题目描述
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.
因为两个点有多条边的情况。需要全部加起来。
直接上模板。。
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define inf 0x3f3f3f3f
#define MAXN 1005
#define clr(x,k) memset((x),(k),sizeof(x))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000
typedef vector<int> vi;
typedef stack<int> si;
typedef vector<string> vs;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i = 0;i < n;++i)
#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int Map[MAXN][MAXN],n,m,p[MAXN]; //map:邻接数组;n:点数;p:前驱数组
void init(){
clr(Map,0);
for(int i = 0,a,b,c;i < m;i++){
scanf("%d %d %d",&a,&b,&c);
Map[a][b] += c; //存在多重边
}
}
bool EK_Bfs(int start,int end){
queue <int>que; //宽度悠闲搜索队列
int flag[MAXN];
clr(flag,0);
clr(p,-1);
que.push(start);
flag[start]=true;
while(!que.empty()){
int e = que.front();
if(e==end) //当队头弹到终点时,即可判定增广路存在
return true;
que.pop();
for(int i= 1;i <=n;i++){
if(Map[e][i]&&!flag[i]){
flag[i] = 1;
p[i]=e;
que.push(i);
}
}
}
return false;
}
int KM_Max_Flow(int start,int end){
int u,flow_ans=0,mn;
while(EK_Bfs(start,end)){
mn = inf;
u = end;
while(p[u]!=-1){
mn = min(mn,Map[p[u]][u]);
u = p[u];
}
flow_ans+=mn;
u=end;
while(p[u]!=-1){
Map[p[u]][u]-=mn;
Map[u][p[u]]+=mn;
u=p[u];
}
}
return flow_ans;
}
int main(){
//freopen("3594.in","r",stdin);
int t,tt=0;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
init();
printf("Case %d: %d\n",++tt,KM_Max_Flow(1,n));
}
return 0;
}
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.
因为两个点有多条边的情况。需要全部加起来。
直接上模板。。
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define inf 0x3f3f3f3f
#define MAXN 1005
#define clr(x,k) memset((x),(k),sizeof(x))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000
typedef vector<int> vi;
typedef stack<int> si;
typedef vector<string> vs;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i = 0;i < n;++i)
#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int Map[MAXN][MAXN],n,m,p[MAXN]; //map:邻接数组;n:点数;p:前驱数组
void init(){
clr(Map,0);
for(int i = 0,a,b,c;i < m;i++){
scanf("%d %d %d",&a,&b,&c);
Map[a][b] += c; //存在多重边
}
}
bool EK_Bfs(int start,int end){
queue <int>que; //宽度悠闲搜索队列
int flag[MAXN];
clr(flag,0);
clr(p,-1);
que.push(start);
flag[start]=true;
while(!que.empty()){
int e = que.front();
if(e==end) //当队头弹到终点时,即可判定增广路存在
return true;
que.pop();
for(int i= 1;i <=n;i++){
if(Map[e][i]&&!flag[i]){
flag[i] = 1;
p[i]=e;
que.push(i);
}
}
}
return false;
}
int KM_Max_Flow(int start,int end){
int u,flow_ans=0,mn;
while(EK_Bfs(start,end)){
mn = inf;
u = end;
while(p[u]!=-1){
mn = min(mn,Map[p[u]][u]);
u = p[u];
}
flow_ans+=mn;
u=end;
while(p[u]!=-1){
Map[p[u]][u]-=mn;
Map[u][p[u]]+=mn;
u=p[u];
}
}
return flow_ans;
}
int main(){
//freopen("3594.in","r",stdin);
int t,tt=0;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
init();
printf("Case %d: %d\n",++tt,KM_Max_Flow(1,n));
}
return 0;
}
相关文章推荐
- HDU 3549 Flow Problem 最大流 最小增广路 SAP算法 从EK算法的753MS降到了46MS
- HDU 3549 Flow Problem 最大流 最小增广路 DINIC算法 也是46MS
- hdu 3549 Flow Problem【最大流增广路入门模板题】
- hdu 3549 Flow Problem【最大流增广路入门模板题】
- HDU 3549 Flow Problem(网络流入门题-最大流的Ford-Fulkerson算法)
- Flow Problem 最大流 最小增广路 SAP算法 从EK算法的753MS降到了46MS
- HDU 3549 Flow Problem 流问题(最大流,入门)
- hdu 3549 Flow Problem(增广路算法)
- hdu 3549 Flow Problem 最大流入门 EK算法
- HDU 3549 Flow Problem【最大流入门题】【Ford-Fulkerson算法】【Dinic算法】【ISAP算法】
- HDU 3549 最大流 EK算法 邻接表实现
- HDU 1150 Machine Schedule(匈牙利算法 二分图的最小顶点覆盖 二分图最大匹配)
- HDU 3549 Flow Problem 解题报告(EK)算法
- 【最大流(EK)】hdu 3549 Flow Problem
- hdu------(3549)Flow Problem(最大流(水体))
- HDU 3549--Flow Problem 【最大流 && dinic】
- hdu 1863 畅通工程 最小生成树模板入门题 prim+kruskal两种算法AC。
- hdu 3549 Flow Problem (最大流)
- hdu 3549 Flow Problem 最大流
- HDU 3549 Flow Problem 解题报告(EK)算法