Poj 2391 ombrophobic bovines [simple network stream]

Source: Internet
Author: User

... The first question about network flow

Create a chart with a template

# Include <cstdio> # include <cstdlib> # include <iostream> # include <algorithm> # include <cstring> # include <cmath> # include <vector> using namespace STD; long long F, P; long cowsum; const int max = 555; long tttt = (1 <31)-1; const long INF = tttt * tttt; long long dis [Max] [Max]; long cownum [Max]; long sheld [Max]; // sap maximum flow template const long maxn = 1111; // maximum number of points const long maxm = 222222; // maximum number of edges struc T node {long from, to, next; long cap;} edge [maxm]; long tol; long head [maxn]; long Dep [maxn]; long long gap [maxn]; // gap [x] = Y: indicates that the number of Dep [I] = X in the residual network is ylong long N; // n is the total number of points, including the source and sink void Init () {Tol = 0; memset (Head,-1, sizeof (head ));} void addedge (Long U, long V, long w) {edge [tol]. from = u; edge [tol]. to = V; edge [tol]. CAP = W; edge [tol]. next = head [u]; head [u] = tol ++; edge [tol]. fro M = V; edge [tol]. to = u; edge [tol]. CAP = 0; edge [tol]. next = head [v]; head [v] = tol ++;} void BFS (long start, long end) {memset (DEP,-1, sizeof (DEP); memset (GAP, 0, sizeof (GAP); Gap [0] = 1; long que [maxn]; long front, rear; front = rear = 0; Dep [end] = 0; que [rear ++] = end; while (front! = Rear) {long u = que [Front ++]; If (front = maxn) Front = 0; For (long I = head [u]; I! =-1; I = edge [I]. Next) {long v = edge [I]. To; If (DEP [v]! =-1) continue; que [rear ++] = V; If (Rear = maxn) rear = 0; Dep [v] = Dep [u] + 1; ++ gap [Dep [v] ;}} long SAP (long start, long end) {long res = 0; BFS (START, end ); long long cur [maxn]; long s [maxn]; long top = 0; memcpy (cur, Head, sizeof (head); long u = start; long long I; while (DEP [start] <n) {If (u = END) {long temp = inf; long inser; for (I = 0; I <top; I ++) if (temp> edge [s [I]. CAP) {Temp = edge [s [I]. CAP; inser = I ;}for (I = 0; I <top; I ++) {edge [s [I]. cap-= temp; edge [s [I] ^ 1]. cap + = temp;} res + = temp; Top = inser; u = edge [s [Top]. from;} If (u! = End & gap [Dep [u]-1] = 0) // when a fault occurs, the break is not added. For (I = cur [u]; I! =-1; I = edge [I]. Next) if (edge [I]. Cap! = 0 & Dep [u] = Dep [edge [I]. To] + 1) break; if (I! =-1) {cur [u] = I; s [top ++] = I; u = edge [I]. to;} else {long min = N; for (I = head [u]; I! =-1; I = edge [I]. next) {If (edge [I]. CAP = 0) continue; If (min> Dep [edge [I]. to]) {min = Dep [edge [I]. to]; cur [u] = I ;}-- gap [Dep [u]; Dep [u] = min + 1; ++ gap [Dep [u]; if (u! = Start) u = edge [s [-- top]. from ;}} return res;} void input () {scanf ("% LLD", & F, & P); n = f * 2 + 2; for (long I = 1; I <= f; I ++) {for (long j = 1; j <= f; j ++) {dis [I] [J] = inf;} dis [I] [I] = 0;} For (long I = 1; I <= f; I ++) {long a, B; scanf ("% LLD", & A, & B); cownum [I] = A; sheld [I] = B; cowsum + = A;} For (long I = 1; I <= P; I ++) {long U, V, Len; scanf ("% LLD", & U, & V, & Len); If (DIS [u] [v]> Len) {dis [v] [u] = dis [u] [v] = Len ;}} void Floyd () {for (long k = 1; k <= F; k ++) {for (long I = 1; I <= f; I ++) {for (long j = 1; j <= f; j ++) {If (DIS [I] [J]> dis [I] [k] + dis [k] [J]) dis [I] [J] = dis [I] [k] + dis [k] [J] ;}}} void makemap (long maxedge) {for (long I = 1; I <= f; I ++) {for (long j = 1; j <= f; j ++) {If (DIS [I] [J] <= maxedge) {addedge (I, j + F, INF) ;}}for (long I = 1; I <= f; I ++) {addedge (0, I, cownum [I]);} For (long I = 1; I <= f; I ++) {addedge (I + F, 2 * F + 1, sheld [I]) ;}} int main () {# ifndef online_judgefreopen ("G:/1.txt"," r ", stdin); freopen ("G:/2.txt"," W ", stdout); # endifinput (); Floyd (); long ans; long l = 0, r = 0, mid = 0; For (long I = 1; I <= f; I ++) {for (long j = 1; j <= F; j ++) {If (DIS [I] [J] <inf) r = max (R, DIS [I] [J]) ;}} R + = 10; mid = (L + r)> 1; long ans =-1; while (L <r) {Init (); makemap (MID ); ans = SAP (* F + 1); If (ANS> = cowsum) {ans = r = mid; Mid = (L + r)> 1 ;} else {L = Mid + 1; Mid = (L + r)> 1 ;}} printf ("% LLD \ n", ANS );}


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.