Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 11475 Accepted Submission(s): 5437
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
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
Case 2: 2
题解:最大流入门题目,就是个模版,就是不要知道为啥,要是+=。。。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
int N;
queue<int>dl;
int vis[MAXN],pre[MAXN];
int map[MAXN][MAXN];
int s,e;
bool bfs(){
while(!dl.empty())dl.pop();
mem(vis,);
mem(pre,);
vis[s]=;
dl.push(s);
int a;
while(!dl.empty()){
a=dl.front();
dl.pop();
if(a==e)return true;
for(int i=;i<=N;i++){
if(!vis[i]&&map[a][i])dl.push(i),vis[i]=,pre[i]=a;
}
}
return false;
}
int maxflow(){
int ans=;
while(){
if(!bfs())return ans;
int a=e,temp=INF;
while(a!=s){
temp=min(temp,map[pre[a]][a]);
a=pre[a];
}a=e;
while(a!=s){
map[pre[a]][a]-=temp;
map[a][pre[a]]+=temp;
a=pre[a];
}
ans+=temp;
}
}
int main(){
int T,M,flot=;
scanf("%d",&T);
while(T--){
mem(map,);
scanf("%d%d",&N,&M);
int a,b,c;
while(M--){
scanf("%d%d%d",&a,&b,&c);
map[a][b]+=c;//加等是因为在两个点可能存在多条管道,需要合并容量。。。 }
s=;e=N;
printf("Case %d: %d\n",++flot,maxflow());
}
return ;
}
dinic算法:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=;
int edgnum;
int vis[MAXN],dis[MAXN];
int head[MAXM];
queue<int>dl;
int s,e,tflow;
struct Node{
int from,to,next,flow,cup;
}dt[MAXM];
void initial(){
mem(head,-);
edgnum=;
}
void add(int u,int v,int w){
Node E={u,v,head[u],,w};
dt[edgnum]=E;
head[u]=edgnum++;
E={v,u,head[v],,};
dt[edgnum]=E;
head[v]=edgnum++;
}
bool bfs(){
mem(vis,);mem(dis,-);
dis[s]=;
while(!dl.empty())dl.pop();
dl.push(s);
vis[s]=;
int a;
while(!dl.empty()){
a=dl.front();
dl.pop();
for(int i=head[a];i!=-;i=dt[i].next){
Node b=dt[i];
if(!vis[b.to]&&b.cup>b.flow){
dis[b.to]=dis[a]+;
vis[b.to]=;
if(b.to==e)return true;
dl.push(b.to);
}
}
}
return false;
}
/************/
int dfs(int x,int a)//把找到的这个路径上所有的边的当前流量都增加a(a是所找出路径的边中 残余流量的最小值)
{
if(x==e||a==)
return a;
int flow=,f;
for(int i=head[x];i!=-;i=dt[i].next)//从上次考虑的弧开始
{
Node &E=dt[i];
if(dis[E.to]==dis[x]+&&(f=dfs(E.to,min(a,E.cup-E.flow)))>)//可继续增广
{
E.flow+=f;//正向边
dt[i^].flow-=f;//反向边
flow+=f;//总流量 加上 f
a-=f;//最小可增流量 减去f
if(a==)
break;
}
}
return flow;}
/***************/
/*void dfs(int x,int a){
for(int i=head[x];i!=-1;i=dt[i].next){
Node &b=dt[i];
if(dis[b.to]==dis[x]+1){
if(b.cup-b.flow>0&&b.to!=e){
tflow=min(tflow,b.cup-b.flow);
dfs(b.to,min(a,b.cup-b.flow));
b.flow+=tflow;
dt[i^1].flow-=tflow;
a-=tflow;
if(!a)break;
}
}
}
}*/
int maxflow(){
int flow=;
while(bfs()){
//tflow=INF;
flow+=dfs(s,INF);
//flow+=tflow;
}
return flow;
}
int main(){
int T,N,M,flot=;
scanf("%d",&T);
while(T--){
initial();
scanf("%d%d",&N,&M);
int u,v,w;
while(M--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
s=;e=N;
printf("Case %d: %d\n",++flot,maxflow());
}
return ;
}