The maximum flow of HDU 4975 is similar to that of 4888, but the maximum flow is optimized.

Source: Internet
Author: User
// Optimization from Pan Shen # include <stdio. h> # include <string. h ># include <queue> using namespace STD; # define INF 0x3fffffff # define n 1100 struct node {int U, V, W, next ;} bian [N * n * 4]; int head [N], Yong, DIS [N], work [N]; void Init () {Yong = 0; memset (Head, -1, sizeof (head);} void addbian (int u, int V, int W) {Bian [Yong]. U = u; Bian [Yong]. V = V; Bian [Yong]. W = W; Bian [Yong]. next = head [u]; head [u] = Yong ++;} void add (int u, int V, int W) {addbian (U, V, W); Ad Dbian (v, U, 0);} int min (int A, int B) {return a <B? A: B;} int BFS (INT S, int t) {memset (DIS,-1, sizeof (DIS); queue <int> q; q. push (s); DIS [s] = 0; while (! Q. Empty () {int u = Q. Front (); q. Pop (); For (INT I = head [u]; I! =-1; I = Bian [I]. next) {int v = Bian [I]. v; If (Bian [I]. W & dis [v] =-1) {dis [v] = dis [u] + 1; q. push (V); If (V = T) return 1 ;}} return 0 ;}int DFS (int s, int limit, int T) {If (S = T) return limit; For (Int & I = work [s]; I! =-1; I = Bian [I]. next) {int v = Bian [I]. v; If (Bian [I]. W & dis [v] = dis [s] + 1) {int TT = DFS (v, min (limit, Bian [I]. w), T); If (TT) {Bian [I]. w-= tt; Bian [I ^ 1]. W + = tt; return TT ;}}return 0 ;}int dinic (int s, int t) {int ans = 0; while (BFS (S, T )) {memcpy (work, Head, sizeof (head); While (int tt = DFS (S, INF, t) ans + = tt;} return ans ;} int vis [N], K; int DFS (int u, int pre) {vis [u] = 1; for (Int & I = work [u]; I! =-1; I = Bian [I]. Next) {// optimized int v = Bian [I]. V; If (Bian [I]. W & V! = Pre) {If (vis [v]) return 1; if (DFS (v, u) return 1 ;}} vis [u] = 0; return 0 ;} int judge (int n) {int I; memset (VIS, 0, sizeof (VIS); memcpy (work, Head, sizeof (head )); // optimized for (I = 1; I <= N; I ++) {If (DFS (I,-1) return 1;} return 0 ;} int main () {int M, F, I, j, S, N, T, Suma, sumb, T, cou = 0; scanf ("% d ", & T); While (t --) {scanf ("% d", & N, & M); k = 9; S = 0; t = n + m + 1; Init (); Suma = 0; sumb = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & F); sum A + = f; add (S, I, f) ;}for (I = 1; I <= m; I ++) {scanf ("% d ", & F); sumb + = f; add (I + N, T, f) ;}for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) add (I, j + n, k); If (SUMA! = Sumb) {printf ("case # % d: so naive! \ N ", ++ cou); continue;} f = dinic (S, T); // printf (" % d \ n ", f); If (F! = SUMA) {printf ("case # % d: so naive! \ N ", ++ cou); continue;} If (Judge (N) printf (" case # % d: so young! \ N ", ++ cou); else printf (" case # % d: so simple! \ N ", ++ cou);} return 0 ;}

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.