zoukankan      html  css  js  c++  java
  • SGU 194 Reactor Cooling

    SGU_194

        无源汇的上下界可行流问题,这类的问题的解法可以参考:http://blog.csdn.net/water_glass/article/details/6823741,按套路来就可以了。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAXD 210
    #define MAXM 240010
    #define INF 0x3f3f3f3f
    int N, M, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM];
    int S, T, d[MAXD], q[MAXD], work[MAXD];
    struct Edge
    {
        int x, y, low, high;
    }edge[MAXM];
    void add(int x, int y, int z)
    {
        v[e] = y, flow[e] = z;
        next[e] = first[x], first[x] = e ++;    
    }
    void init()
    {
        int i, x, y;
        S = 0, T = N + 1;
        memset(first, -1, sizeof(first[0]) * T + 1);
        e = 0;
        for(i = 0; i < M; i ++)
        {
            scanf("%d%d%d%d", &edge[i].x, &edge[i].y, &edge[i].low, &edge[i].high);
            add(edge[i].x, edge[i].y, edge[i].high - edge[i].low), add(edge[i].y, edge[i].x, 0);
        }
    }
    int build()
    {
        int i, sum = 0;
        for(i = 0; i < M; i ++)
        {
            add(S, edge[i].y, edge[i].low), add(edge[i].y, S, 0), add(edge[i].x, T, edge[i].low), add(T, edge[i].x, 0);
            sum += edge[i].low;
        }
        return sum;    
    }
    int bfs()
    {
        int i, j, rear = 0;
        memset(d, -1, sizeof(d[0]) * (T + 1));
        d[S] = 0, q[rear ++] = S;
        for(i = 0; i < rear; i ++)
            for(j = first[q[i]]; j != -1; j = next[j])
                if(flow[j] && d[v[j]] == -1)
                {
                    d[v[j]] = d[q[i]] + 1, q[rear ++] = v[j];
                    if(v[j] == T) return 1;    
                }
        return 0;
    }
    int dfs(int cur, int a)
    {
        if(cur == T) return a;
        for(int &i = work[cur]; i != -1; i = next[i])
            if(flow[i] && d[v[i]] == d[cur] + 1)
                if(int t = dfs(v[i], std::min(a, flow[i])))
                {
                    flow[i] -= t, flow[i ^ 1] += t;
                    return t;
                }
        return 0;
    }
    int dinic()
    {
        int ans = 0, t;
        while(bfs())
        {
            memcpy(work, first, sizeof(first[0]) * (T + 1));
            while(t = dfs(S, INF))
                ans += t;
        }
        return ans;
    }
    void print()
    {
        int i;
        printf("YES\n");
        for(i = 0; i < M; i ++)
            printf("%d\n", flow[i << 1 | 1] + edge[i].low);    
    }
    void solve()
    {
        int sum = build();
        if(sum != dinic())
            printf("NO\n");
        else
            print();    
    }
    int main()
    {
        while(scanf("%d%d", &N, &M) == 2)
        {
            init();
            solve();    
        }
        return 0;    
    }
  • 相关阅读:
    Git忽略规则.gitignore梳理
    makefile 系统脚本分析
    Kipmi0 占用CPU 100%
    kubectl检查组件健康状态异常处理
    Java 数组如何转成List集合
    用户控件中多控件自定义单击事件
    DevExpress启动窗体SplashScreen
    使用C#获取统计局行政区划代码
    解决mysqlclient模块安装问题
    com.aspose.words 类LoadOptions
  • 原文地址:https://www.cnblogs.com/staginner/p/2638056.html
Copyright © 2011-2022 走看看