Codeforces 546E. Soldier and Traveling network streams

Source: Internet
Author: User

Topic links

Given n cities, and the number of people at the beginning of each city and the number of targets. Initially, some cities were connected. Everyone in the city can only stay in their own city or go to the city adjacent to him, adjacent to the equivalent of walking only one road.

If the target state is unreachable, output no, otherwise the output of each city is how people go, such as the first city has 2 people walked to the second city, 1 people left in the first city, then the output of the first row of two numbers is 1, 2.

Obviously the network flow, output there wrote for a long time ...

First of all to determine whether can reach, if the initial state of the number of people and the target State is not the same, and secondly, if the completion of the graph run network flow results with all the target city number and different numbers, also cannot reach.

To build a map, the city will be split into two points, (U, U ') intermediate edge inf, indicating that you can go casually, the source and u connected, the weight of the initial state of the number of people, U ' and meeting point connected, the number of people in the target State. If there is a road connection between two cities, then add edge (U, V '), (V, U '), and the value is INF.

Each city is how people walk, should look at the flow of the opposite side, if the Edge (V ', u) of the weight of X, then ans[u][v] = x.

See the code specifically.

1#include <bits/stdc++.h>2 using namespacestd;3 #definePB (x) push_back (x)4 #definell Long Long5 #defineMK (x, y) make_pair (x, y)6 #defineLson L, M, rt<<17 #defineMem (a) memset (a, 0, sizeof (a))8 #defineRson m+1, R, rt<<1|19 #defineMem1 (a) memset (a,-1, sizeof (a))Ten #defineMEM2 (a) memset (a, 0x3f, sizeof (a)) One #defineRep (I, A, n) for (int i = A; i<n; i++) A #defineull unsigned long Long -typedef pair<int,int>PLL; - Const DoublePI = ACOs (-1.0); the Const DoubleEPS = 1e-8; - Const intMoD = 1e9+7; - Const intINF =1061109567; - Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} }; + Const intMAXN =2e5; - intq[maxn*2], head[maxn*2], dis[maxn/Ten], S, T, NUM, vis[ the], val[205][205]; + structnode A { at     intto, Nextt, C; - node () {} -NodeintTo,intNextt,intc): To, Nextt (NEXTT), C (c) {} -}e[maxn*2]; - voidinit () { -num =0; in mem1 (head); - } to voidAddintUintVintc) { +E[num] = node (V, Head[u], c); Head[u] = num++; -E[num] = node (u, Head[v],0); HEAD[V] = num++; the } * intBFs () { $ mem (dis);Panax NotoginsengDis[s] =1; -     intSt =0, ed =0; theq[ed++] =s; +      while(st<ed) { A         intU = q[st++]; the          for(inti = Head[u]; ~i; i =e[i].nextt) { +             intv =e[i].to; -             if(!dis[v]&&e[i].c) { $DIS[V] = dis[u]+1; $                 if(v = =t) -                     return 1; -q[ed++] =v; the             } -         }Wuyi     } the     return 0; - } Wu intDfsintUintlimit) { -     if(U = =t) { About         returnlimit; $     } -     intCost =0; -      for(inti = Head[u]; ~i; i =e[i].nextt) { -         intv =e[i].to; A         if(E[i].c&&dis[v] = = dis[u]+1) { +             intTMP = DFS (V, min (limit-Cost , e[i].c)); the             if(tmp>0) { -E[I].C-=tmp; $e[i^1].C + =tmp; theCost + =tmp; the                 if(Cost = =limit) the                      Break; the}Else { -DIS[V] =-1; in             } the         } the     } About     returnCost ; the } the intDinic () { the     intAns =0; +      while(BFS ()) { -Ans + =DFS (s, INF); the     }Bayi     returnans; the } the intMain () - { -     intN, m, x, y, sum =0, TMP =0; theCin>>n>>m; the init (); thes =0, t = N2+1; the      for(inti =1; i<=n; i++) { -scanf"%d", &x); the Add (S, I, x); theTMP + =x; the     }94      for(inti =1; i<=n; i++) { thescanf"%d", &x); theAdd (i+N, t, x); theSum + =x;98Add (i, i+N, INF); About     } -      while(m--) {101scanf"%d%d", &x, &y);102Add (x, y+N, INF);103Add (Y, x+N, INF);104     } the     intAns =dinic ();106     if(ans! = sum| | sum!=tmp) {107cout<<"NO"<<Endl;108         return 0;109     } thecout<<"YES"<<Endl;111      for(intU =1; u<=n; u++) { the          for(inti = Head[u]; ~i; i =e[i].nextt) {113             intv =e[i].to; the             if(v>N) { theVal[u][v-n] = e[i^1].C;//Reverse Edge Flow the             }117         }118     }119      for(inti =1; i<=n; i++) { -          for(intj =1; j<=n; J + +) {121cout<<val[i][j]<<" ";122         }123cout<<Endl;124     } the     return 0;126}

Codeforces 546E. Soldier and Traveling network streams

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.