题目描述:
Every time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie’s clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
输入:
InputThe input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
输出:
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
样例输入:
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出:
50
code:
这是一道最大流的模板题,给你m条边和n个点,然后给出每个边的两点以及权值,求最大流
应该算是最大流的模板题,在看《挑战程序设计》中的最大流问题时找的一个题目
但是很遗憾,书上介绍的Ford-Fulkerson算法在本题中超时
但还是放上来一下吧
超时代码:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#define inf 999999999
using namespace std;
int n,m;
struct node
{
///分别表示终点,容量,反向边
int to,cap,next;
}edge[220*4];
int head[202];
bool used[202];
int pre[202];
int cnt;
//向图中增加一条从s到t容量为cap的边
void add_edge(int u,int v,int flow)
{
edge[cnt].to=v;edge[cnt].cap=flow;edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;edge[cnt].cap=0;edge[cnt].next=head[v];
head[v]=cnt++;
}
//通过dfs寻找增广路
int dfs(int v,int t,int f)///起点,终点,流量
{
// printf("******%d\n",v);
if(v==t) return f;
used[v]=true;
for(int i=head[v];i!=-1;i=edge[i].next )//遍历以这个点为起点的所有的边
{
// printf("*******%d\n",edge[i].to);
if(!used[edge[i].to]&&edge[i].cap>0)
{
int d=dfs(edge[i].to,t,min(f,edge[i].cap));
if(d>0)
{
edge[i].cap-=d;
edge[i^1].cap+=d;
return d;
}
}
}
}
//从s到t的最大流
int max_flow(int s,int t)
{
int flow=0;
for(;;)
{
memset(used,0,sizeof(used));
int f=dfs(s,t,inf);
if(f<=0)
return flow;
flow+=f;
}
}
int main()
{
int u,v,w,s,t;
while(~scanf("%d%d",&m,&n))
{
memset(head,-1,sizeof(head));
memset(used,0,sizeof(used));
cnt=0;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
printf("%d\n",max_flow(1,n));
}
return 0;
}
下面的代码使用EK算法
AC代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
const int maxn=202;
const int inf=99999999;
int visit[maxn];
int pre[maxn];
int g[maxn][maxn];
int f[maxn][maxn];
int n,m;
bool bfs(int s,int t)
{
memset(pre,0,sizeof(pre));
memset(visit,0,sizeof(visit));
queue<int> que;
visit[s]=1;
que.push(s);
while(!que.empty())
{
int where=que.front();
que.pop();
for(int i=1;i<=n;i++)
{
if(visit[i]==0&&g[where][i]>0)
{
visit[i]=1;
pre[i]=where;
if(i==t)
return true;
que.push(i);
}
}
}
return false;
}
int ek(int s,int t)
{
int maxf=0,v,w,d;
while(bfs(s,t))
{
v=t;
d=inf;
while(v!=s)
{
w=pre[v];
d=min(d,g[w][v]);
v=w;
}
maxf+=d;
v=t;
while(v!=s)
{
w=pre[v];
g[w][v]-=d;
g[v][w]+=d;
v=w;
}
}
return maxf;
}
int main()
{
int a,b,c;
while(~scanf("%d%d",&m,&n))
{
memset(g,0,sizeof(g));
memset(f,0,sizeof(f));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
g[a][b]+=c;
}
printf("%d\n",ek(1,n));
}
return 0;
}
另外,在继续补充dinic算法,本蒟蒻现在对此算法基本属于入门阶段,等有了更深入的了解之后将继续补充
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
using namespace std;
const int maxn=220;
const int inf=0x7fffffff;
struct node{
int v,next,flow;
}edge[maxn*4];
int cnt;
int head[maxn];
int n,m,from,to;
void add_edge(int u,int v,int flow)
{
edge[cnt].v=v;edge[cnt].flow=flow;edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;edge[cnt].flow=0;edge[cnt].next=head[v];
head[v]=cnt++;
}
int d[maxn];
int bfs()
{
memset(d,0,sizeof(d));
d[from]=1;
queue<int> que;
que.push(from);
while(!que.empty())
{
int q=que.front();
que.pop();
for(int i=head[q];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==0&&edge[i].flow>0)
{
que.push(v);
d[v]=d[q]+1;
if(v==to)return 1;
}
}
}
return 0;
}
int dfs(int u,int flow)
{
if (u==to || flow==0) return flow;
int cap=flow;
for (int i=head[u] ;i!=-1 ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+1 && edge[i].flow)
{
int x=dfs(v,min(cap,edge[i].flow));
edge[i].flow -= x;
edge[i^1].flow += x;
cap -= x;
if (cap==0) return flow;
}
}
return flow-cap;
}
//int dfs(int u,int f)
//{
// if(u==to||f==0)return f;
// int cap=f;
// for(int i=head[u];i!=-1;i=edge[i].next)
// {
// int v=edge[i].v;
// if(d[v]==d[u]+1&&edge[i].flow>0)
// {
// int x=dfs(v,min(f,edge[i].flow));
// edge[i].flow-=x;
// edge[i^1].flow+=x;
// cap-=x;
// if(cap==0)return f;
// }
// }
// return f-cap;
//}
int dinic()
{
int ans=0;
while(bfs())ans+=dfs(from,inf);
return ans;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
cnt=0;
memset(head,-1,sizeof(head));
int a,b,c;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
from=1;to=n;
printf("%d\n",dinic());
}
return 0;
}
或者这样:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
using namespace std;
const int maxn=220;
const int inf=0x7fffffff;
struct node{
int v,next,flow;
}edge[maxn*4];
int cnt;
int head[maxn];
int n,m,from,to;
void add_edge(int u,int v,int flow)
{
edge[cnt].v=v;edge[cnt].flow=flow;edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;edge[cnt].flow=0;edge[cnt].next=head[v];
head[v]=cnt++;
}
int d[maxn];
int bfs()
{
memset(d,0,sizeof(d));
d[from]=1;
queue<int> que;
que.push(from);
while(!que.empty())
{
int q=que.front();
que.pop();
for(int i=head[q];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==0&&edge[i].flow>0)
{
que.push(v);
d[v]=d[q]+1;
if(v==to)return 1;
}
}
}
return 0;
}
int dfs(int u,int flow)
{
if (u==to || flow==0) return flow;
int a;
for (int i=head[u] ;i!=-1 ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+1 && edge[i].flow>0&&(a=dfs(v,min(flow,edge[i].flow))))
{
edge[i].flow -= a;
edge[i^1].flow += a;
return a;
}
}
return 0;
}
int dinic()
{
int ans=0;
while(bfs())ans+=dfs(from,inf);
return ans;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
cnt=0;
memset(head,-1,sizeof(head));
int a,b,c;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
from=1;to=n;
printf("%d\n",dinic());
}
return 0;
}