网络流入门——Drainage Ditches POJ1273

问题描述

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



代码实现

·首次尝试使用静态邻接表

·照着模板敲了Dinic算法

·在创建图的时候忘记存反向边调试了好久

·第一次提交TLE:

  --scanf后忘加!=EOF

  --没有注意题上的N,M数据范围,数组开错大小

  --建立图时,最开始index应该初始化为0,而不是1。e与e^1的组合是01 23 45。


AC代码如下

#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <cstring>

using namespace std;

const long edge_maxn=300;
const long point_maxn=300;
const long INF=0xfffffff;

struct node{
    int v;
    int f;
    int next;
}edge[edge_maxn];

int first[point_maxn];
int n,m;

void init(){
    memset(first,-1,sizeof(first));
    int index=0;
    int x,y,f;
    for(int i=0;i<n;i++){
        cin>>x>>y>>f;

        edge[index].v=y;
        edge[index].f=f;
        edge[index].next=first[x];
        first[x]=index;
        index++;
        edge[index].v=x;
        edge[index].f=0;
        edge[index].next=first[y];
        first[y]=index;
        index++;
    }
}

int level[point_maxn];
bool bfs(int s,int t){
    memset(level,0,sizeof(level));
    level[s]=1;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int x=q.front();q.pop();
        if(x==t) return true;


        for(int e=first[x];e!=-1;e=edge[e].next){
            int v=edge[e].v, f=edge[e].f;
            if(!level[v] && f){
                level[v]=level[x]+1;
                q.push(v);
            }
        }
    }

    return false;

}

int dfs(int u,int maxf,int t){
    if(u==t) return maxf;
    int flow=0;
    for(int e=first[u];e!=-1;e=edge[e].next){
        int v=edge[e].v,f=edge[e].f;

        if(level[u]+1 == level[v] && f){
            int Min=min(maxf-flow,f);
            f=dfs(v,Min,t);
            edge[e].f-=f;
            edge[e^1].f+=f;
            flow+=f;
            if(flow==maxf) return flow;

        }
    }
    return flow;
}

int dinic(int s,int t){
    int ans=0;
    while(bfs(s,t)){
        ans+=dfs(s,INF,t);
    }
    return ans;
}

int main(){
    while(scanf("%d %d",&n,&m)!=EOF){
        init();
        int ans=dinic(1,m);
        cout<<ans<<endl;
    }
}



猜你喜欢

转载自blog.csdn.net/adobemomo/article/details/79722069