• Hdu 3549 Flow Problem(最大流)


    Flow Problem
    Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 14412 Accepted Submission(s): 6859
    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

    /*
    裸题.
    求1~n的最大流.
    多组数据哦.
    */
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #define MAXN 10001
    using namespace std;
    struct data{int v,next,c;}e[MAXN*2];
    int n,m,tot,cut,s=1,max1=1e9,head[MAXN],dis[MAXN];
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
        return x*f;
    }
    void add(int u,int v,int x)
    {
        e[++cut].v=v;
        e[cut].c=x;
        e[cut].next=head[u];
        head[u]=cut;
    }
    int bfs()
    {
        queue<int >q;
        memset(dis,-1,sizeof dis);
        dis[s]=0;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();q.pop();
            for(int i=head[u];i;i=e[i].next)
            {
                int v=e[i].v;
                if(dis[v]==-1&&e[i].c>0)
                {
                    dis[v]=dis[u]+1;
                    q.push(v);
                }
            }
        }
        return dis[n]!=-1;
    }
    int dfs(int u,int y)
    {
        int rest=0;
        if(u==n) return y;
        for(int i=head[u];i&&rest<y;i=e[i].next)
        {
            int v=e[i].v;
            if(e[i].c>0&&dis[v]==dis[u]+1)
            {
                int x=min(e[i].c,y-rest);
                x=dfs(v,x);
                rest+=x;
                e[i].c-=x;
                e[i^1].c+=x;
            }
        }
        if(!rest) dis[u]=-1;
        return rest;
    }
    int dinic(int s,int t)
    {
        int tot=0;
        while(bfs())
        {
            int ans=dfs(s,max1);
            while(ans){tot+=ans;ans=dfs(s,max1);}
        }
        return tot;
    }
    void Clear()
    {
        cut=1;//2WA
        memset(dis,-1,sizeof dis);
        memset(head,0,sizeof head);
    }
    int main()
    {
        int t,u,v,x;t=read();
        for(int i=1;i<=t;i++)
        {
            n=read();m=read();
            Clear();
            while(m--)
            {
                u=read(),v=read(),x=read();
                add(u,v,x),add(v,u,0);//1WA
            }
            printf("Case %d: %d
    ",i,dinic(1,n));
        }
        return 0;
    }
  • 相关阅读:
    会说话的TOM猫的原理是什么
    ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
    HJ2 计算某字母出现次数
    HJ1 字符串最后一个单词的长度
    windows 服务开发和windows install开发
    DICOM简介
    Active Directory 开发
    WF Workflow 状态机工作流 开发
    DICOM Query 和Retrieve 的方法和定义
    步步为营UML建模系列七、表图(Data model diagram)
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068141.html
Copyright © 2020-2023  润新知