hdu 3549Flow Problem網路流入門模板
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 23469 Accepted Submission(s): 10868
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
zhengfeng | We have carefully selected several similar problems for you: 1532 3572 3416 3081 3491
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 50
#define maxm 2000+10
#define inf 1e9
struct edge
{int from,to,ca,flow;
edge()
{
}
edge(int from,int to,int ca,int flow):from(from),to(to),ca(ca),flow(flow){}
};
struct din
{int n,m,s,t;
int head[maxn],next[maxm];
bool vis[maxn];
int d[maxn];
int cur[maxn];
edge edges[maxm];
void init(int n,int s,int t)
{this->n=n;
this->s=s;
this->t=t;
memset(head,-1,sizeof(head));
m=0;
}
void addedge(int from,int to,int cap)
{
edges[m]=edge(from,to,cap,0);
next[m]=head[from];
head[from]=m++;
edges[m]=edge(to,from,0,0);
next[m]=head[to];
head[to]=m++;
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=0;
vis[s]=true;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=head[x];i!=-1;i=next[i])
{
edge&e=edges[i];
if(!vis[e.to]&&e.ca>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{if(x==t||a==0)
return a;
int flow=0,f;
for(int&i=cur[x];i!=-1;i=next[i])
{
edge&e=edges[i];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.ca-e.flow)))>0)
{
e.flow+=f;
edges[i^1].flow-=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
}
int maxflow()
{
int flow=0;
while(bfs())
{
for(int i=1;i<=n;i++)
cur[i]=head[i];
flow+=dfs(s,inf);
}
return flow;
}
}Dc;
int main()
{
int T;
scanf("%d",&T);
for(int k=1;k<=T;k++)
{int n,m;
scanf("%d%d",&n,&m);
Dc.init(n,1,n);
int u,v,c;
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
Dc.addedge(u,v,c);
}
printf("Case %d: %d\n",k,Dc.maxflow());
}
return 0;
}
EK演算法
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 15+5
#define inf 0x3f3f3f3f
int n,m;
int flow[maxn][maxn];
int cap[maxn][maxn];
void init()
{
memset(cap,0,sizeof(cap));
}
int solve(int s,int t)
{
queue<int>q;
memset(flow,0,sizeof(flow));
int ans=0;
int a[maxn];
int p[maxn];
while(true)
{
memset(a,0,sizeof(a));
a[s]=inf;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v=1;v<=n;v++)
if(!a[v]&&cap[u][v]>flow[u][v])
{
p[v]=u;
q.push(v);
a[v]=min(a[u],cap[u][v]-flow[u][v]);
}
}
if(a[t]==0)
break;
for(int u=t;u!=s;u=p[u])
{
flow[p[u]][u]+=a[t];
flow[u][p[u]]-=a[t];
}
ans+=a[t];
}
return ans;
}
int main()
{int T;
scanf("%d",&T);
for(int k=1;k<=T;++k)
{
scanf("%d%d",&n,&m);
init();
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
cap[u][v]+=w;
}
printf("Case %d: %d\n",k,solve(1,n));
}
return 0;
}