zoukankan      html  css  js  c++  java
  • HDU 3549 Dinic

    Flow Problem

    Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 8946    Accepted Submission(s): 4205


    Problem Description
    Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
     
    Input
    The first line of input contains an integer T, denoting the number of test cases.
    For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
    Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
     
    Output
    For each test cases, you should output the maximum flow from source 1 to sink N.
     
    Sample Input
    2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
     
    Sample Output
    Case 1: 1 Case 2: 2
     
    Author
    HyperHexagon
     
    Source
     
    Recommend
    zhengfeng   |   We have carefully selected several similar problems for you:  3572 3416 3081 3491 1533 
     跟上一道题一样。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<algorithm>
    using namespace std;
    const int inf=0x3fffffff;
    const int maxn=207;
    struct Edge
    {
        int cap,flow;
    };
    int n,m,s,t;
    Edge edges[maxn][maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    void init()
    {
              memset(edges,0,sizeof(edges));
    }
    bool BFS()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty()){
            int x=Q.front();Q.pop();
            for(int i=1;i<=n;i++){
                Edge &e=edges[x][i];
                if(!vis[i]&&e.cap>e.flow){
                    vis[i]=1;
                    d[i]=d[x]+1;
                    Q.push(i);
                }
            }
        }
        return vis[t];
    }
    int DFS(int u,int cp)
    {
        int tmp=cp;
        int v,t;
        if(u==n)
            return cp;
        for(v=1;v<=n&&tmp;v++)
        {
            if(d[u]+1==d[v])
            {
                if(edges[u][v].cap>edges[u][v].flow)
                {
                    t=DFS(v,min(tmp,edges[u][v].cap-edges[u][v].flow));
                    edges[u][v].flow+=t;
                    edges[v][u].flow-=t;
                    tmp-=t;
                }
            }
        }
        return cp-tmp;
    }
    int Maxflow()
    {
        int flow=0;
        while(BFS()){
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,inf);
        }
        return flow;
    }
    int main()
    {
      // freopen("in.txt","r",stdin);
       int tt,cnt=1;
       scanf("%d",&tt);
        int a,b, c;
        while(tt--){
            scanf("%d%d",&n,&m);
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                edges[a][b].cap+=c;
            }
            s=1;t=n;
            int ans=Maxflow();
            printf("Case %d: %d
    ",cnt++,ans);
        }
        return 0;
    }
  • 相关阅读:
    第2章 医疗检查安全吗?推荐!
    第3章 药物对你有何影响
    基础篇 第二节 项目管理的知识点
    第1章 简介
    魔兽争霸 意志亡灵Space专访:20岁就要死去
    第八章 陶穆太太 第九章 冒险经历 第十章 在旧地下室里
    第4章 医生在利用你试验新药吗
    第十二章 陶穆太太归来
    第十一章 爸爸妈妈不明白……
    <a>锚链接的功能取消
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4262867.html
Copyright © 2011-2022 走看看