Poj 3469 dual core CPU (minimum cut)

Source: Internet
Author: User

Poj 3469 dual core CPU Link: http://poj.org/problem? Id = 3469
Question: There are two CPUs and N modules. Each module runs on a single CPU. There is m for information interaction between modules. If the module is on the same CPU, no additional time is required for information interaction; otherwise, additional time is required. Ask how to allocate modules to minimize the time spent.
Ideas: To divide the module into two CPUs, and at the same time to minimize the time, it is to find the minimum cut. Then the question can be converted to the maximum stream.
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 (1 <30) # define PI ACOs (-1.0) # define MEM (a, B) memset (a, B, sizeof (A) # define rep (I, A, n) for (INT I = A; 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 = 1210 00; const int maxm = 1410000; int St, Ed, n, m; struct node {int V; // vertex int cap; // capacity int flow; // current flow in this arc int NXT;} e [maxm * 2]; int G [maxn], CNT; void add (int u, int V, int 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 = n + 1; int U, V, C; For (INT I = 1; I <= N; I ++) {scanf ("% d", & U, & V); add (St, I, U); add (I, Ed, V );} while (M --) {scanf ("% d", & U, & V, & C); add (u, v, c); add (V, u, c) ;}n = N + 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 DIST [I] = maxn; numbs [I] = 0;} Q [font] = Ed; Dist [ed] = 0; numbs [0] = 1; while (F ONT! = 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 ;}}int maxflow () {rev_bfs (); int U, 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 Path int augf Low = 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 () {While (~ Scanf ("% d", & N, & M) {Init (); printf ("% d \ n", maxflow () ;}return 0 ;}


Poj 3469 dual core CPU (minimum cut)

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.