zoukankan      html  css  js  c++  java
  • 网络流--最大流--POJ 1459 Power Network

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #define INF 1e9
    using namespace std;
    const int maxn=100+5;
     
    struct Edge
    {
        int from,to,cap,flow;
        Edge(){}
        Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
    };
     
    struct Dinic
    {
        int n,m,s,t;
        vector<Edge> edges;
        vector<int> G[maxn];
        int cur[maxn];
        bool vis[maxn];
        int d[maxn];
     
        void init(int n,int s,int t)
        {
            this->n=n, this->s=s, this->t=t;
            edges.clear();
            for(int i=0;i<n;i++) G[i].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()
        {
            queue<int> Q;
            memset(vis,0,sizeof(vis));
            vis[s]=true;
            d[s]=0;
            Q.push(s);
            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]=true;
                        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[e.to]==d[x]+1 && (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 max_flow()
        {
            int ans=0;
            while(BFS())
            {
                memset(cur,0,sizeof(cur));
                ans+= DFS(s,INF);
            }
            return ans;
        }
    }DC;
     
    int main()
    {
        int n,np,nc,m;
        while(scanf("%d%d%d%d",&n,&np,&nc,&m)==4)
        {
            DC.init(n+2,0,n+1);
            for(int i=0;i<m;i++)
            {
                int u,v,w;
                scanf(" (%d,%d)%d",&u,&v,&w);
                ++u,++v;
                DC.AddEdge(u,v,w);
            }
            for(int i=0;i<np;i++)
            {
                int u,w;
                scanf(" (%d)%d",&u,&w);
                ++u;
                DC.AddEdge(0,u,w);
            }
            for(int i=0;i<nc;i++)
            {
                int u,w;
                scanf(" (%d)%d",&u,&w);
                ++u;
                DC.AddEdge(u,n+1,w);
            }
            printf("%d
    ",DC.max_flow());
        }
        return 0;
    }

    Description

    A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= lmax(u,v) of power delivered by u to v. Let Con=Σuc(u) be the power consumed in the net. The problem is to compute the maximum value of Con.


    An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.

    Input

    There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of cmax(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

    Output

    For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.

    Sample Input

    2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
    7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
             (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
             (0)5 (1)2 (3)2 (4)1 (5)4

    Sample Output

    15
    6
  • 相关阅读:
    linux运维 技能树
    ORACLE:毫秒与日期的相互转换,获取某天的信息
    Mybatis通用Mapper(转)
    Redis数据类型之散列类型hash
    Redis数据类型(字符串)
    Redis整理第三波(生存时间、事务管理)
    Redis整理第二波(启动、命令)
    非关系型数据库(NOSQL)-Redis
    冒泡排序,冒泡性能优化--java实现
    学习过的设计模式总结(分别从概念、分类、优缺点、应用、区别来说)
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798629.html
Copyright © 2011-2022 走看看