[Loj. AC] #117. There are Yuanhui with the lowest flow in the upper and lower bounds

Source: Internet
Author: User
Title Description

N points, M-bars, each edge E has a traffic lower bound lower (e) lower (e) \text{lower} (e) and upper bound traffic Upper (e) Upper (E) \text{upper} (e), given source point S S S and meeting point T T, which is the minimum flow from the source point to the sink point. Input Format

The first line is two positive integers n n N, M M m, s S S, T T T.

After the M m line, four integers per line s S, T T, Lower lower \text{lower}, Upper Upper \text{upper}. output Format

If there is no solution, output one line of the go home to sleep.

Otherwise, the minimum flow is output. Sample Example

Sample input
7 12 6 7
6 1 0 2147483647
1 7 0 2147483647
6 2 0 2147483647
2 7 0 2147483647
6 3 0 2147483647
3 7 0 2147483647
6 4 0 2147483647
4 7 0 2147483647
6 5 0 2147483647
5 7 0 2147483647
5 1 1 2147483647
3 4 1 2147483647
Sample output
2

Data range and hints
1≤n≤50003,1≤m≤125003 1≤n≤50003, 1≤m≤125003 1≤n≤50003,1≤m≤125003 :

Rep (i, E) {
    //scanf ("%d%d%d%d", &x, &y, &b, &c);
    Read (x); Read (y); read (b); Read (c)
    ; Add_edge (x, y, c-b);
    Add_edge (x, T, b);
    Add_edge (S, y, b);
}
Max_flow (S, T);
Add_edge (t, S, INF);
Max_flow (S, T);

After processing the lower limit of traffic, we should try to let s out to t flow less, so first to (Super Yuanhui point) s to T run the maximum flow, priority s emitted to T traffic, of course, will probably not full stream, then Add_edge (t, S, INF), and then to S to T run the maximum flow, if full stream, that The total flow of s outflow or T inflow (excluding the added (T, S, INF) edge) is the answer.
Strict proof that said no, on the internet crawled a lot of articles, also did not see who has the certification process.

Of course, can also be added to the T-s side of the capacity of the upper limit, this method is obviously no problem.

AC Code

#include <stdio.h> #include <algorithm> #include <assert.h> #include <bitset> #include < cmath> #include <complex> #include <deque> #include <functional> #include <iostream> # Include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> # Include <stdlib.h> #include <string.h> #include <string> #include <time.h>//#include < unordered_map>//#include <unordered_set> #include <vector>//#pragma comment (linker, "/stack

: 336777216 ") using namespace std; #define MP Make_pair #define PB push_back #define SE second #define FI first #define REP (i, n) for (int i = 0; i < n; + +)
i) typedef long long LL;
typedef unsigned long long ull;
typedef double DB;
typedef long double LDB;

typedef pair<int, int> PII;
const int MOD = (int) 1e9 + 7;
const int INF = 0X3F3F3F3F;
CONST LL ll_inf = 0x3f3f3f3f3f3f3f3f;
Const DB PI = ACOs (-1.0); Const DB EPS = 1e-10;
const int MAXS = 64 * 1024 * 1024;
Char Buf[maxs], *ch;
    void read (int &x) {int t = 1;
    while (*ch <=) ++ch;
    if (*ch = = '-') ++ch, t =-1;
    for (x = 0; *ch >= ' 0 '; ++ch) x = x * + *ch-48;
x = t * x;
    } void Read_init () {fread (buf, sizeof (char), MAXS, stdin);
ch = buf;
} const int MAXV = 50030<<1;

const int maxe = 125030*6;

int V, E;
    struct edge{int to, cap, NXT;
        Edge () {} edge (const int _TO, const int _CAP, const int _nxt) {to = _to;
        Cap = _cap;
    NXT = _NXT;

}}dat[maxe];

int HEAD[MAXV], LEVER[MAXV], ITER[MAXV], tail;
    void Add_edge (int from, int. to, int caps) {Dat[tail] = edge (To, Cap, Head[from]);
    Head[from] = tail++;
    Dat[tail] = Edge (from, 0, head[to]);
Head[to] = tail++;
} queue<int> que;
    void BFs (int s, int t) {memset (lever, 0xff, sizeof (int) * (T + 1));
    Lever[s] = 0;
    Que.push (s); while (!que.empty ()) {int v = que.front (); Que.pop ();
        for (int i = head[v]; ~i; i = dat[i].nxt) {Edge &e = Dat[i];
                if (Lever[e.to] < 0 && e.cap > 0) {lever[e.to] = Lever[v] + 1;
            Que.push (e.to);
    }}}} int dfs (int u, int t, int f) {if (U = = t) return F;
        for (int &v = iter[u]; ~v; v = dat[v].nxt) {Edge &e = Dat[v];
            if (lever[e.to] > Lever[u] && e.cap > 0) {int d = DFS (e.to, T, Min (E.cap, f));
                if (d > 0) {e.cap-= D;
                Dat[v^1].cap + = D;
            return D;
}}} return 0;
    } int Max_flow (int s, int t) {int res = 0, F;
        while (true) {BFS (S, t);
        if (Lever[t] < 0) return res;
        memcpy (ITER, head, sizeof (int) * (T + 1));
    while ((f = DFS (S, T, INF)) res + = f;
    }} int main () {read_init ();
    scanf ("%d%d", &v, &e);
    int S, t; Read (V); Read(E); Read (s);
    Read (t);
    int S = 0, T = v<<1|1;
    memset (head, 0xFF, sizeof (head));
    tail = 0;
    int x, y, B, C;
        Rep (i, E) {//scanf ("%d%d%d%d", &x, &y, &b, &c);
        Read (x); Read (y); read (b); Read (c);
        Add_edge (x, y, c-b);
        Add_edge (x, T, b);
    Add_edge (S, y, b);
    } max_flow (S, T);
    Add_edge (t, S, INF);
    Max_flow (S, T);
    int _flow = 0;
    bool ans = true;
    for (int v = head[s]; ~v && ans; v = dat[v].nxt) {if (Dat[v].cap! = 0) ans = false;
    } for (int v = dat[head[s]].nxt; ~v && ans; v = dat[v].nxt) {_flow + = Dat[v^1].cap;
    } if (ans) {printf ("%d\n", _flow); } else {

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.