UVA-10480 sabotage min Cut, output cut method

Source: Internet
Author: User

UVA-10480 Sabotage

Test instructions: Now there are n cities, M-Road, now to divide the entire picture into 2 parts, the number of cities divided into part, the opening of each road will need to spend, now ask to achieve the goal of the minimum cost to separate those roads.

Solution: Build the map directly to your diagram, and then run the maximum flow, we know the minimum cut is how much, the answer is the smallest cut .

The output cutting method is now required. We go forward from S, if there is a flow on the side, we will continue to walk on this side, knowing that we can no longer go and dye all the points we have experienced. Finally see all sides, is not a one is dyed, the other end is not stained, if it is, this edge is cut edge, the output is good.


1#include <bits/stdc++.h>2 using namespacestd;3 #defineFopen freopen ("_in.txt", "R", stdin); Freopen ("_out.txt", "w", stdout);4 #defineLL Long Long5 #defineULL unsigned LL6 #defineFi first7 #defineSe Second8 #definePB Emplace_back9 #defineLson l,m,rt<<1Ten #defineRson m+1,r,rt<<1|1 One #defineLCH (x) tr[x].son[0] A #defineRCH (x) tr[x].son[1] - #defineMax3 (A,B,C) max (A,max (b,c)) - #defineMin3 (a,b,c) min (a,min (b,c)) thetypedef pair<int,int>PLL; - Const intINF =0x3f3f3f3f; - ConstLL INF =0x3f3f3f3f3f3f3f3f; - ConstLL mod = (int) 1e9+7; + Const intN = $; - Const intM = nN; + intHead[n], deep[n], cur[n]; A intW[m], to[m], nx[m]; at inttot; - voidAddintUintVintval) { -W[tot] = val; To[tot] =v; -Nx[tot] = Head[u]; Head[u] = tot++; -  -W[tot] =0; To[tot] =u; inNx[tot] = Head[v]; HEAD[V] = tot++; - } to intBFsintSintt) { +queue<int>Q; -memset (Deep,0,sizeof(deep)); the Q.push (s); *Deep[s] =1; $      while(!Q.empty ()) {Panax Notoginseng         intU =Q.front (); - Q.pop (); the          for(inti = Head[u]; ~i; i =Nx[i]) { +             if(W[i] >0&& Deep[to[i]] = =0){ ADeep[to[i]] = Deep[u] +1; the Q.push (To[i]); +             } -         } $     } $     returnDeep[t] >0; - } - intDfs (intUintTintflow) { the     if(U = = t)returnflow; -      for(int&i = Cur[u]; ~i; i =Nx[i]) {Wuyi         if(deep[u]+1= = Deep[to[i]] && W[i] >0){ the             intDI =Dfs (To[i], T, Min (w[i], flow)); -             if(Di >0){ WuW[i]-= di, w[i^1] +=di; -                 returndi; About             } $         } -     } -     return 0; - } A  + intN, M; the intDinic (intSintt) { -     intAns =0, TMP; $      while(BFS (S, t)) { the          for(inti =1; I <= N; i++) Cur[i] =Head[i]; the          while(TMP = DFS (S, T, INF)) ans + =tmp; the     } the     returnans; - } in intVis[n]; the inttop; the intans[m][2]; About voidinit () { theMemset (Head,-1,sizeof(head)); thememset (Vis,0,sizeof(Vis)); thetop = tot =0; + } - voidSolveintu) { theVis[u] =1;Bayi      for(inti = Head[u]; ~i; i =Nx[i]) { the         if(Vis[to[i]])Continue; the         if(W[i]) { - solve (to[i]); -         } the     } the } the intMain () { the      while(~SCANF ("%d%d", &n, &m) && n +m) { -         intu, V, W; the init (); the          for(inti =1; I <= m; i++){ thescanf"%d%d%d", &u, &v, &W);94 Add (U, V, W); the Add (V, u, W); the         } theDinic (1,2);98Solve1); About          for(inti =1; I <= N; i++){ -              for(intj = Head[i]; ~j; j =Nx[j]) {101                 if(! (j&1)){102v =To[j];103                     if(Vis[i]! = Vis[v] && i <v)104printf"%d%d\n", I, v); the                 }106             }107         }108Puts"");109     } the     return 0;111}
View Code

UVA-10480 sabotage min Cut, output cut method

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.