程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

編輯:C++入門知識

Drainage DitchesHal Burch
Time Limit 1000 ms
Memory Limit 65536 kb
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. Note however,
that there can be more than one ditch between two intersections.
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
Input file contains multiple test cases.
In a test case:
Line 1: 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.
Line 2..N+1: Each of 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,One line with 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 4.2

 


題意:就是給出各個邊的最大流量,和起點終點,求最大流。

 


    Edmonds-Karp 增廣路算法

Code:

 

//Edmondes-Karp   
#include <cstdio>   
#include <cstring>   
#include <queue>   
#define INF 0x7fffffff   
using namespace std;  
queue<int> q;  
const int maxn = 200;  
int n, m, ans;  
int next[maxn+10], p[maxn+10], f[maxn+10][maxn+10], cap[maxn+10][maxn+10];  
int Edmondes_Karp(int s, int t) {  
    int ans = 0, v, u;  
    queue<int> q;  
    memset(f,0,sizeof(f));  
    while(true) {  
        memset(p,0,sizeof(p));  
        p[s] = INF;  
        q.push(s);  
        while(!q.empty()) { //BFS找增廣路   
            int u = q.front();  
            q.pop();  
            for(v=1; v<=m; v++)  
                if(!p[v]&&cap[u][v]>f[u][v]) { //找到新節點v   
                    next[v] = u;                //記錄v的父親,並加入FIFO隊列   
                    q.push(v);  
                    p[v] = p[u] < cap[u][v]-f[u][v]?p[u] : cap[u][v] - f[u][v];  
                    //s-v路徑上的最小殘量   
                }  
        }  
        if(!p[t]) break;    //找不到增廣路,則當前流已經是最大流   
        for(u=t; u!=s; u= next[u]) { //從匯點往回走   
            f[next[u]][u] +=p[t];//更新正向流量   
            f[u][next[u]] -=p[t];//更新反向流量   
        }  
        ans += p[t];    //更新從s流出的總流量   
    }  
    return ans;  
}  
int main() {  
    int i, k, k1, k2, k3;  
    while(~scanf("%d%d",&n,&m)) {  
        memset(cap,0,sizeof(cap));  
        for(i=1; i<=n; i++) {  
            scanf("%d%d%d",&k1,&k2,&k3);  
            cap[k1][k2] +=k3;  
        }  
        printf("%d\n",Edmondes_Karp(1,m) );  
    }  
    return 0;  
}  

//Edmondes-Karp
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x7fffffff
using namespace std;
queue<int> q;
const int maxn = 200;
int n, m, ans;
int next[maxn+10], p[maxn+10], f[maxn+10][maxn+10], cap[maxn+10][maxn+10];
int Edmondes_Karp(int s, int t) {
    int ans = 0, v, u;
    queue<int> q;
    memset(f,0,sizeof(f));
    while(true) {
        memset(p,0,sizeof(p));
        p[s] = INF;
        q.push(s);
        while(!q.empty()) { //BFS找增廣路
            int u = q.front();
            q.pop();
            for(v=1; v<=m; v++)
                if(!p[v]&&cap[u][v]>f[u][v]) { //找到新節點v
                    next[v] = u;                //記錄v的父親,並加入FIFO隊列
                    q.push(v);
                    p[v] = p[u] < cap[u][v]-f[u][v]?p[u] : cap[u][v] - f[u][v];
                    //s-v路徑上的最小殘量
                }
        }
        if(!p[t]) break;    //找不到增廣路,則當前流已經是最大流
        for(u=t; u!=s; u= next[u]) { //從匯點往回走
            f[next[u]][u] +=p[t];//更新正向流量
            f[u][next[u]] -=p[t];//更新反向流量
        }
        ans += p[t];    //更新從s流出的總流量
    }
    return ans;
}
int main() {
    int i, k, k1, k2, k3;
    while(~scanf("%d%d",&n,&m)) {
        memset(cap,0,sizeof(cap));
        for(i=1; i<=n; i++) {
            scanf("%d%d%d",&k1,&k2,&k3);
            cap[k1][k2] +=k3;
        }
        printf("%d\n",Edmondes_Karp(1,m) );
    }
    return 0;
}



 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved