zoukankan      html  css  js  c++  java
  • POJ 1459 Power Network (最大流)

    Power Network
    Time Limit: 2000MS   Memory Limit: 32768K
         

    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

    /* *************************************************
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue>
    using namespace std;
    //****************************************************
    //最大流模板Edmonds_Karp算法
    //初始化:G[][],st,ed
    //******************************************************
    const int MAXN = 100+10;
    const int INF = 0x3fffffff;
    int G[MAXN][MAXN];
    int path[MAXN],flow[MAXN],st,ed;
    int n;
    
    queue<int>Q;
    int bfs()
    {
        int t;
        memset(path,-1,sizeof(path));
        while(!Q.empty()) Q.pop();
        path[st]=0;
        flow[st]=INF;
        Q.push(st);
        while(!Q.empty()){
            t=Q.front();
            Q.pop();
            if(t==ed) break;
            for(int i=0;i<=n;i++){
                if(i!=st&&path[i]==-1&&G[t][i]){
                    flow[i]=flow[t]<G[t][i]?flow[t]:G[t][i];
                    Q.push(i);
                    path[i]=t;
                }
            }
        }
        if(path[ed]==-1) return -1;
        return flow[ed];
    }
    
    int Edmonds_Karp()
    {
        int max_flow=0;
        int step,now,pre;
        while((step=bfs())!=-1){
            max_flow+=step;
            now=ed;
            while(now!=st){
                pre=path[now];
                G[pre][now]-=step;
                G[now][pre]+=step;
                now=pre;
            }
        }
        return max_flow;
    }
    
    int main()//多源多汇点,加一个源点和汇点
    {
        int np,nc,m;
        while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF){
            n+=1;
            st=0,ed=n;
            int u,v,w;
            memset(G,0,sizeof(G));
            for(int i=1;i<=m;i++){
                while(getchar()!='(');
                scanf("%d,%d)%d",&u,&v,&w);
                u++,v++;
                G[u][v]=w;
            }
            for(int i=1;i<=np;i++){
                while(getchar()!='(');
                scanf("%d)%d",&v,&w);
                v++;
                G[st][v]=w;
            }
            for(int i=1;i<=nc;i++){
                while(getchar()!='(');
                scanf("%d)%d",&u,&w);
                u++;
                G[u][ed]=w;
            }
            printf("%d
    ",Edmonds_Karp());
        }
        return 0;
    }
    * ****************************************************** */
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    //****************************************************
    //最大流模板  SAP算法
    //邻接表形式
    //******************************************************
    
    const int MAXN = 200;//点数的最大值
    const int MAXM = 40000;//边数的最大值
    const int INF = 0x3f3f3f3f;
    
    struct Node
    {
        int from,to,next;
        int cap;
    }edge[MAXM];
    int tol;
    int head[MAXN];
    int dep[MAXN];
    int gap[MAXN];//gap[x]=y :说明残留网络中dep[i]==x的个数为y
    
    int n;//n是总的点的个数,包括源点和汇点
    
    void init()
    {
        tol=0;
        memset(head,-1,sizeof(head));
    }
    
    void addedge(int u,int v,int w)
    {
        edge[tol].from=u;
        edge[tol].to=v;
        edge[tol].cap=w;
        edge[tol].next=head[u];
        head[u]=tol++;
        edge[tol].from=v;
        edge[tol].to=u;
        edge[tol].cap=0;
        edge[tol].next=head[v];
        head[v]=tol++;
    }
    
    void BFS(int start,int end)
    {
        memset(dep,-1,sizeof(dep));
        memset(gap,0,sizeof(gap));
        gap[0]=1;
        int que[MAXN];
        int front,rear;
        front=rear=0;
        dep[end]=0;
        que[rear++]=end;
        while(front!=rear)
        {
            int u=que[front++];
            if(front==MAXN)front=0;
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(dep[v]!=-1)continue;
                que[rear++]=v;
                if(rear==MAXN)rear=0;
                dep[v]=dep[u]+1;
                ++gap[dep[v]];
            }
        }
    }
    
    int SAP(int start,int end)
    {
        int res=0;
        BFS(start,end);
        int cur[MAXN];
        int S[MAXN];
        int top=0;
        memcpy(cur,head,sizeof(head));
        int u=start;
        int i;
        while(dep[start]<n)
        {
            if(u==end)
            {
                int temp=INF;
                int inser;
                for(i=0;i<top;i++)
                   if(temp>edge[S[i]].cap)
                   {
                       temp=edge[S[i]].cap;
                       inser=i;
                   }
                for(i=0;i<top;i++)
                {
                    edge[S[i]].cap-=temp;
                    edge[S[i]^1].cap+=temp;
                }
                res+=temp;
                top=inser;
                u=edge[S[top]].from;
            }
            if(u!=end&&gap[dep[u]-1]==0)//出现断层,无增广路
              break;
            for(i=cur[u];i!=-1;i=edge[i].next)
               if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)
                 break;
            if(i!=-1)
            {
                cur[u]=i;
                S[top++]=i;
                u=edge[i].to;
            }
            else
            {
                int min=n;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                    if(edge[i].cap==0)continue;
                    if(min>dep[edge[i].to])
                    {
                        min=dep[edge[i].to];
                        cur[u]=i;
                    }
                }
                --gap[dep[u]];
                dep[u]=min+1;
                ++gap[dep[u]];
                if(u!=start)u=edge[S[--top]].from;
            }
        }
        return res;
    }
    
    int main()//多源多汇点,加一个源点和汇点
    {
        int np,nc,m;
        while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF){
            n+=1;
            int st=0,ed=n;
            n++;
            int u,v,w;
            init();
            for(int i=1;i<=m;i++){
                while(getchar()!='(');
                scanf("%d,%d)%d",&u,&v,&w);
                u++,v++;
                addedge(u,v,w);
            }
            for(int i=1;i<=np;i++){
                while(getchar()!='(');
                scanf("%d)%d",&v,&w);
                v++;
                addedge(st,v,w);
            }
            for(int i=1;i<=nc;i++){
                while(getchar()!='(');
                scanf("%d)%d",&u,&w);
                u++;
                addedge(u,ed,w);
            }
            printf("%d
    ",SAP(st,ed));
        }
        return 0;
    }


  • 相关阅读:
    Protected和Default的区别
    将数组中负数放在正数前面
    java.io包和杯子测楼
    hadoop基础
    极限编程和JUnit
    接口和抽象类
    C# 中窗口AutoScaleMode属性
    计算机的自启动管理
    labview中的移位寄存器、循环隧道,自动索引隧道的区别
    发现C#winform编程中不常用的控件(一)<FlowLayoutPanel控件><拆分器控件Splitcontainer >
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5611240.html
Copyright © 2011-2022 走看看