zoukankan      html  css  js  c++  java
  • poj1273 Drainage Ditches Dinic最大流

    
    
    Drainage Ditches
    
    
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 76000   Accepted: 29530
    
    

    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.
    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
    
    

    Source

    
    
    

    /*
    * 题目:poj1273 Drainage Ditches 链接:http://poj.org/problem?id=1273 题意:裸的最大流 思路:裸的最大流 */ #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<algorithm> using namespace std; const int INF = 0x3f3f3f3f; typedef long long LL; const int N = 250; struct edge{ int to, cap, rev; }; vector<edge> G[N]; bool used[N]; void add_edge(int from,int to,int cap) { G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size()-1}); } int dfs(int v,int t,int f) { if(v==t) return f; used[v] = true; for(int i = 0; i < G[v].size(); i++){ edge&e = G[v][i]; if(!used[e.to]&&e.cap>0){ int d = dfs(e.to,t,min(f,e.cap)); if(d>0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } LL max_flow(int s,int t) { LL flow = 0; for(;;){ memset(used, 0, sizeof used); int f = dfs(s,t,INF); if(f==0) return flow; flow+=f; } } int main() { int n , m; while(scanf("%d%d",&m,&n)==2) { int u, v, cap; for(int i = 0; i <= n; i++) G[i].clear(); for(int i = 0; i < m; i++){ scanf("%d%d%d",&u,&v,&cap); add_edge(u,v,cap); } printf("%lld ",max_flow(1,n)); } return 0; }
    /**
    题目:poj1273 Drainage Ditches
    链接:http://poj.org/problem?id=1273
    题意:
    思路:Dinic算法解最大流
    
    */
    #include<iostream>
    #include<cstring>
    #include<vector>
    #include<map>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    typedef long long LL;
    const int N = 210;
    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 Dinic{
      int n, m, s, t;
      vector<Edge> edges;
      vector<int> G[N];
      bool vis[N];
      int d[N];
      int cur[N];
    
      void init(int n)
      {
        this->n = 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);
      }
    
      bool BFS(){
        memset(vis, 0, sizeof vis);
        queue<int> Q;
        Q.push(s);
        d[s] = 0;
        vis[s] = 1;
        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(!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 n, m;
        while(scanf("%d%d",&m,&n)==2){
            int from, to, cap;
            Dinic dinic;
            dinic.init(n);
            for(int i = 0; i < m; i++){
                scanf("%d%d%d",&from,&to,&cap);
                dinic.AddEdge(from,to,cap);
            }
            printf("%d
    ",dinic.Maxflow(1,n));
        }
        return 0;
    }
    /**
    题目:poj1273 Drainage Ditches
    链接:http://poj.org/problem?id=1273
    题意:裸的最大流
    思路:EdmondsKarp最大流
    
    */
    #include<iostream>
    #include<cstring>
    #include<vector>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    typedef long long LL;
    const int N = 250;
    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[N];
        int a[N];
        int p[N];
    
        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(;;){
                memset(a, 0, sizeof a);
                queue<int> Q;
                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;
        }
    };
    int main()
    {
        int  n, m;
        while(scanf("%d%d",&m,&n)==2)
        {
            EdmondsKarp ek;
            ek.init(n);
            int from, to, cap;
            for(int i = 0; i < m; i++){
                scanf("%d%d%d",&from,&to,&cap);
                ek.AddEdge(from,to,cap);
            }
            printf("%d
    ",ek.Maxflow(1,n));
        }
        return 0;
    }
  • 相关阅读:
    浅入浅出EmguCv(一)OpenCv与EmguCv
    Selenium2入门(三)WebDriver API之Get
    Selenium2入门(二)WebDriver
    Selenium2入门(一)简介
    Tomcat部署Solr4.10.4
    On the Optimal Approach of Survivable Virtual Network Embedding in Virtualized SDN
    几篇虚拟映射文章粗读
    SDN网络虚拟化中有效协调的映射算法
    SDN网络中hypervisor带来的控制器时延(Hypervisor位置的优化)
    FlowerVisor理解
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/7181944.html
Copyright © 2011-2022 走看看