poj 1273

来源:互联网 发布:鬼子来了 知乎 编辑:程序博客网 时间:2024/05/15 11:05

       入门级最大流的题,不过如果你用邻接矩阵的话,要注意有重边。

       在这里,推荐一篇证明最大流最小割定理的文章:http://wenku.baidu.com/link?url=u_lLVv5Nk0XqXNcnUEKhaDupdpkYKD6IMe45PK0QafProSoW86QWQFEnz2JNKC0KdTVPz97DcwiZEFz9m85C9W-ub5bJsnp47WrNtsjGVHm。

       在这道题中,我用了基于邻接矩阵的Edmond-Karp算法(参考《算法竞赛入门经典》P209),然后也用了Ford-Fulkerson算法(参考《挑战程序设计竞赛》P211)。

       这两种方法思想上很类似,都是基于增广路定理,即先从源点搜索到汇点,然后增广,直到找不到增广路,此时的网络流为最大流。不过在找增广路是,前一个用BFS,后一个用DFS。

        这道题是我第一次用《挑战程序设计竞赛》中的邻接表的代码,发现在poj上,要用G++来提交,用C++会CE,因为《挑战程序设计竞赛》上邻接表的代码很简洁,以至于poj的C++认为有语法问题。此外,由于邻接表是基于STL的vector,所以每次用完之后要clear,这是书上代码没有体现的。虽然这两种算法思路上不复杂,但是细节上还是很值得多多体会。

代码(C++,Edmond-Karp算法):

#include <cstdlib>#include <iostream>#include <queue>#define MAX 209 #define INF 2000000000using namespace std;//#define LOCALint cap[MAX][MAX],flow[MAX][MAX],a[MAX],pre[MAX];int main(int argc, char *argv[]){#ifdef LOCAL    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif        int n,m,p,e,c,s,t,i,u,ans;    queue<int> qi;    while(scanf("%d %d",&n,&m)!=EOF)  //n表示边数,m表示点数               {        s=1;   //1为源点,m为汇点        t=m;                    memset(cap,0,sizeof(cap));                    for(i=0;i<n;i++)        {            scanf("%d %d %d",&p,&e,&c);             cap[p][e]+=c;               //到有重边                 }                         memset(flow,0,sizeof(flow));          ans=0;        pre[s]=s;                while(1)        {              memset(a,-1,sizeof(a));                   qi.push(s);                          a[s]=INF;                        while(!qi.empty())             {                u=qi.front();                qi.pop();                for(i=1;i<=m;i++)                {                    if(a[i]==-1&&cap[u][i]>flow[u][i])                    {                        a[i]=min(a[u],cap[u][i]-flow[u][i]);                        pre[i]=u;                                                      qi.push(i);                    }                            }              }                if(a[t]==-1) break;                 for(i=t;i!=s;i=pre[i])             {                 flow[pre[i]][i]+=a[t];                 flow[i][pre[i]]-=a[t];                               }             ans+=a[t];              }         printf("%d\n",ans);     }     system("PAUSE");    return EXIT_SUCCESS;}

代码(G++,Ford-Fulkerson算法): 

#include <cstdio>#include <iostream>#include <vector>#include <cstring>#define MAX 209#define INF 2000000000using namespace std;struct Edge{    int to;    int c;    int rev;};vector<Edge> G[MAX];bool vis[MAX];void add_edge(int from,int to,int cap){     G[from].push_back((Edge){to,cap,G[to].size()});     G[to].push_back((Edge){from,0,G[from].size()-1});}int dfs(int v,int t,int f){    int i,d;    if(v==t) return f;    vis[v]=true;    for(i=0;i<G[v].size();i++)    {        Edge &e=G[v][i];        if(e.c>0&&!vis[e.to])        {            d=dfs(e.to,t,min(f,e.c));            if(d>0)            {                e.c-=d;                G[e.to][e.rev].c+=d;                return d;            }        }    }    return 0;}int main(){    //freopen("in.txt","r",stdin);    int n,m,p,e,c,i,s,t,ans,f;    while(scanf("%d %d",&n,&m)!=EOF)  //n表示边数,m表示点数    {        s=1;   //1为源点,m为汇点        t=m;        for(i=0;i<n;i++)        {            scanf("%d %d %d",&p,&e,&c);            add_edge(p,e,c);        }        ans=0;        while(1)        {            memset(vis,false,sizeof(vis));            f=dfs(s,t,INF);            if(f==0) break;            ans+=f;        }        for(i=1;i<=m;i++) G[i].clear();        printf("%d\n",ans);    }    return 0;}

题目(http://poj.org/problem?id=1273):

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K   

Description

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. 

Input

The 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.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 41 2 401 4 202 4 202 3 303 4 10

Sample Output

50

0 0
原创粉丝点击