Nbu 2429 Transfer stations

Source: Internet
Author: User
Tags acos

There is now a project with n (n <= 5000, number 1 ~ N) sites can be created. It takes pi to establish an I site. if both Site a and site B are established, you can earn C <a, B>. There are m relationships (m <= 50000 ). ask those sites to get the maximum net profit. question: bare max weight closure diagram. there are two ways to create a graph for this question type. i. (the original images of the created graph are completely different.) (1) Consider all edges as points (becoming profit points ), and create an edge with an infinite capacity at the two vertices that constitute this edge. (3) Use the source point (create a super source point) to create a profit edge for all profit points. (2) create a capacity edge for all the original points (becoming a consumption point) to the sink point (Building a super collection point. in this case, the minimum cut of the graph is required, and the minimum cut must be cut on the edge connected to the source or sink, because the remaining edge capacity is infinite. www.2cto.com in general, the minimum cut set is the cost of the site we do not need and the side that cannot make profits. the correctness of this method can be considered in this way. The path from the source point to the sink point must be: the source point ---> the profit side ---> the consumption side ---> Sink, and the minimum cut must be the smallest one on the three sides. after the problem is converted to a minimum cut, because the minimum cut capacity is equal to the maximum flow, you can use the profit margin and the subtraction to obtain the maximum flow. 2: (only need to be improved on the basis of the source image) for the first analysis, we can analyze the number of points = n + m + 2, the maximum n is 5000, and the maximum m is 50000, therefore, the constructed graph has 55002 points. number of edges = n + m, about 0.11 million edges. obviously, this is a huge image, and the efficiency is not good. therefore, we need a better modeling method. (1) A super source point S and a super sink point T are also required. (2) The source point is connected to all points, and the capacity is U (U is the sum of all profits), which is called zero edge. (3) All vertices are connected to the edge. The capacity is U + 2 * pi-di (di is the weight of all edges associated with I), which is called the difference edge. ps: U is used to make the capacity of the difference edge non-negative. in this way, we can look at the path from the source point to the sink point. assume that there is a profit edge a --> B in the source image, so there must be several paths to connect S and T. S ---> a ---> TS ---> B ---> TS ---> a ---> B ---> TS ---> B ---> a ---> T to make S and T not connected, the minimum cut may be the following two types. S ---> a and S ---> B, that is, all cut zero edge, the cut capacity is 2Ua ---> T and B ---> T, that is, all cut difference edge, the cut capacity is 2U + 2 * (pa + pb)-(va + vb) S ---> a and B ---> T, that is, to cut the zero side and the difference side, the cut capacity is 2U + 2 * pb-vb + weight (a, B ). S ---> B and a ---> T, that is, cut the zero side and the difference side. The cut capacity is 2U + 2 * pa-va + weight (, B ). if zero edge is cut, neither Site a nor Site B is established. Obviously, the profit is 0. on the contrary, both a and B are established, and the profit should be weimpaired (a, B)-(pa + pb ). if there is only one edge associated with a and B, that is (a, B), then va = Vb = weimpaired (a, B ). when the side is cut, the cut capacity is 2U + 2 * (pa + pb)-(va + vb) = 2U + 2 * (pa + pb) -2 * weimpaired (a, B ). assume that there is also a cut point c Associated with a, and there is no correlation between a and c except. 1 ). assume that the c site is created, so one more cut is c ---> T. the cut capacity is 3U + 2 * (pa + pb + pc)-(va + vb + vc) = 3U + 2 * (pa + pb + pc) -2 * (weimpaired (a, B) + weight (a, c )). 2 ). if the c site is not set up, then the cut capacity for the other two is S ---> c and a ---> c is 3U + 2 * (pa + pb)-(va + vb) + weight (a, c) = 3U + 2 * (pa + pb)-2 * weimpaired (a, B)-weight (a, c) + weight (a, c) = 3U + 2 * (pa + pb)-2 * weimpaired (a, B) likewise proves that this formula is also true when c or B has other correlated edges. that's why Profit = (nU-cut [S, T])/2. code (1): [cpp] # include <stdlib. h> # include <string. h> # include <stdio. h> # include <ctype. h> # include <math. h> # include <stack> # include <queue> # include <map> # include <set> # include <vector> # include <string> # include <iostream> # include <algorithm> using namespace std; # define ll long # define ls rt <1 # define rs ls | 1 # define lson l, mid, ls # define rson mid + 1, r, rs # define mi Ddle (l + r)> 1 # define eps (1e-8) # define clr_all (x, c) memset (x, c, sizeof (x) # define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1) # define MOD 1000000009 # define INF 0x3f3f3f # define PI acos (-1.0) # define _ max (x, y) (x)> (y ))? (X) :( y) # define _ min (x, y) (x) <(y ))? (X) :( y) # define _ abs (x) <0? (-(X) :( x) # define getmin (x, y) (x = (x) <0 | (y) <(x ))? (Y) :( x) # define getmax (x, y) (x = (y)> (x ))? (Y) :( x) template <class T> void _ swap (T & x, T & y) {T = x; x = y; y = t ;} int TS, cas = 1; const int M = 60000 + 5; int n, m; struct sap {typedef int type; struct edge {int to, next; type flow ;} edg [999999]; int n, m; int g [M], cur [M], pre [M]; int dis [M], gap [M]; int q [M], head, tail; void init (int _ n) {n = _ n, m = 0, clr_all (g,-1 );} void insert (int u, int v, type f, type c = 0) {edg [m]. to = v, edg [m]. flow = f, edg [m]. next = g [u], g [u] = m ++; Edg [m]. to = u, edg [m]. flow = 0, edg [m]. next = g [v], g [v] = m ++;} bool bfs (int s, int t) {clr (gap, 0, n), clr (dis, -1, n); gap [dis [t] = 0] ++, dis [s] =-1; for (q [head = tail = 0] = t; head <= tail;) {int u = q [head ++]; for (int I = g [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I], ee = edg [I ^ 1]; if (dis [e. to] =-1 & ee. flow> 0) {gap [dis [e. to] = dis [u] + 1] ++; q [++ tail] = e. to; }}} return dis [s]! =-1;} type maxFlow (int s, int t) {if (! Bfs (s, t) return 0; type res = 0, a; int I; for (I = 0; I <n; I ++) cur [I] = g [I]; pre [s] = s, cur [s] = g [s], cur [t] = g [t]; for (int u = s; dis [s] <n;) {if (u = t) {for (a =-1; u! = S; u = pre [u]) getmin (a, edg [cur [pre [u]. flow); for (u = t; u! = S; u = pre [u]) {edg [cur [pre [u]. flow-= a; edg [cur [pre [u] ^ 1]. flow + = a;} res + = a;} bool OK = 0; for (I = cur [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I]; if (dis [u] = dis [e. to] + 1 & e. flow> 0) {cur [u] = I, pre [e. to] = u, u = e. to; OK = 1; break;} if (OK) continue; int mindis = n-1; for (I = g [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I]; if (mindis> dis [e. to] & e. flow> 0) mindis = dis [e. to], cur [u] = I;} if (-- gap [dis [u] = 0) break; gap [dis [u] = mindis + 1] ++, u = pre [u];} return res ;}} p; void run () {int I, j; p. init (n + m + 2); int tot = 0; for (I = 1; I <= n; I ++) {scanf ("% d ", & j); p. insert (I, n + m + 1, j) ;}for (I = 1; I <= m; I ++) {int a, B, c; scanf ("% d", & a, & B, & c); p. insert (0, n + I, c); p. insert (n + I, a, INF); p. insert (n + I, B, INF ); Tot + = c;} printf ("% d \ n", tot-p.maxFlow (0, n + m + 1);} void preSof () {} int main () {// freopen ("input.txt", "r", stdin); // freopen ("output.txt", "w", stdout); preSof (); // run (); while (~ Scanf ("% d", & n, & m) run (); // for (scanf ("% d", & TS); cas <= TS; cas ++) run (); return 0;} code (2): [cpp] # include <stdlib. h> # include <string. h> # include <stdio. h> # include <ctype. h> # include <math. h> # include <stack> # include <queue> # include <map> # include <set> # include <vector> # include <string> # include <iostream> # include <algorithm> using namespace std; # define ll long # define ls rt <1 # define r S ls | 1 # define lson l, mid, ls # define rson mid + 1, r, rs # define middle (l + r)> 1 # define eps (1e-8) # define clr_all (x, c) memset (x, c, sizeof (x) # define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1) # define MOD 1000000009 # define INF 0x3f3f3f3f # define PI acos (-1.0) # define _ max (x, y) (x)> (y ))? (X) :( y) # define _ min (x, y) (x) <(y ))? (X) :( y) # define _ abs (x) <0? (-(X) :( x) # define getmin (x, y) (x = (x) <0 | (y) <(x ))? (Y) :( x) # define getmax (x, y) (x = (y)> (x ))? (Y) :( x) template <class T> void _ swap (T & x, T & y) {T = x; x = y; y = t ;} int TS, cas = 1; const int M = 5000 + 5; int n, m, U; int pr [M], d [M]; int a [M * 10], B [M * 10], c [M * 10]; struct sap {typedef int type; struct edge {int to, next; type flow;} edg [999999]; int n, m; int g [M], cur [M], pre [M]; int dis [M], gap [M]; int q [M], head, tail; void init (int _ n) {n = _ n, m = 0, clr_all (g, -1);} void insert (int u, int v, type uf, type vf = 0) {edg [M]. to = v, edg [m]. flow = uf, edg [m]. next = g [u], g [u] = m ++; edg [m]. to = u, edg [m]. flow = vf, edg [m]. next = g [v], g [v] = m ++;} bool bfs (int s, int t) {clr (gap, 0, n), clr (dis, -1, n); gap [dis [t] = 0] ++, dis [s] =-1; for (q [head = tail = 0] = t; head <= tail;) {int u = q [head ++]; for (int I = g [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I], ee = edg [I ^ 1]; if (dis [e. to] =-1 & ee. flow> 0) {gap [dis [e. to] = dis [u] + 1] ++; q [++ tail] = e. to; }}} return dis [s]! =-1;} type maxFlow (int s, int t) {if (! Bfs (s, t) return 0; type res = 0, a; int I; for (I = 0; I <n; I ++) cur [I] = g [I]; pre [s] = s, cur [s] = g [s], cur [t] = g [t]; for (int u = s; dis [s] <n;) {if (u = t) {for (a =-1; u! = S; u = pre [u]) getmin (a, edg [cur [pre [u]. flow); for (u = t; u! = S; u = pre [u]) {edg [cur [pre [u]. flow-= a; edg [cur [pre [u] ^ 1]. flow + = a;} res + = a;} bool OK = 0; for (I = cur [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I]; if (dis [u] = dis [e. to] + 1 & e. flow> 0) {cur [u] = I, pre [e. to] = u, u = e. to; OK = 1; break;} if (OK) continue; int mindis = n-1; for (I = g [u]; I! =-1; I = edg [I]. next) {edge & e = edg [I]; if (mindis> dis [e. to] & e. flow> 0) mindis = dis [e. to], cur [u] = I;} if (-- gap [dis [u] = 0) break; gap [dis [u] = mindis + 1] ++, u = pre [u];} return res ;}} p; void run () {int I, j; p. init (n + 1); int tot = 0; for (I = 1; I <= n; I ++) scanf ("% d ", & pr [I]); clr (d, 0, n); for (U = 0, I = 1; I <= m; I ++) {scanf ("% d", & a [I], & B [I], & c [I]); U + = c [I], d [a [I] + = c [I], d [B [I] + = c [I]; p. insert (a [I], B [I], c [I], c [I]);} For (I = 1; I <= n; I ++) {p. insert (0, I, U); p. insert (I, n + 1, U + 2 * pr [I]-d [I]);} www.2cto.com printf ("% d \ n ", (U * n-p.maxFlow (0, n + 1)/2);} void preSof () {} int main () {// freopen ("input.txt", "r ", stdin); // freopen ("output.txt", "w", stdout); preSof (); // run (); while (~ Scanf ("% d", & n, & m) run (); // for (scanf ("% d", & TS); cas <= TS; cas ++) run (); return 0 ;}

Related Article

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.