• (网络流) Island Transport --Hdu -- 4280


    链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=4280

    源点是West, 汇点是East, 用Dinic带入求就好了

    代码:要用c++提交

    #pragma comment(linker, "/STACK:102400000,102400000")  ///手动开大栈区
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    using namespace std;
    
    #define N 100005
    #define INF 0x3fffffff
    
    struct node {int v, flow, next;} a[N<<1];
    
    int Layer[N], Head[N], cnt, n, m;
    
    void Init()
    {
        cnt = 0;
        memset(Head, -1, sizeof(Head));
    }
    
    void Add(int u, int v, int flow)
    {
        a[cnt].v = v;
        a[cnt].flow = flow;
        a[cnt].next = Head[u];
        Head[u] = cnt++;
    }
    
    bool BFS(int Start, int End)
    {
        queue<int>Q;
        Q.push(Start);
    
        memset(Layer, 0, sizeof(Layer));
        Layer[Start] = 1;
    
        while(Q.size())
        {
            int u = Q.front(); Q.pop();
    
            if(u==End) return true;
    
            for(int i=Head[u]; i!=-1; i=a[i].next)
            {
                int v = a[i].v;
    
                if(!Layer[v] && a[i].flow)
                {
                    Layer[v] = Layer[u] + 1;
                    Q.push(v);
                }
            }
        }
        return false;
    }
    int DFS(int u, int MaxFlow, int End)
    {
        if(u==End) return MaxFlow;
    
        int uflow=0;
    
        for(int i=Head[u]; i!=-1; i=a[i].next)
        {
            int v = a[i].v;
            if(Layer[v]==Layer[u]+1 && a[i].flow)
            {
                int flow = min(a[i].flow, MaxFlow-uflow);
                flow = DFS(v, flow, End);
    
                a[i].flow -= flow;
                a[i^1].flow += flow;
                uflow += flow;
    
                if(uflow==MaxFlow) break;
            }
        }
    
        if(uflow==0) Layer[u] = 0;
    
        return uflow;
    }
    int Dinic(int Start, int End)
    {
        int MaxFlow=0;
    
        while(BFS(Start, End)==true)
          MaxFlow += DFS(Start, INF, End);
    
        return MaxFlow;
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int i, u, v, flow, East=-INF, West=INF, Start, End, x, y;
    
            scanf("%d%d", &n, &m);
    
            Init();
            for(i=1; i<=n; i++)
            {
                scanf("%d%d", &x, &y);
                if(West > x)
                {
                    West = x;
                    Start = i;
                }
                if(East < x)
                {
                    East = x;
                    End = i;
                }
            }
    
    
            for(i=1; i<=m; i++)
            {
                scanf("%d%d%d", &u, &v, &flow);
                Add(u, v, flow);
                Add(v, u, flow);
            }
    
            printf("%d
    ", Dinic(Start, End));
        }
        return 0;
    }
    勿忘初心
  • 相关阅读:
    ArcPad 10 的安装部署
    各种机械键盘轴的差别,究竟什么轴好
    default argument given of parameter 的问题
    Quartz中时间表达式的设置-----corn表达式
    图像切割之(一)概述
    SMTP协议分析
    Android学习小Demo(19)利用Loader来实时接收短信
    qml动画控制器AnimationController
    httpclient 文件上传
    Java习题10.24
  • 原文地址:https://www.cnblogs.com/YY56/p/4729198.html
Copyright © 2020-2023  润新知