zoukankan      html  css  js  c++  java
  • POJ1459 Power Network

    POJ1459 网络流模板

    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<map>
    using namespace std;
    const int maxn=1014;
    const int inf=1e9;
    queue<int> q;
    int n;
    int g[maxn][maxn];
    int pre[maxn];
    int flow[maxn];
    int maxflow;
    void init () {
        memset (g,0,sizeof(g));
        fill (pre,pre+maxn,0);
        fill (flow,flow+maxn,0);
        maxflow=0; 
    }
    int bfs (int s,int t) {
        while (!q.empty()) q.pop();
        for (int i=0;i<=n;i++) pre[i]=-1;
        pre[s]=0;
        q.push(s);
        flow[s]=inf;
        while (!q.empty()) {
            int x=q.front();
            q.pop();
            if (x==t) break;
            for (int i=0;i<=n;i++) 
            if (g[x][i]>0&&pre[i]==-1) {
                pre[i]=x;
                flow[i]=min(flow[x],g[x][i]);
                q.push(i);
            } 
        }
        if (pre[t]==-1) return -1;
        else return flow[t];
    }
    void Edmonds_Karp (int s,int t) {
        int increase=0;
        while ((increase=bfs(s,t))!=-1) {
            int k=t;
            while (k!=s) {
                int last=pre[k];
                g[last][k]-=increase;
                g[k][last]+=increase;
                k=last;
            }
            maxflow+=increase;
        }
    }
    int main () {
        int u,v,z,np,nc,m;
        while (~scanf("%d %d %d %d",&n,&np,&nc,&m)) {
            init ();
            while (m--) {
                while (getchar()!='(');
                scanf ("%d,%d)%d",&u,&v,&z);
                u++;
                v++;
                g[u][v]=z; 
            }
            while (np--) {
                while (getchar()!='(');
                scanf ("%d)%d",&u,&z);
                u++;
                g[0][u]=z;
            }
            while (nc--) {
                while (getchar()!='(');
                scanf ("%d)%d",&u,&z);
                u++;
                g[u][n+1]=z;
            }
            n++;
            Edmonds_Karp (0,n);
            printf ("%d
    ",maxflow);
        }
        return 0;
    }
  • 相关阅读:
    Centos 7.3 配置Xmanager XDMCP
    xstart使用方法
    Linux下安装xwindow图形界面
    使用Xftp连接Centos 6.6服务器详细图文教程
    linux远程管理器
    xftp的使用教程
    CentOS 7 关闭图形界面
    Java反射机制
    java反射的性能问题
    Java 虚拟机面试题全面解析(干货)
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/12318498.html
Copyright © 2011-2022 走看看