SGU194 Reactor Cooling 有下界最大流

学习别人的方法。大致是:

无源汇的最大流 : 新建源点,汇点,sum[i]为每个点进来的下界流之和减去出去的下界流之和,如果sum[i] > 0,由源点向该点建一条边,上界为sum[i],下界为0

如果sum[i] < 0, 由该点向汇点建边,上界为-sum[i],下界为0。跑一遍源点到汇点的最大流即为答案

#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <fstream>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
//OTHER
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s)
//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(n) printf("%s\n", n)
//debug
//#define online_judge
#ifndef online_judge
#define dt(a)  << (#a) << "=" << a << " "
#define debugI(a) cout dt(a) << endl
#define debugII(a, b) cout dt(a) dt(b) << endl
#define debugIII(a, b, c) cout dt(a) dt(b) dt(c) << endl
#define debugIV(a, b, c, d) cout dt(a) dt(b) dt(c) dt(d) << endl
#define debugV(a, b, c, d, e) cout dt(a) dt(b) dt(c) dt(d) dt(e) << endl
#else
#define debugI(v)
#define debugII(a, b)
#define debugIII(a, b, c)
#define debugIV(a, b, c, d)
#endif

#define sqr(x) (x) * (x)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const double eps = 1e-9;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int maxn = 100010;

struct Edge{
    int from, to, cap, flow, lcap;
};

int n, m, s, t;
vector<Edge> edges;
VI G[maxn];
bool vis[maxn];
int d[maxn], cur[maxn];

void init()
{
    REP(i, n + 1)   G[i].clear();
    edges.clear();
}

void addEdge(int from, int to, int cap, int lcap)
{
    edges.PB((Edge){from, to, cap, 0, lcap});
    edges.PB((Edge){to, from, 0, 0, -lcap});
    m = edges.size();
    G[from].PB(m - 2);
    G[to].PB(m - 1);
}

bool bfs()
{
    CLR(vis, 0);
    queue<int> Q;
    Q.push(s);
    d[s] = 0, vis[s] = 1;
    while (!Q.empty())
    {
        int u = Q.front(); Q.pop();
        REP(i, G[u].size())
        {
            Edge& e = edges[G[u][i]];
            if (!vis[e.to] && e.cap > e.flow)
            {
                vis[e.to] = 1;
                d[e.to] = d[u] + 1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}

int dfs(int x, int a)
{
    if (x == t || a == 0)
        return a;
    int flow = 0, f;
    for (int& i = cur[x];i < G[x].size(); i++)
    {
        Edge& e = edges[G[x][i]];
        if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0)
        {
            e.flow += f;
            edges[G[x][i] ^ 1].flow -= f;
            flow += f;
            a -= f;
            if (a == 0) break;
        }
    }
    return flow;
}

int maxflow(int ss, int tt)
{
    s = ss, t = tt;
    int flow = 0;
    while (bfs())
    {
        CLR(cur, 0);
        flow += dfs(s, INF);
    }
    return flow;
}

int sum[maxn];

int main()
{
    //freopen("0.txt", "r", stdin);
    int nodenum, edgenum;
    int u, v, l, c;
    int se, te;
    while (~RII(nodenum, edgenum))
    {
        CLR(sum, 0);
        n = nodenum + 2;
        init();
        REP(i, edgenum)
        {
            RIV(u, v, l, c);
            addEdge(u, v, c - l, l);
            sum[u] -= l;
            sum[v] += l;
        }
        se = edges.size(), te = se;
        FE(i, 1, nodenum)
            if (sum[i] > 0)
                addEdge(0, i, sum[i], 0), te = edges.size();
        FE(i, 1, nodenum)
            if (sum[i] < 0)
                addEdge(i, nodenum + 1, -sum[i], 0);
        int maxf = maxflow(0, nodenum + 1);
//        debugI(maxf);
//        debugII(se, te);
        bool f = 0;
        for (int i = se; i < te; i += 2)
            if (edges[i].flow < edges[i].cap)
            {
                f = 1;
                break;
            }
        if (f)
            puts("NO");
        else
        {
            puts("YES");
//            REP(i, edges.size())
            for (int i = 0; i < se; i += 2)
                printf("%d\n", edges[i].flow + edges[i].lcap);
        }
    }
    return 0;
}

SGU194 Reactor Cooling 有下界最大流

时间: 2024-08-06 09:25:53

SGU194 Reactor Cooling 有下界最大流的相关文章

zoj2314 Reactor Cooling --- 上下界可行流

题目给出了每条边的上下界, 此类题目的建边方法是: 1.添加源点汇点, 2.对每条边 添加边 c(u,v) = up(u,v) - low(u,v) 3.对每个点 c(s,v) = out(v) c(v,t) = in(v)   (权值为正) 求s到t的最大流,若最大流等于所有边下界的和,则存在可行流, 每条边的流量为 flow(u,v) +low(u,v) #include <iostream> #include <cstring> #include <string>

ZOJ 2314/SGU194 Reactor Cooling

无源汇点上下界可行流问题..... 建图: 对于一条边 u--->v  low(u,v) high(u,v) 连边 u--->v high(u,v) - low(u,v)  就变成了无上下界的网络流问题了 但这样不一定满足low的关系 ,所以我每要再每个点流量后面加上low..... 设自由流g(u,v)=high(u,v) - low(u,v) 每一个点的流量由自由流g和下界流low组成.... 变一下型: 可以看到每个点流入流出的流量不一定平衡... 我们用一个数组low记录每个点的下界流

sgu194 Reactor Cooling【无源汇有上下界可行流】

这是模板题了吧,先建立附加源汇,然后保留每个点的in-out,如果这个值是正的,那么就从附加源先这个点连一个边权为in-out的边,否则从这个点向附加汇连一条相反数的边,剩下题目中的边就用上界-下界连就好了. 1 #include <bits/stdc++.h> 2 #define rep(i, a, b) for (int i = a; i <= b; i++) 3 #define drep(i, a, b) for (int i = a; i >= b; i--) 4 #def

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

Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear re

acdream 1211 Reactor Cooling 【上下界网络流 + 输出流量】

题目:acdream 1211 Reactor Cooling 分类:无源无汇的有上下界网络流. 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质. 并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li. 例如: 46(4个点,6个pipe) 12 1 3 (1->2上界为3,下界为1) 23 1 3

ZOJ--2314--Reactor Cooling【无源汇上下界可行流】

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意:某恐怖组织要建立一个核反应堆,他们需要设计一个冷却系统,n个点由m个管子连接,为使液体循环流动,每个节点的总流入量需要等于总流出量,现告诉你每根管子的最小流量及最大流量及它们连接的两点(有向),问是否存在可行流,如存在,输出每个管子的流量. 有上下界的网络流分为四种:无源汇的上下界可行流.有源汇的上下界可行流.有源汇的上下界最大流.有源汇的上下界最小流,这道

acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】

称号:acdream 1211 Reactor Cooling 分类:无汇的有上下界网络流. 题意: 给n个点.及m根pipe,每根pipe用来流躺液体的.单向的.每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体.里面流躺物质. 而且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题).同一时候最小不能低于Li. 比如: 46(4个点,6个pipe) 12 1 3 (1->2上界为3,下界为1) 23 1 3

ZOJ2314 Reactor Cooling

Reactor Cooling Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for the nuclear bomb they are planni

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

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