Sgu194 Reactor Cooling has a lower bound maximum stream

Source: Internet
Author: User

Learn other people's methods. Roughly:

Maximum stream without a source sink: The New Source Vertex, sink vertex, and sum [I] are the sum of the lower bound streams that come in to each vertex minus the sum of the lower bound streams that exit. If sum [I]> 0, creates an edge from The Source Vertex To This vertex. The upper bound is sum [I] and the lower bound is 0.

If sum [I] <0, an edge is created from the vertex to the vertex. The upper bound is-sum [I], and the lower bound is 0. The maximum stream from the source point to the sink point is the answer


#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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.