Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 16993 Accepted Submission(s): 8005
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)
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
题意:
最大流裸题
Ford-Fulkerson:
自己实现的,以后应该都用Dinic,用不太到了。
#include<iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
const int inf=0x3f3f3f3f;
int n,m;
const int N = 20;
int mp[N][N];
int now[N][N];
int pre[N];
int ans;
void bfs(int u)
{
memset(now,0,sizeof(now));
queue<int> q;
int doit=1,a;
while(doit)
{
a=inf;
doit=0;
q.push(1);
memset(pre,-1,sizeof pre);
while(!q.empty())
{
u=q.front();
q.pop();
if(u==n)
{
ans+=a;
for(int i=n;pre[i]!=-1;i=pre[i])
{
if(mp[pre[i]][i])
{
now[pre[i]][i]+=a;
}
else if(mp[i][pre[i]])
{
now[i][pre[i]]-=a;
}
}
doit=1;
break;
}
for(int i=2;i<=n;i++)
{
if(mp[u][i]-now[u][i]>0&&pre[i]==-1)
{
a=min(a,mp[u][i]-now[u][i]);
pre[i]=u;
q.push(i);
}
else if(now[i][u]>0&&pre[i]==-1)
{
a=min(a,now[i][u]);
pre[i]=u;
q.push(i);
}
}
}
while(!q.empty()) q.pop();
}
}
int main()
{
int T,o=0;
scanf("%d",&T);
while(T--)
{
memset(mp,0,sizeof mp);
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v,c;
scanf("%d %d %d",&u,&v,&c);
mp[u][v]+=c;
}
ans=0;
bfs(1);
printf("Case %d: ",++o);
printf("%d\n",ans);
}
return 0;
}
#include<iostream>
#include <stdio.h>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int maxn = 20;
struct Edge
{
int u,v,cap,flow;
};
vector<Edge>Edges;
vector<int> mp[maxn];
int vis[maxn],d[maxn],cur[maxn];
int n,m;
void add(int u,int v,int cap)
{
Edge len;
len.u=u,len.v=v,len.cap=cap,len.flow=0;
Edges.push_back(len);
len.u=v,len.v=u,len.cap=0;
Edges.push_back(len);
int m=Edges.size();
mp[u].push_back(m-2);
mp[v].push_back(m-1);
}
int bfs()
{
memset(vis,0,sizeof vis);
memset(d,0,sizeof d);
queue<int>q;
q.push(1);
vis[1]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=0;i<mp[x].size();i++)
{
Edge len=Edges[mp[x][i]];
if(len.cap>len.flow&&!vis[len.v])
{
d[len.v]=d[x]+1;
q.push(len.v);
vis[len.v]=1;
}
}
}
return vis[n];
}
int dfs(int x,int a)
{
if(x==n||a==0) return a;
int flow=0,f=0;
for(int &i=cur[x];i<mp[x].size();i++)
{
Edge &len=Edges[mp[x][i]];
if(d[x]+1==d[len.v]&&(f=dfs(len.v,min(a,len.cap-len.flow)))>0)
{
a-=f;
Edges[mp[x][i]].flow+=f;
Edges[mp[x][i]^1].flow-=f;
flow+=f;
}
if(a==0) break;
}
if(flow==0) d[x]=-1;
return flow;
}
int maxflow()
{
int ans=0;
while(bfs())
{
memset(cur,0,sizeof cur);
ans+=dfs(1,inf);
}
return ans;
}
void init()
{
for(int i=0;i<maxn;i++)
{
mp[i].clear();
}
Edges.clear();
}
int main()
{
int T;
scanf("%d",&T);
int o=0;
while(T--)
{
init();
scanf("%d %d",&n,&m);
while(m--)
{
int u,v,cap;
scanf("%d %d %d",&u,&v,&cap);
add(u,v,cap);
}
printf("Case %d: ",++o);
printf("%d\n",maxflow());
}
return 0;
}