zoukankan      html  css  js  c++  java
  • POJ 1459 Power Network

    最大流问题。


    题意,表示电网有三个站点, 发电站。集线器,用户站。 超直接建立S 和 T 。

    S-> 发电站 发电量。用户-> T 容量是电。

    然后,你可以找到的最大流量。



    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<stack>
    #include<iostream>
    #include<list>
    #include<set>
    #include<cmath>
    #define INF 0x7fffffff
    #define eps 1e-6
    #define LL long long
    #define acfun std::ios::sync_with_stdio(false)
    using namespace std;
    
    int n,np,nc,m;
    struct lx
    {
        int c,f;
    };
    lx g[301][301];
    bool vis[301];
    int path[301];
    int flow[301];
    
    void EK(int start,int thend)
    {
        int maxflow=0;
        while(1)
        {
            for(int i=0;i<n+2;i++)
                vis[i]=0,path[i]=-1,flow[i]=0;
            queue<int>q;
            q.push(start);
            vis[start]=1,flow[start]=INF;
            while(!q.empty()&&!vis[thend])
            {
                int u=q.front();q.pop();
                for(int j=0;j<n+2;j++)
                {
                    if(vis[j])continue;
                    if(g[u][j].f<g[u][j].c)
                    {
                        vis[j]=1;
                        path[j]=u;
                        flow[j]=min(flow[u],g[u][j].c-g[u][j].f);
                        q.push(j);
    
                    }
                    else if(g[j][u].f>0)
                    {
                        vis[j]=1;
                        path[j]=u;
                        flow[j]=min(flow[u],g[j][u].f);
                        q.push(j);
                    }
                }
            }
            if(!vis[thend]||flow[thend]==0)break;
    
            int v=thend,u=path[thend];
            int tmp=flow[thend];
    
            maxflow+=tmp;
    
            while(u!=-1)
            {
                if(g[u][v].f<g[u][v].c)
                    g[u][v].f+=tmp;
                else
                    g[v][u].f-=tmp;
                v=u,u=path[v];
    
            }
        }
        printf("%d
    ",maxflow);
    }
    
    int main()
    {
        while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
        {
            for(int i=0;i<n+2;i++)
                for(int j=0;j<n+2;j++)
                g[i][j].c=g[i][j].f=0;
            int u,v,c;
            char str[1001];
            while(m--)
            {
                scanf("%s",str);
                sscanf(str,"(%d,%d)%d",&u,&v,&c);
                g[u][v].c=c;
    
            }
            for(int i=0;i<np;i++)
            {
                scanf("%s",str);
                sscanf(str,"(%d)%d",&v,&c);
                g[n][v].c=c;
            }
            for(int i=0;i<nc;i++)
            {
                scanf("%s",str);
                sscanf(str,"(%d)%d",&u,&c);
                g[u][n+1].c=c;
            }
            EK(n,n+1);
        }
    }
    


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Android 常见adb命令
    下载安装JDK,并且配置java环境变量
    安装黑苹果教程
    创建不死目录、不死文件
    win10下安装centos7双系统
    Hadoop 3.0完全分布式集群搭建方法(CentOS 7+Hadoop 3.2.0)
    Hadoop 2.0完全分布式集群搭建方法(CentOS7+Hadoop 2.7.7)
    启动HBase脚本start-hbase.sh时报Class path contains multiple SLF4J bindings.解决方法
    HQuorumPeer和QuorumPeerMain进程的区别
    Zookeeper集群安装与配置
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4853209.html
Copyright © 2011-2022 走看看