Poj 3308 paratroopers (minimum vertex weight overwrite in a bipartite graph-> Minimum Cut-> maximum Stream)

Source: Internet
Author: User
Poj 3308 paratroopers Link: http://poj.org/problem? Id = 3308
Question:There is a square matrix of N * m, and an umbrella soldier lands on the square matrix. Now we want to eliminate all the paratroopers. We can install an anti-aircraft gun in each column of each line. If an anti-aircraft gun is installed in a row (in a column), we can eliminate all the arms in this row (in this column). There is a charge for each line of the security anti-aircraft guns. How can I minimize the cost of installation.
Ideas:First, the question requires the minimum product. If we take the product to the logarithm of E, we will find that the result is the sum. Then a bipartite graph is abstracted. Each row is a vertex of X, and each vertex has the right value. The weight is the cost and LN is used. Each column is one of the Y parts, and the fee calculation is the same. If an umbrella soldier lands on the square, connect X to Y. The problem is that the minimum vertex weight overwrite of the Bipartite Graph is obtained. You can obtain the minimum cut. The minimum cut is the maximum stream. Therefore, a flow chart is constructed. A Source Vertex is created, and the source vertex is connected to each row. The traffic is billed. A collection vertex is created, and each column is connected to the edge. The traffic is billed. In the bipartite graph, the original edge traffic is INF. The final answer is exp (maximum stream ).
Details:INF cannot be too large. Otherwise, the precision may be incorrect.
Code:
/* ID: [email protected] prog: Lang: c ++ */# include <map> # include <set> # include <queue> # include <stack> # include <cmath> # include <cstdio> # include <vector> # include <string> # include <fstream> # include <cstring> # include <ctype. h> # include <iostream> # include <algorithm> using namespace STD; # define linf (1ll <60) # define INF 1e8 # define PI ACOs (-1.0) # define MEM (a, B) memset (a, B, sizeof (A) # define rep (I, A, n) for (INT I =; I <n; I ++) # define per (I, A, n) for (INT I = n-1; I> = A; I --) # define EPS 1e-6 # define debug puts ("================ ") # define Pb push_back // # define MP make_pair # define all (x ). begin (), (x ). end () # define Fi first # define se second # define SZ (x) (INT) (x ). size () # define posin (x, y) (0 <= (x) & (x) <n & 0 <= (y) & (y) <m) typedef long ll; typedef unsigned long ull; const int maxn = 110; cons T int maxm = 20000; int St, Ed, N, M, L; struct node {int V; // vertex double cap; // capacity double flow; // current flow in this arc int NXT;} e [maxm * 2]; int G [maxn], CNT; void add (int u, int V, double C) {e [++ CNT]. V = V; E [CNT]. CAP = C; E [CNT]. flow = 0; E [CNT]. NXT = G [u]; G [u] = CNT; E [++ CNT]. V = u; E [CNT]. CAP = 0; E [CNT]. flow = 0; E [CNT]. NXT = G [v]; G [v] = CNT;} void Init () {MEM (G, 0); CNT = 1; ST = 0; ED = m + n + 1; double W; int U, V; For (INT I = 1; I <= m; I ++) {scanf ("% lf", & W); add (St, I, log (w) ;}for (INT I = m + 1; I <ED; I ++) {scanf ("% lf", & W); add (I, Ed, log (w) ;}for (INT I = 1; I <= L; I ++) {scanf ("% d", & U, & V); add (u, M + V, INF );} N = ed + 3;} int Dist [maxn], numbs [maxn], Q [maxn]; void rev_bfs () {int font = 0, rear = 1; for (INT I = 0; I <= N; I ++) {// n indicates the total number of Di points. St [I] = maxn; numbs [I] = 0;} Q [font] = Ed; Dist [ed] = 0; numbs [0] = 1; while (font! = Rear) {int u = Q [font ++]; for (INT I = G [u]; I; I = E [I]. NXT) {If (E [I ^ 1]. CAP = 0 | Dist [E [I]. v] <maxn) continue; Dist [E [I]. v] = DIST [u] + 1; ++ numbs [Dist [E [I]. v]; Q [rear ++] = E [I]. V ;}} double maxflow () {rev_bfs (); int U; double totalflow = 0; int curg [maxn], revpath [maxn]; for (INT I = 0; I <= N; ++ I) curg [I] = G [I]; u = sT; while (Dist [st] <n) {If (u = ed) {// find an augmenting pat H double augflow = inf; For (INT I = sT; I! = Ed; I = E [curg [I]. v) augflow = min (augflow, E [curg [I]. CAP); For (INT I = sT; I! = Ed; I = E [curg [I]. v) {e [curg [I]. cap-= augflow; E [curg [I] ^ 1]. cap + = augflow; E [curg [I]. flow + = augflow; E [curg [I] ^ 1]. flow-= augflow;} totalflow + = augflow; u = sT;} int I; for (I = curg [u]; I; I = E [I]. NXT) if (E [I]. cap> 0 & Dist [u] = DIST [E [I]. v] + 1) break; if (I) {// find an admissible arc, then advance curg [u] = I; revpath [E [I]. v] = I ^ 1; u = E [I]. v;} else {// no admissible arc, Then relabel This vertex if (0 = (-- numbs [Dist [u]) break; // gap cut, important! Curg [u] = G [u]; int mindist = N; For (Int J = G [u]; j = E [J]. NXT) if (E [J]. cap> 0) mindist = min (mindist, DIST [E [J]. v]); Dist [u] = mindist + 1; ++ numbs [Dist [u]; If (u! = ST) u = E [revpath [u]. v; // backtrack} return totalflow;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & M, & N, & L); Init (); printf ("%. 4f \ n ", exp (maxflow ();} return 0 ;}


Poj 3308 paratroopers (minimum vertex weight overwrite in a bipartite graph-> Minimum Cut-> maximum Stream)

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.