zoukankan      html  css  js  c++  java
  • UVA-820 Internet Bandwidth (最大流)

    题目大意:单源单汇无向网络求最大流。

    题目分析:入门级别的题。但是ISAP在这儿好像不大好使?。。。

    代码如下:

    # include<iostream>
    # include<cstdio>
    # include<cstring>
    # include<vector>
    # include<queue>
    # include<algorithm>
    using namespace std;
    
    const int INF=1<<30;
    const int maxn=105;
    
    int p[maxn],g[maxn][maxn];
    int s,t,n,m;
    
    int bfs()
    {
        memset(p,-1,sizeof(p));
        p[s]=s;
        queue<int>q;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int i=0;i<n;++i){
                if(g[u][i]>0&&p[i]==-1){
                    p[i]=u;
                    q.push(i);
                }
            }
        }
        if(p[t]==-1) return 0;
        int a=INF,u=t;
        while(p[u]!=u){
            a=min(a,g[p[u]][u]);
            u=p[u];
        }
        u=t;
        while(p[u]!=u){
            g[p[u]][u]-=a;
            g[u][p[u]]+=a;
            u=p[u];
        }
        return a;
    }
    
    int maxFlow()
    {
        int flow=0,f;
        while(1){
            f=bfs();
            if(f<=0) break;
            flow+=f;
        }
        return flow;
    }
    
    int main()
    {
        int a,b,c,cas=0;
        while(scanf("%d",&n)&&n)
        {
            scanf("%d%d%d",&s,&t,&m);
            --s,--t;
            memset(g,0,sizeof(g));
            while(m--)
            {
                scanf("%d%d%d",&a,&b,&c);
                --a,--b;
                g[a][b]+=c;
                g[b][a]+=c;
            }
            printf("Network %d
    ",++cas);
            printf("The bandwidth is %d.
    
    ",maxFlow());
        }
        return 0;
    }
    

      

  • 相关阅读:
    遭遇争强好胜
    Redis学习笔记一:数据结构与对象
    Lua模块测试
    SQL JOIN
    Error:(1, 1) java: 非法字符: ‘ufeff’
    Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
    HTTPS为什么又快又安全?
    QA
    linux日志分割、去重、统计
    Maven:dependency scope
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/5116584.html
Copyright © 2011-2022 走看看