SGU 194. Reactor Cooling(无源汇有上下界的网络流)

时间限制:0.5s

空间限制:6M

题意:

显然就是求一个无源汇有上下界的网络流的可行流的问题

Solution:

没什么好说的,直接判定可行流,输出就好了

code

/*
       无汇源有上下界的网络流
*/
#include <iostream>
#include <cstring>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
const int MAXN = 209;

struct node {
    int u, v, c, ne;
} edge[MAXN * MAXN << 2];
int pHead[MAXN*MAXN], SS, ST, T, ncnt, ans;
int Gup[MAXN][MAXN], Glow[MAXN][MAXN], st[MAXN], ed[MAXN], cap[MAXN][MAXN], tflow;

void addEdge (int u, int v, int c) {
    edge[++ncnt].v = v, edge[ncnt].c = c, edge[ncnt].u = u;
    edge[ncnt].ne = pHead[u], pHead[u] = ncnt;
    edge[++ncnt].v = u, edge[ncnt].c = 0, edge[ncnt].u = v;
    edge[ncnt].ne = pHead[v], pHead[v] = ncnt;
}

int SAP (int pStart, int pEnd, int N) {
    int numh[MAXN], h[MAXN], curEdge[MAXN], pre[MAXN];
    int cur_flow, flow_ans = 0, u, neck, i, tmp;
    ms (h, 0); ms (numh, 0); ms (pre, -1);
    for (i = 0; i <= N; i++) curEdge[i] = pHead[i];
    numh[0] = N;
    u = pStart;
    while (h[pStart] <= N) {
        if (u == pEnd) {
            cur_flow = 1e9;
            for (i = pStart; i != pEnd; i = edge[curEdge[i]].v)
                if (cur_flow > edge[curEdge[i]].c) neck = i, cur_flow = edge[curEdge[i]].c;
            for (i = pStart; i != pEnd; i = edge[curEdge[i]].v) {
                tmp = curEdge[i];
                edge[tmp].c -= cur_flow, edge[tmp ^ 1].c += cur_flow;
            }
            flow_ans += cur_flow;
            u = neck;
        }
        for ( i = curEdge[u]; i != 0; i = edge[i].ne) {
            if (edge[i].v > N) continue; //重要!!!
            if (edge[i].c && h[u] == h[edge[i].v] + 1)     break;
        }
        if (i != 0) {
            curEdge[u] = i, pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else {
            if (0 == --numh[h[u]]) continue;
            curEdge[u] = pHead[u];
            for (tmp = N, i = pHead[u]; i != 0; i = edge[i].ne) {
                if (edge[i].v > N) continue; //重要!!!
                if (edge[i].c)  tmp = min (tmp, h[edge[i].v]);
            }
            h[u] = tmp + 1;
            ++numh[h[u]];
            if (u != pStart) u = pre[u];
        }
    }
    return flow_ans;
}
int solve (int n) {
    SS = n + 1, ST = n + 2;
    for (int i = 1; i <= n; i++) {
        if (ed[i]) addEdge (SS, i, ed[i]);
        if (st[i]) addEdge (i, ST, st[i]);
    }
    int tem = SAP (SS, ST, ST);
    if (tem != tflow) return 0;
    else
              return 1;
}
int n, m;
int main() {
    ios::sync_with_stdio (0);
    ncnt = 1;
    cin >> n >> m;
    for (int i = 1, u, v, x, y; i <= m; i++) {
        cin >> u >> v >> x >> y;
        Gup[u][v] = y, Glow[u][v] = x;
        st[u] += x, ed[v] += x;
        tflow += x;
        addEdge (u, v, y - x);
    }
    if (solve (n) ) {
        cout << "YES\n";
        for (int i = 2, x, y; i <= m * 2; i += 2) {
            x = edge[i].u, y = edge[i].v;
            cout << Gup[x][y]-edge[i].c << ‘\n‘;
        }
    }
    else
        cout << "NO\n";
    return 0;
}

时间: 2024-10-24 21:46:24

SGU 194. Reactor Cooling(无源汇有上下界的网络流)的相关文章

sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)

[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] 无源无汇可行流要求所有的顶点都满足流量平衡. 基本思路是转化成最大流来做. 对于边(u,v,b,c),连边(u,v,c-b).为了保持流量平衡,我们还需要连边         1.(S,u,inB[u]-outB[u])  inB>outB 2.(u,T,outB[u]-inB[u])  outB>

ZOJ 2314 Reactor Cooling 无源汇有上下界网络流

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意:给出N个点,M条边的有向图,每条边的有上下界规定,问是否存在一个可行流满足条件,如果满足输出YES并输出每条边的流量. 如果不满足输出NO. 根据周源的<一种简易的方法求解流量有上下界的网络中网络流问题> 无源汇上下界网络流的做法是: 设边u->v的下界是B(u,v),上界是C(u,v). 设M(i)为对于i结点的流入i的下界总和-流出i的下界总

ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质. 并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li. 解题思路: 转自:https://www.cnbl

ZOJ2314 Reactor Cooling 无源汇有上下界最大流

推荐看这里 #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; int n, m, uu, vv, ww, cc, hea[225], cnt, ss, tt, maxFlow, lev[225], tot, T; const int oo=0x3f3f3f3f; struct Edge{ int too, nxt, ca

SGU 194 Reactor Cooling 带容量上下限制的网络流

建立虚拟的源点和汇点,因为忽略掉了容量下界之后会导致流量不平衡,所以对于每个u,v,u多出来的流量流到汇点,v不够的流量从源点补流 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <ma

无源汇有上下界可行流存在定理

H - Reactor Cooling Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium

hdu 4940 无源汇有上下界最大流

题意:给出一个有向强连通图,每条边有两个值分别是破坏该边的代价和把该边建成无向边的代价(建立无向边的前提是删除该边)问是否存在一个集合S,和一个集合的补集T,破坏所有S集合到T集合的边代价和是X,然后修复T到S的边为无向边代价和是Y,满足Y<X:满足输出unhappy,否则输出happy:</span> <span style="font-family: Arial, Helvetica, sans-serif;">分析:无源汇有上下界可行流判定, 原来每

[loj#115] 无源汇有上下界可行流 网络流

#115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 这是一道模板题. n nn 个点,m mm 条边,每条边 e ee 有一个流量下界 lower(e) \text{lower}(e)lower(e) 和流量上界 upper(e) \text{upper}(e)upper(e),求一种可行方案使得在所有点满足流量平衡条件的前提下,所有边满足流量限

LOJ #115. 无源汇有上下界可行流

#115. 无源汇有上下界可行流 描述 这是一道模板题. n n n 个点,m m m 条边,每条边 e e e 有一个流量下界 lower(e) \text{lower}(e) lower(e) 和流量上界 upper(e) \text{upper}(e) upper(e),求一种可行方案使得在所有点满足流量平衡条件的前提下,所有边满足流量限制. 输入格式 第一行两个正整数 n n n.m m m. 之后的 m m m 行,每行四个整数 s s s.t t t.lower \text{lowe