[Floyd + split point + second answer + network stream] poj 2391

Source: Internet
Author: User

# Include <map> # include <set> # include <list> # include <queue> # include <deque> # include <stack> # include <string> # include <cstdio> # include <math. h> # include <iomanip> # include <cstdlib> # include <limits. h> # include <string. h> # include <iostream> # include <fstream> # include <algorithm> using namespace STD; # define ll long # define PII pair <int, int> # define bug cout <"here !! "<Endl # define PI ACOs (-1.0) # define fre freopen (" input.txt "," r ", stdin) # define FF freopen (" output.txt "," W ", stdout) # define EPS 1e-8 # define min int_minconst ll max = (1ll <58); const int INF = (1 <29 ); # define n 410 # define M (N * n) int n, m; // n indicates the number of points. M indicates the number of edges. Int H [N]; int gap [N]; int P [N], ecnt; int source, sink; struct edge {int V; int next; int val;} e [m]; inline void Init () {memset (p,-1, sizeof (p); ecnt = 0;} // directed inline Vo Id insert1 (int from, int to, int Val) {e [ecnt]. V = to; E [ecnt]. val = val; E [ecnt]. next = P [from]; P [from] = ecnt ++; swap (from, to); e [ecnt]. V = to; E [ecnt]. val = 0; E [ecnt]. next = P [from]; P [from] = ecnt ++;} // undirected inline void insert2 (int from, int to, int Val) {e [ecnt]. V = to; E [ecnt]. val = val; E [ecnt]. next = P [from]; P [from] = ecnt ++; swap (from, to); e [ecnt]. V = to; E [ecnt]. val = val; E [ecnt]. next = P [from]; P [from] = ecnt ++;} inline in T DFS (INT POs, int cost) {If (Pos = sink) {return cost;} Int J, Minh = n-1, Lv = Cost, D; for (j = P [POS]; J! =-1; j = E [J]. next) {int v = E [J]. v, val = E [J]. val; If (Val> 0) {If (H [v] + 1 = H [POS]) {If (LV <E [J]. val) d = lv; else d = E [J]. val; D = DFS (v, d); e [J]. val-= D; E [J ^ 1]. val + = D; LV-= D; If (H [Source]> = N) return cost-lv; If (Lv = 0) break ;} if (H [v] <Minh) Minh = H [v] ;}} if (Lv = cost) {-- gap [H [POS]; if (gap [H [POS] = 0) H [Source] = N; H [POS] = Minh + 1; ++ gap [H [POS];} return cost-lv;} int SAP (INT St, int ed) {source = sT; sink = ed; Int ans = 0; memset (GAP, 0, sizeof (GAP); memset (H, 0, sizeof (h); Gap [st] = N; while (H [st] <n) {ans + = DFS (St, int_max);} return ans;} ll G [205] [205]; int flow; int X [205], Y [205]; void Floyd (int nn) {int I, j, k; For (k = 1; k <= nn; k ++) {for (I = 1; I <= nn; I ++) {If (G [I] [k] <max) for (j = 1; j <= nn; j ++) {If (G [k] [J] <max) if (G [I] [k] + G [k] [J] <G [I] [J]) {G [I] [J] = G [I] [k] + G [k] [J] ;}}} bool chk (LL mid, int NN) {int I, J; Init (); for (I = 1; I <= nn; I ++) {insert1 (I, I + NN, INF); insert1 (0, I, X [I]); insert1 (I + NN, n-1, Y [I]);} for (I = 1; I <nn; I ++) {for (j = I + 1; j <= nn; j ++) {If (G [I] [J] <= mid) {insert1 (I, J + NN, INF); insert1 (J, I + NN, INF) ;}}int ans = SAP (0, n-1); Return ans = flow ;} ll l, R, mid; void Gao (int nn) {L = 0; ll ans =-1; while (L <= r) {mid = (L + r)> 1; if (chk (MID, NN) {r = mid-1; ans = mid ;} else l = Mid + 1;} If (ANS =-1) puts ("- 1 "); else printf (" % i64d \ n ", ANS);} int main () {fre; while (scanf (" % d ", & N, & M )! =-1) {int I, j; for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {G [I] [J] = max;} G [I] [I] = 0;} int nn = N; n = 2 * n + 2; flow = 0; r = 0; for (I = 1; I <= nn; I ++) {scanf ("% d", & X [I], & Y [I]); flow + = x [I];} while (M --) {int A, B; ll C; scanf ("% d % i64d", & A, & B, & C); If (G [a] [B]> C) {G [a] [B] = G [B] [a] = C; R + = C ;}} Floyd (NN); Gao (NN );} 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.