poj1273 Drainage Ditches (maximum flow bare title)

Problem Description

Whenever it rains John's field, Bessie's favorite clover ground will form a pond. This means that the clover is covered by water for a while, take a long time to grow back. Therefore, the farmer John built a drainage ditch, so Bessie's alfalfa field will not be covered with water. Instead, the water is discharged nearby stream. As a first-class engineers, the beginning of the Farmer John also each gutter is installed regulator, so he could control the speed of the water flow into the ditch.
Farmer John not only knows how many gallons of water per minute, each capable of delivering ditches, canals also know the exact layout of these canals flowing from the pond, flow into each other, forming a complex network of potential.
According to all this information, determine the maximum speed of the water from the pond into the creek. For any given ditch water flows in only one direction, but there may be a way to allow the water to flow in a circle.

Entry

Input comprises several situations. In each case, the first row comprises two spaces separated by an integer, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of farmer John dug ditch. M is the number of intersections of these trenches. 1 is the intersection of the pond. M is the intersection of the flow. Each line contains the following N rows three integers, Si, Ei and Ci. Si and Ei (1 <= Si, Ei <= M) indicates that the flow through the trench intersection. Si Ei from water flowing through the ditch. Ci (0 <= Ci <= 10,000,000) is the maximum speed of the water through the ditch.

Export

In each case, the output of an integer, i.e. the maximum speed of the water drained from the ponds.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

analysis:

Bare title, much bb.
Code comes with a little comment

code (EK algorithm):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
typedef long long ll;
const int inf=0x3f3f3f3f;
const int inn=0x80808080;
using namespace std;
const int maxm=205;
int n,m;
int g[maxm][maxm];
int pre[maxm];
int mark[maxm];
int bfs(int s,int t){//bfs一遍标记反向路径
    memset(mark,0,sizeof mark);
    memset(pre,0,sizeof pre);
    pre[s]=s;
    mark[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=1;i<=n;i++){
            if(!mark[i]&&g[x][i]){//如果没标记过(即没走过)且容量不为0(容量为0表示没有路)
                pre[i]=x;
                mark[i]=1;
                if(i==t)return 1;
                q.push(i);
            }
        }
    }
    return 0;
}
int ek(int s,int t){
    int ans=0;//最大流
    while(bfs(s,t)){
        int d=inf;
        for(int i=t;i!=s;i=pre[i]){//找最小detla
            if(g[pre[i]][i]<d){
                d=g[pre[i]][i];
            }
        }
        for(int i=t;i!=s;i=pre[i]){
            g[pre[i]][i]-=d;//正边减少
            g[i][pre[i]]+=d;//反边增加
        }
        ans+=d;
    }
    return ans;
}
int main(){
    while(cin>>m>>n){//个人习惯把n作为节点数量,m作为路径数量,和这题题目是反的
        memset(g,0,sizeof g);//容量为0表示没有路
        for(int i=1;i<=m;i++){
            int a,b,c;
            cin>>a>>b>>c;
            g[a][b]+=c;//重边合并成一条边
        }
        cout<<ek(1,n)<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/94987884