HDU1532最大流 Edmonds-Karp,Dinic算法 模板

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45 Accepted Submission(s): 38
 
Problem 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
 
 
Source
USACO 93
 

题意:

裸的最大流

代码:

//Edmonds-Karp算法,紫书366页。模板。点的编号从0开始。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=202,inf=0x7fffffff;
struct edge{
    int from,to,cap,flow;
    edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Edmonds_Karp{
    int n,m;
    vector<edge>edges;//边数的两倍
    vector<int>g[maxn];//邻接表,g[i][j]表示节点i的第j条边在e数组中的序号
    int a[maxn];//当起点到i的可改进量
    int p[maxn];//最短路树上p的入弧编号
    void init(int n){
        for(int i=0;i<n;i++) g[i].clear();
        edges.clear();
    }
    void addedge(int from,int to,int cap){
        edges.push_back(edge(from,to,cap,0));
        edges.push_back(edge(to,from,0,0));//反向弧
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    int Maxflow(int s,int t){
        int flow=0;
        for(;;){
            memset(a,0,sizeof(a));
            queue<int>q;
            q.push(s);
            a[s]=inf;
            while(!q.empty()){
                int x=q.front();q.pop();
                for(int i=0;i<(int)g[x].size();i++){
                    edge&e=edges[g[x][i]];
                    if(!a[e.to]&&e.cap>e.flow){
                        p[e.to]=g[x][i];
                        a[e.to]=min(a[x],e.cap-e.flow);
                        q.push(e.to);
                    }
                }
                if(a[t]) break;
            }
            if(!a[t]) break;
            for(int u=t;u!=s;u=edges[p[u]].from){
                edges[p[u]].flow+=a[t];
                edges[p[u]^1].flow-=a[t];
            }
            flow+=a[t];
        }
        return flow;
    }
}EK;
int main()
{
    int n,m,a,b,c;
    while(scanf("%d%d",&n,&m)==2){
        EK.init(m);
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&a,&b,&c);
            a--;b--;
            EK.addedge(a,b,c);
        }
        printf("%d\n",EK.Maxflow(0,m-1));
    }
    return 0;
}

 

//Dinic算法模板 白书358页,点的编号从0开始
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=202;
const int inf=0x7fffffff;
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>g[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    void Init(int n){
        this->n=n;
        for(int i=0;i<n;i++) g[i].clear();
        edges.clear();
    }
    void Addedge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));//反向弧
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool Bfs(){
        memset(vis,0,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<(int)g[x].size();i++){
                Edge &e=edges[g[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int Dfs(int x,int a){
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int&i=cur[x];i<(int)g[x].size();i++){
            Edge &e=edges[g[x][i]];
            if(d[x]+1==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[g[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s,int t){
        this->s=s;this->t=t;
        int flow=0;
        while(Bfs()){
            memset(cur,0,sizeof(cur));
            flow+=Dfs(s,inf);
        }
        return flow;
    }
}dc;
int main()
{
    int n,m,a,b,c;
    while(scanf("%d%d",&n,&m)==2){
        dc.Init(m);
        while(n--){
            scanf("%d%d%d",&a,&b,&c);
            a--;b--;
            dc.Addedge(a,b,c);
        }
        printf("%d\n",dc.Maxflow(0,m-1));
    }
    return 0;
}

 

//anather
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int INF=0x7fffffff;
const int MAXN=10002;//点数
const int MAXM=10002;//边数
int n,m,tot,S,T,head[MAXN],h[MAXN],q[MAXN],ans;
struct Edge { int to,val,next; }edge[MAXM];
void init(int last)
{
    S=0;T=last-1;//S源点,T汇点
    tot=0;
    memset(head,-1,sizeof(head));
}
void addedge(int x,int y,int z)
{
    edge[tot].to=y;edge[tot].val=z;edge[tot].next=head[x];
    head[x]=tot++;
}
bool bfs()
{
    memset(h,-1,sizeof(h));
    int top=0,last=1;
    q[top]=S;h[S]=0;
    while(top<last){
        int now=q[top];top++;
        for(int i=head[now];i!=-1;i=edge[i].next){
            if(edge[i].val&&h[edge[i].to]<0){
                q[last++]=edge[i].to;
                h[edge[i].to]=h[now]+1;
            }
        }
    }
    if(h[T]==-1) return 0;
    return 1;
}
int dfs(int x,int f)
{
    if(x==T) return f;
    int w,used=0;
    for(int i=head[x];i!=-1;i=edge[i].next){
        if(edge[i].val&&h[edge[i].to]==h[x]+1){
            w=f-used;
            w=dfs(edge[i].to,min(w,edge[i].val));
            edge[i].val-=w;
            edge[i^1].val+=w;
            used+=w;
            if(used==f) return f;
        }
    }
    if(!used) h[x]=-1;
    return used;
}
int dinic()
{
    int ans=0;
    while(bfs()) ans+=dfs(S,INF);
    return ans;
}
int main()
{
    int n,m,a,b,c;
    while(scanf("%d%d",&n,&m)==2){
        init(m);//根据题目传参
        while(n--){
            scanf("%d%d%d",&a,&b,&c);
            a--;b--;
            addedge(a,b,c);
            addedge(b,a,0);//建反向边
            //建边根据题目而定
        }
        int ans=dinic();
        printf("%d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6369229.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值