A strongly connected and directed simple graph. Each edge has two values, D and B. Set S to a non-null real subset of the point set.
Q: Is there sum (d (I, j) <= sum (D (J, I) + B (J, I) for any set S )), (I, j) is the cut edge of Point Set S and s '.
No idea at all during the competition... The maximum stream is still a whiteboard.
Idea: Use the D value as the lower bound of the edge, and D + B as the upper bound of the edge. If a feasible stream exists, then for any set of S
Both have the capacity upper bound of the traffic less than or equal to the edge, and are greater than or equal to the capacity lower bound of the edge, that is, d (I, j) <= f (I, j) <= D (J, I) + B (J, I)
This translates the problem into a bare maximum stream with a lower bound. For the solution, see here.
# 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,, b) For (INT I = (a); I <= (B); ++ I) # define fed (I, B,) 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) // other # define Pb push_back // input # define RI (N) scanf ("% d", & N) # define RII (n, m) scanf ("% d", & N, & M) # define riii (n, m, k) scanf ("% d", & N, & M, & K) # define RIV (n, m, K, P) scanf ("% d", & N, & M, & K, & P) # define sqr (x) * (X) typedef long ll; typedef unsigned long lon G ull; typedef vector <int> VI; const double EPS = 1e-9; const int mod = 1000000007; const double Pi = ACOs (-1.0); const int INF = 0x3f3f3f; const int maxn = 250; // dinic algorithm // struct edge {int from, to, Cap, flow ;}; struct dinic {int N, m, S, T; vector <edge> edges; vi g [maxn]; bool vis [maxn]; int d [maxn]; int cur [maxn]; void Init (int nn) {This-> N = nn; rep (I, n + 2) g [I]. clear (); edges. clear ();} Void addedge (int from, int to, int cap) {edges. pb (edge) {from, to, Cap, 0}); edges. pb (edge) {to, from, 0, 0}); 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 x = Q. front (); q. pop (); rep (I, G [X]. size () {edge & E = edges [G [x] [I]; If (! Vis [E. to] & E. cap> E. flow) {vis [E. to] = 1; d [E. to] = d [x] + 1; q. push (E. to) ;}}} return vis [T];} int DFS (int x, int A) {If (x = T | A = 0) return; 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;} // exit when the requested traffic is greater than need. Reduce the time int maxflow (int s, int T, int need) {This-> S = s; this-> T = T; int flow = 0; while (BFS () {CLR (cur, 0); flow + = DFS (S, INF ); if (flow> need) return flow;} // minimum cut edge vector <int> mincut () {BFS (); vector <int> ans; for (INT I = 0; I <edges. size (); I ++) {edge & E = edges [I]; If (vis [E. from] &! Vis [E. to] & E. cap> 0) ans. push_back (I);} return ans;} void reduce () {for (INT I = 0; I <edges. size (); I ++) edges [I]. cap-= edges [I]. flow;} void clearflow () {for (INT I = 0; I <edges. size (); I ++) edges [I]. flow = 0 ;}} Sol; int sum [maxn]; int main () {// freopen ("0.txt"," r ", stdin); int t, x, y, b, d, TOT; int n, m; RI (t); Fe (Kase, 1, t) {CLR (sum, 0), TOT = 0; RII (n, m); Sol. init (n + 2); rep (I, m) {RIV (X, Y, D, B); sum [x]-= D; sum [y] + = D; Sol. addedge (X, Y, B);} Fe (I, 1, n) if (sum [I]> 0) Sol. addedge (0, I, sum [I]), TOT + = sum [I]; else if (sum [I] <0) Sol. addedge (I, n + 1,-sum [I]); int ans = Sol. maxflow (0, n + 1, INF); printf ("case # % d:", Kase); If (ANS = ToT) puts ("happy "); else puts ("unhappy");} return 0 ;}