HDU1532 Drainage Ditches(網絡流、EdmondsKarp)

HDU3549 Flow Problem(網絡流、EdmondsKarp)

鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3549


題目

Time Limit:5000MS Memory Limit:32768KB
Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

Output
For each test cases, you should output the maximum flow from source 1 to sink N.

Sample Input
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1

Sample Output
Case 1: 1
Case 2: 2


分析

網絡流基礎題,與HDU1532爲同一類問題,使用EdmondsKarp算法。


源碼

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,-1,sizeof x)
#define dbug cout<<"here"<<endl;
//#define LOCAL

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3+10;
const int MOD = 1000000007;

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 EdmondsKarp{
    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( ; ; ){
            mem0(a);
            queue<int> Q;
            while(!Q.empty())
                Q.pop();
            Q.push(s);
            a[s] = INF;
            while(!Q.empty()){
                int x = Q.front();
                Q.pop();
                for(int i = 0; i < 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;
    }
};

EdmondsKarp graph;

int main(){
    #ifdef LOCAL
        freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin);
        freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout);
    #endif
    int T;
    int N, M;
    int x,y,c;
    scanf("%d", &T);
    int kase = 0;
    while(T--){
        scanf("%d%d", &N, &M);
        graph.init(N);
        for(int i = 0; i < M; ++i){
            scanf("%d%d%d", &x, &y, &c);
            graph.AddEdge(x, y, c);
        }
        printf("Case %d: %d\n", ++kase, graph.MaxFlow(1, N));
    }
    return 0;
}
發佈了56 篇原創文章 · 獲贊 16 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章