POJ1273--Drainage Ditches(最大流)

Discussion

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

思路

下面是最大流兩個模板

代碼

// EK算法
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

int G[300][300];
int pre[300]; //前驅結點
bool vis[300]; 
int n,m;
unsigned EK()
{
//每次用BFS找到一條路
    int v; int i;
    queue<int> q;
    memset(pre,0,sizeof(pre));
    memset(vis,0,sizeof(vis));
    pre[1] = 0; vis[1] = 1; q.push(1);
    bool flag = false;
    while( ! q.empty() ) {
        v = q.front(); q.pop();
        for( i = 1;i <= m;i ++) {
            if( G[v][i] > 0 && vis[i] == 0) {
                pre[i] = v; vis[i] = 1;
                if( i == m ) { flag = true; break; }//找到路了
                else q.push(i);
            }
        }
    }
    if( ! flag) return 0;//不再有增廣路
    int minflow = 999999999;
    v = m;
    while( pre[v] ) // 從後往前,找路上最小的流量,是這條路的流量
        { minflow = min( minflow,G[pre[v]][v]); v = pre[v]; }
    v = m;
    while( pre[v] ) //添加反向邊,修改路的流量
        { G[pre[v]][v] -= minflow; G[v][pre[v]] += minflow; v = pre[v]; }
    return minflow;
}
int main()
{
    while(cin >> n >> m)
    {
        int s,e,c ;
        memset(G,0,sizeof(G));
        for(int i = 0 ; i < n; i ++)
        {
            cin >> s >> e >> c;
            G[s][e] += c;
        }
        unsigned int MaxFlow = 0;
        unsigned int aug;
        while( aug = EK() ) // 直到流量爲0 跳出
            MaxFlow += aug; 
        cout << MaxFlow << endl;
    }
    return 0;
}
//Dinic
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <deque>
#include <cstring>
using namespace std;
const int INF = 1 << 25;
int G[300][300];
int Layer[300];
int vis[300];
int n, m; // 1 爲源點   m爲匯點
bool bfs()   // bfs 分層
{
    deque<int > q;
    memset(Layer, -1, sizeof(Layer));
    Layer[1] = 0; q.push_back(1); 
    while( !q.empty() )
    {
        int v = q.front(); q.pop_front();
        for(int i = 1; i <= m; i ++)
        {
            if(G[v][i] > 0 && Layer[i] == -1)
            {
                Layer[i] = Layer[v] + 1;
                if(i == m) return true;
                else q.push_back(i);
            }
        }
    }
    return false;
}
int dfs()
{
    int maxflow = 0; deque<int > q;
    while( bfs() )
    {
        q.push_back(1);
        memset(vis,0,sizeof(vis)); vis[1] = 1;
        while( !q.empty() )
        {
            int nd = q.back();
            if(nd == m)  //當前這個點爲匯點
            {
                int minc = INF; int mincvs;
                for(int i = 1; i < q.size(); i ++)
                {
                //尋找路上最小的流量,
                    int vs = q[i-1]; int ve = q[i];
                    if(G[vs][ve] > 0 && minc > G[vs][ve])
                        { minc = G[vs][ve]; mincvs = vs;}
                }
                maxflow += minc;
                for(int i = 1; i < q.size(); i ++)
                {
                //修改路上的流量和添加反向邊
                    int vs = q[i-1]; int ve = q[i];
                    G[vs][ve] -= minc; G[ve][vs] += minc;
                }
                while( !q.empty() && q.back() != mincvs)
                {
                //取消標記,退棧
                    vis[q.back()] = 0; q.pop_back();
                }
            }
            else 
            {
                int i;
                for(i = 1; i <= m; i ++)
                {
                //選下一層的點進棧
                    if(G[nd][i] > 0 && Layer[i] == Layer[nd] + 1 && !vis[i])
                    { vis[i] = 1; q.push_back(i); break;}                       
                }
                if( i > m ) q.pop_back();
            }
        }
    }
    return maxflow;
}
int main()
{
    while( cin >> n >> m)
    {
        int s, e, c;
        memset(G, 0, sizeof(G));
        for(int i = 0; i < n; i ++)
            { cin >> s >> e >> c; G[s][e] += c; }
        cout << dfs() << endl;
    }
    return 0;
}
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <deque>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <functional>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int maxn = 500;
const int INF = 100000000;
int n,m;
struct Edge
{
    int from,to,cap,flow;

};
struct Dinic{
    int mm,s,t;
    vector<Edge>edges;
    vector<int>G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    void AddEdge(int from,int to,int cap){
        edges.push_back((Edge){from, to, cap, 0});
        edges.push_back((Edge){to, from, 0, 0});
        mm = edges.size();
        G[from].push_back(mm-2);
        G[to].push_back(mm-1);
    }
    void init()
    {
        Del(vis,0); Del(cur,0); Del(d,0);
    }
    bool BFS(){
        int x,i;
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s] = 0;
        vis[s] = 1;
        while(!Q.empty()){
            x = Q.front(),Q.pop();
            for(i = 0;i < 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 < 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;
    }
};
int main()
{
    int u, v, s;
    while(scanf("%d %d",&m,&n) != EOF && n|m)
    {
        Dinic DC;
    //    DC.init();
        for(int i = 0; i < m; i ++)
        {
            scanf("%d %d %d",&u,&v,&s);
            DC.AddEdge(u,v,s);
           // AddEdge(v,u,1);
        }
        int ans = DC.Maxflow(1,n);
        printf("%d\n",ans);
    }
}

//審覈太慢CSDNsima

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章