[Network stream Topic 4] least vertex weight coverage set

Source: Internet
Author: User

Example of the maximum stream BFS print + + Code \ n

Ahead

11.1.2018

Example

Poj 2125

Select some vertices to overwrite all edges.
It is still the minimum cut and cut point. For the two points of each edge, the source point is connected to each point to delete the weight of all edges starting from this point, that is, W -, similarly, W + center links are divided into graph edge relationships for each point to the sink point.
Then the maximum stream is enough.
A deep search is required for the solution. If the node connected to the source node cannot be accessed, it must be cut off, the access to the vertex must be cut off.

Code
# Include <iostream> # include <cstdlib> # include <cstdio> # include <cstring> # include <queue> using namespace STD; const int S = 1000, t = 1001; const int n = 40000, INF = 0x3f3f3f; int n, m; int to [n <1], NXT [n <1], last [N], W [n <1], Len = 1; inline void ins (int x, int y, int c) {to [++ Len] = Y, NXT [Len] = last [X], W [Len] = C, last [x] = Len;} int H [N]; bool vis [N]; bool BFS () {memset (H, 0, sizeof (h); queue <int> q; vis [s] = 1; q. push (S); H [s] = 1; while (! Q. empty () {int x = Q. front (); q. pop (); vis [x] = 0; For (int K = last [X]; k = NXT [k]) {int y = to [k]; if (W [k] &! H [y]) {H [y] = H [x] + 1; if (! Vis [y]) {vis [y] = 1; q. Push (y) ;}}} return H [T]! = 0;} int DFS (int x, int flow) {If (x = T) return flow; int res = flow; For (int K = last [x]; k; k = NXT [k]) {int y = to [k]; If (W [k] & H [y] = H [x] + 1) {int T = DFS (Y, min (W [K], Res); W [k]-= T; W [k ^ 1] + = T; res-= T; If (RES = 0) break;} If (RES = flow) H [x] = 0; Return Flow-res ;} inline int dinic () {int maxflow = 0; while (BFS () {int TMP = DFS (S, INF); While (TMP) {maxflow + = TMP; TMP = DFS (S, INF) ;}return maxflow;} Bo Ol OK [N]; void DFS (int x) {OK [x] = 1; for (int K = last [X]; k = NXT [k]) {int y = to [k]; If (! OK [y] & W [k]) DFS (y) ;}} int main () {int x, y, z; scanf ("% d ", & N, & M); For (INT I = 1; I <= N; ++ I) {scanf ("% d", & Z ); INS (I + N, T, Z), INS (t, I + N, 0) ;}for (INT I = 1; I <= N; ++ I) {scanf ("% d", & Z); ins (S, I, Z), INS (I, S, 0) ;}for (INT I = 1; I <= m; ++ I) {scanf ("% d", & X, & Y); ins (X, Y + N, INF ), INS (Y + n, x, 0);} printf ("% d \ n", dinic (); int res = 0; DFS (s ); for (INT I = 1; I <= N; I ++) {If (! OK [I]) RES ++; If (OK [I + N]) RES ++;} printf ("% d \ n", Res ); for (INT I = 1; I <= N; I ++) {If (! OK [I]) printf ("% d-\ n", I); If (OK [I + N]) printf ("% d + \ n ", i);} return 0 ;}

network stream Topic 4: minimum point weight coverage set

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.