Codeforces Round #304 (Div. 2) E. Soldier and traveling max Stream dinic EK algorithm

Source: Internet
Author: User

E. Soldier and Travelingtime limit per test1 secondmemory limit per test256 Megabytesinputstandard Inputoutputstandard out Put

In the country there isNCities andmBidirectional roads between them. All city have an army. Army of theI-th City consists of ai Soldiers. Now soldiers roam. After roaming each soldier have to either stay in he city or to go to the one of neighboring cities by atmoving along at the most one road.

Check if is it possible this after roaming there'll be exactly bi soldiers in the I- Th city.

Input

First line of input consists of integers n and m (1?≤? N. ≤?100, 0?≤? M.≤?200).

Next Line containsNIntegers a1,? a 2,?...,? a N (0?≤? a i? ≤?100).

Next Line containsNIntegers b1,? b 2,?...,? b N (0?≤? b i? ≤?100).

ThenmLines follow, each of them consists of the integersPandQ(1?≤? P,? q? ≤? N , P. ≠? Q ) denoting that there are an undirected road between citiesPandQ.

It is guaranteed, that there are at the most one road between each pair of cities.

Output

If the conditions can not is met output single word "NO".

Otherwise output word "YES"And thenNLines, each of them consisting ofNIntegers. Number in theI-th Line in theJ-th column should denote how many soldiers should road from cityITo CityJ(If i? ≠? J ) or how many soldiers should stayI(If i? =? J ).

If there is several possible answers you could output any of the them.

Sample Test (s) input
4 41 2 6 33 5 3 11 22 33 44 2
Output
Input
2 01 22 1
Output
NO
Test instructions, ask, give a diagram, each point has an initial value, requires a scheme to make each point can reach the target value. Each point of the soldier can only walk one city.
Network flow, first set up a model, the n points each point into points and out point two points, connected to the edge of A[i]
, build an S point, connect all the points, build an e point, and even all the out points. If the i,j has an edge, it is at the edge of the out point I and the into Point J even a a[i]. This will ensure that after running a maximum flow, if the maximum flow equals, the total number of people, it is explained that there is a solution, the output edge of the flow is to transfer the person. 
Because the edge of the point is a[i], guaranteed, out of the person up to a[i], because only from the point of entry to the point, so that the soldiers can only walk one side.
The code uses templates directly, and there are two implementations of the Dinic EK algorithm.
 
#define Inf9000000000#define EPS (double) 1e-9#define mod1000000007#define pi3.14159265358979//********************* /#endif # define N 405#define M 100005#define maxn 450#    Define MOD 1000000000000000007int n,a[n],b[n],s,e,sum,maxf,m,ans[n][n],s1;struct edge{int from,to,cap,flow;    Edge (int u,int v,int c,int f): From (U), to (v), Cap (c), Flow (f) {}};struct edmondskarp{int n,m;     Vector<edge> edges;//twice times vector<int> g[maxn];//adjacency table, figure int a[maxn];//starting point to I an improved amount int p[maxn];//shortest circuit into the arc number        void init (int n) {FI (n) g[i].clear ();    Edges.clear ();        } void Addedge (int from,int to,int cap) {Edges.push_back (Edge (from,to,cap,0));        Edges.push_back (Edge (to,from,0,0));//Reverse M = edges.size ();        G[from].push_back (m-2);    G[to].push_back (m-1);        } int Maxflow (int s,int t) {int flow = 0; for (;;)            {memset (a,0,sizeof (a));           Queue<int> Q; Q.push (s);            A[s] = INF; while (! Q.empty ()) {int x = Q.front ();                Q.pop ();                    FI (G[x].size ()) {Edge & e = edges[g[x][i]];                        if (!a[e.to]&&e.cap > E.flow) {p[e.to] = G[x][i];                        A[e.to] = min (a[x],e.cap-e.flow);                    Q.push (e.to);            }} if (A[t]) break;            } if (!a[t]) break;                for (int u = t;u!=s;u = edges[p[u]].from) {Edges[p[u]].flow + = a[t];            Edges[p[u] ^ 1].flow-= a[t];        } flow + = A[t];    } return flow;    }};struct dinic{int n,m,s,t;     Vector<edge> edges;//twice times vector<int> g[maxn];//adjacency table, figure bool VIS[MAXN];//BFS using int d[maxn];//the distance from beginning to I        int cur[maxn];//current arc subscript void init (int n) {FI (n) g[i].clear ();    Edges.clear (); } void Addedge (int from,int to,inT cap) {Edges.push_back (Edge (from,to,cap,0));        Edges.push_back (Edge (to,from,0,0));//Reverse M = edges.size ();        G[from].push_back (m-2);    G[to].push_back (m-1);        } bool BFS () {memset (vis,0,sizeof (VIS));        Queue<int> Q;        Q.push (s);        D[s] = 0;        Vis[s] = 1; while (! Q.empty ()) {int x = Q.front ();            Q.pop ();                for (int i=0;i<g[x].size (); i++) {Edge & e = edges[g[x][i]];                    if (!vis[e.to] && e.cap > E.flow) {vis[e.to] = 1;                    D[e.to] = d[x] + 1;                Q.push (e.to);    }}} return vis[t];        } int DFS (int x,int a) {if (x = = T | | a== 0) return A;        int flow = 0,f;            for (int i= cur[x];i<g[x].size (); i++) {Edge & e = edges[g[x][i]]; if (d[x] + 1 = = D[e.to] && (f= DFS (E.to,min (A,e.cap-e.flow))) >0) {E.flow + =F                Edges[g[x][i]^1].flow-= f;                Flow + + F;                A-= f;            if (a== 0) break;    }} return flow;        } int Maxflow (int s,int t) {this->s = s;this-> t = t;        int flow = 0;            while (BFS ()) {memset (cur,0,sizeof (cur));        Flow + = DFS (S,inf);    } return flow;        }};//edmondskarp ek;dinic Ek;int Main () {while (S2 (n,m)!=eof) {ek.init (n + n + 2); sum = 0;            FI (n) {S (a[i]); s1+=a[i];        Ek.addedge (n + n,i + i,a[i]);            } FI (n) {S (b[i]);            Ek.addedge (i + i + 1,n + n + 1,b[i]);            Ek.addedge (i + i,i + i + 1,a[i]);        Sum + = B[i];            } FI (M) {S2 (s,e); s--, e--;            Ek.addedge (s + s,e + e + 1,a[s]);        Ek.addedge (e + e,s + S + 1,a[e]);            } if (sum! = S1) {printf ("no\n");        Continue } MAXF = Ek.maxflow (n + n,n + N+ 1);            if (sum = = maxf) {printf ("yes\n");                FI (n) {int s = i + i;                    FJ (Ek.g[s].size ()) {Edge E = ek.edges[ek.g[s][j]];                    ANS[E.FROM/2][E.TO/2] = E.flow;                printf ("Flow%d%d%d\n", e.from,e.to,e.flow,e.cap); }} FI (n) {FJ (n) {if (j) printf ("%d", Ans[i]                    [j]);                else printf ("%d", ans[i][j]);            } printf ("\ n");        }} else {printf ("no\n"); }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #304 (Div. 2) E. Soldier and traveling max Stream dinic EK algorithm

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.