Drainage Ditches(最大流)

題目描述:

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 edge
{
    ///分別表示終點,容量,反向邊
    int to,cap,rev;
};
vector <edge> G[202];
bool used[202];
//向圖中增加一條從s到t容量爲cap的邊
bool add_edge(int from,int to,int cap)
{
 edge feng;
 feng.to=to;
 feng.cap=cap;
 feng.rev=G[to].size();
    G[from].push_back(feng);
    feng.to=from;
 feng.cap=0;
 feng.rev=G[from].size()-1;
    G[to].push_back(feng);
}
//通過dfs尋找增廣路
int dfs(int v,int t,int f)///起點,終點,流量
{
    if(v==t) return f;
    used[v]=true;
    for(int i=0; i<G[v].size(); i++)//遍歷以這個點爲起點的所有的邊
    {
        edge &e=G[v][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;
            }
        }
    }
}
//從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;
    scanf("%d%d",&m,&n);
    memset(G,0,sizeof(G));
    while(m--)
    {
        scanf("%d%d%d",&u,&v,&w);
        add_edge(u,v,w);
    }
    scanf("%d%d",&s,&t);
    printf("%d\n",max_flow(s,t));
    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,&m))
 {
  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;
 } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章