poj 1273

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

       在这里,推荐一篇证明最大流最小割定理的文章: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 2000000000
using namespace std;

//#define LOCAL

int 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 2000000000
using 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 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值