关闭

hdu 1532/poj 1273 Drainage Ditches(最大流模板)

标签: ACMpoj
165人阅读 评论(0) 收藏 举报
分类:
Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 67890   Accepted: 26235

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

此题为最大流模板题

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 1111
#define INF 0x3f3f3f3f
struct Node
{
    int to;
    int cap;///容量
    int next;
    int rev;///反向边序
}edge[N];
int used[N],head[N],cnt;
void addedge(int from,int to,int cap)
{
    edge[cnt].to=to;edge[cnt].cap=cap;
    edge[cnt].next=head[from];edge[cnt].rev=cnt+1;
    head[from]=cnt++;
    edge[cnt].to=from;edge[cnt].cap=0;
    edge[cnt].next=head[to];edge[cnt].rev=cnt-1;
    head[to]=cnt++;
}
int dfs(int s,int t,int f)
{
    if(s==t) return f;
    used[s]=1;
    for(int i=head[s];i!=-1;i=edge[i].next)
    {
        Node &temp=edge[i];
        if(!used[temp.to]&&temp.cap>0)
        {
            int d=dfs(temp.to,t,min(f,temp.cap));
            if(d>0)
            {
                temp.cap-=d;
                edge[temp.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    while(1)
    {
        memset(used,0,sizeof(used));
        int d=dfs(s,t,INF);
        if(d==0) return flow;
        flow+=d;
    }
}
int main()
{
    int n,m;
    int from,to,cap;
    while(~scanf("%d %d",&m,&n))
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&from,&to,&cap);
            addedge(from,to,cap);
        }
        printf("%d\n",max_flow(1,n));
    }
    return 0;
}



Dinic算法模板:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1111
#define INF 0x3f3f3f3f
struct Node
{
    int to,cap,next;
}edge[N];
int head[N];
int d[N];
int cnt;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void addedge(int from,int to,int cap)
{
    edge[cnt].to=to;edge[cnt].cap=cap;
    edge[cnt].next=head[from];head[from]=cnt++;
    edge[cnt].to=from;edge[cnt].cap=0;
    edge[cnt].next=head[to];head[to]=cnt++;
}
int bfs(int s,int t)
{
    memset(d,-1,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(d[v]==-1&&edge[i].cap>0)
            {
                d[v]=d[u]+1;

                q.push(v);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s];i!=-1&&flow<f;i=edge[i].next)
    {
        int v=edge[i].to;
        if(d[v]==d[s]+1&&edge[i].cap>0)
        {
            int x=min(f-flow,edge[i].cap);
            x=dfs(v,t,x);
            flow+=x;
            edge[i].cap-=x;
            edge[i^1].cap+=x;
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t)///起点s终点t
{
    int flow=0,f;
    while(bfs(s,t))
    {
        while(f=dfs(s,t,INF))
            flow+=f;
    }
    return flow;
}
int main()
{
   int n,m;
   int from,to,cap;
   while(~scanf("%d %d",&m,&n))
   {
       init();
       for(int i=0;i<m;i++)
       {
           scanf("%d %d %d",&from,&to,&cap);
           addedge(from,to,cap);
       }
       printf("%d\n",Dinic(1,n));
   }
   return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:113486次
    • 积分:4948
    • 等级:
    • 排名:第6366名
    • 原创:396篇
    • 转载:11篇
    • 译文:0篇
    • 评论:21条
    最新评论