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
HyperHexagon’s Summer Gift (Original tasks)
Recommend
模板题,求1到V的最大流,这里使用了两个算法,好像Dinic算法快一些
Ford-Fulkerson算法:
#include<bits/stdc++.h>
using namespace std;
const int MAX_V = 10010;
const int INF = 0x3f3f3f3f;
struct Edge{
int to,cap,rev;
};
//rev是一条逆边,就是有向边的反向,rev指向的是u的第几条边
vector<Edge> G[MAX_V];
void add(int u,int v,int cap){
G[u].push_back((Edge){v,cap,(int)G[v].size()});
G[v].push_back((Edge){u,0,(int)G[u].size()-1});
}
bool used[MAX_V];
int dfs(int s,int t,int f){
if(s == t) return f;
used[s] = true;
for(int i=0;i<G[s].size();i++){
Edge &e = G[s][i];
if(!used[e.to] && e.cap > 0){
int d = dfs(e.to,t,min(f,e.cap));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int solve(int s,int t){
int res = 0;
while(true){
memset(used,false,sizeof(used));
int f = dfs(s,t,INF);
if(f == 0) return res;
res += f;
}
}
int main(void){
int t,V,E;
int Case = 0;
scanf("%d",&t);
while(t--){
scanf("%d %d",&V,&E);
for(int i=1;i<=V;i++)
G[i].clear();
int x,y,z;
for(int i=1;i<=E;i++){
scanf("%d %d %d",&x,&y,&z);
add(x,y,z);
}
printf("Case %d: ",++Case);
printf("%d\n",solve(1,V));
}
return 0;
}
Dinic 算法
#include<bits/stdc++.h>
using namespace std;
const int MAX_V = 10010;
const int INF = 0x3f3f3f3f;
struct Edge{
int to,cap,rev;
};
vector<Edge> G[MAX_V];
int level[MAX_V],iter[MAX_V];
void add(int u,int v,int cap){
G[u].push_back((Edge){v,cap,(int)G[v].size()});
G[v].push_back((Edge){u,0,(int)G[u].size()-1});
}
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int> q;
level[s] = 0;q.push(s);
while(!q.empty()){
int v = q.front();q.pop();
for(int i=0;i<G[v].size();i++){
Edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0){
level[e.to] = level[v] + 1;
q.push(e.to);
}
}
}
}
int dfs(int s,int t,int f){
if(s == t) return f;
for(int &i = iter[s];i < G[s].size();i++){
Edge &e = G[s][i];
if(e.cap > 0 && level[e.to] > level[s]){
int d = dfs(e.to,t,min(f,e.cap));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int solve(int s,int t){
int res = 0;
while(true){
bfs(s);
if(level[t] < 0) return res;
memset(iter,0,sizeof(iter));
int f;
while((f = dfs(s,t,INF)) > 0)
res += f;
}
}
int main(void){
int t,V,E;
int Case = 0;
scanf("%d",&t);
while(t--){
scanf("%d %d",&V,&E);
for(int i=0;i<=V;i++)
G[i].clear();
int x,y,z;
for(int i=1;i<=E;i++){
scanf("%d %d %d",&x,&y,&z);
add(x,y,z);
}
printf("Case %d: ",++Case);
printf("%d\n",solve(1,V));
}
return 0;
}