The Battle of GuanduTime limit:6000/3000ms (java/others) Memory limit:65535/65535kb (java/others)
In the year of generals, the whose names is Cao Cao and Shao Yuan is fighting in Guandu. The Battle of Guandu was a great battle and the both armies were fighting at M different battlefields whose numbers were 1 To M. There were also n villages nearby numbered from 1 to N. Cao Cao could train some warriors from those villages to Strengthe N his military. For village I, Cao Cao could only call for some number of warriors join the Battlefield XI. However, Shao Yuan ' s power was extremely strong at that time. So-in order-protect themselves, village I would also send equal number of warriors to Battlefield Yi and join the Yuan Shao ' s Army. If Cao Cao had called for one warrior from village I, he would has to pay CI units of money for the village. There is no need for Cao Cao to pay for the Warriors who would join Shao Yuan ' s army. At the beginning, there were no warriors of both sides in every battlefield.
As one of greatest strategist at that time, Cao Cao is considering how to beat Shao Yuan. As we can image, the battlefields would has different level of importance wi. Some of the battlefields with wi=2 were very important, so Cao Cao had to guarantee this in these battlefields, the number Of He warriors was greater than Shao Yuan ' s. And some of the battlefields with Wi=1 were not as important as before, so Cao Cao had to make sure that the number of his Warriors is greater or equal to Shao Yuan ' s. The other battlefields and Wi=0 had no importance, so there were no restriction about the number of warriors in those bat Tlefields. Now, given such conditions, could do Cao Cao find the least number of money he had to pay to win the battlefield?
Input
The first line of the input gives the number of test cases, T (1≤t≤30). T test Cases follow.
Each test case is begins with the integers N and M (1≤n,m≤105) in the one line.
The second line contains N integers separated by blanks. The ith integer XI (1≤xi≤m) means Cao Cao could call for warriors from village I to Battlefield XI.
The third line also contains N integers separated by blanks. The ith integer yi (1≤yi≤m) means if Cao Cao called some number of warriors from village I, there would is the same number of warriors join Shao Yuan ' s Army and fight in Battlefield Yi.
The next line contains N integers separated by blanks. The ith integer ci (0≤ci≤105) means the number of money Cao Cao had to pay for each warrior from this village.
The last line contains M integers separated by blanks. The ith number WI (wi∈{0,1,2}) means the importance level of ith battlefield.
Output
For each test case, output one line containing case #x: Y, where x is the test Case number (starting from 1) and Y is the Least amount of money this Cao Cao had to pay for all the Warriors to win the battle. If He couldn ' t win, y=−1.
Sample Input
22 32 31 11 10 1 21 11112
Sample Output
Case #1:1Case #2:1
Problem solving: Because from the I village to Xi buy people will cause Yi battlefield enemies increase, because the outcome depends on the number of enemies, the enemy increased, equivalent to Yi Battlefield enemy number unchanged, Caocao students in Yi Battlefield number decreased.
So we can think of this, we transferred from Yi Battlefield people to reinforce the XI battlefield. However, only the 0 attributes of the battlefield from the important field of reinforcement, because these battlefield victory is irrelevant, we have to ensure that we can win, so with these attributes of 0 of the battlefield as the source point, to win the battlefield of the shortest possible and can
Here is the explanation of Q God, very clear and reasonable, perfect.
Taking into account the net number of each battlefield (number of persons-the number of people), then the cost of c[i] is equivalent to that of the first village Y[i] The net number of Battlefield -1,x[i] +1, equivalent to the transfer of 1 people come over. Set up the following cost flow model, the source to the importance of 0 of the battlefield with the capacity of the INF cost 0 arc, an importance of 2 of the battlefield to the sink capacity 1 cost 0 of the arc, for the first village, Battlefield Y[i] to X[i] The arc of the capacity inf cost C[i]. If full flow, the >0 of each battlefield net number of importance is 2, and each battlefield with an important degree of 1 due to the flow balance, net number = 0, so can win. But the direct running cost flow is the tle, consider each augmentation is to find a source to the shortest path, and each augmented traffic limit is always 1, even the cost of a total of 0, so you can run from the source one time single source the shortest time to get the cost of each augmentation, Complexity O ((n+m) log (n+m)).
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 ConstLL INF = ~0ull>>2;5 Const intMAXN =100010;6 intX[MAXN],Y[MAXN],C[MAXN],Z[MAXN];7 structarc{8 intTo,next;9 LL W;TenArcintx =0, LL y =0,intz =-1){ Oneto =x; AW =y; -Next =Z; - } the}e[1000010]; - intHead[maxn],tot; - LL D[MAXN]; - voidAddintUintv,ll W) { +E[tot] =arc (V,w,head[u]); -Head[u] = tot++; + } Aqueue<int>Q; at BOOL inch[MAXN]; - intMain () { - intKase,n,m,cs =1; -scanf"%d",&Kase); - while(kase--){ -scanf"%d%d",&n,&M); in for(inti =1; I <= N; ++i) -scanf"%d", X +i); to for(inti =1; I <= N; ++i) +scanf"%d", Y +i); - for(inti =1; I <= N; ++i) thescanf"%d", C +i); * for(inti =1; I <= M; ++i) $scanf"%d", z +i);Panax Notoginsengmemset (head,-1,sizeofhead); -Memsetinch,false,sizeof inch); the while(!q.empty ()) Q.pop (); +tot =0; A for(inti =1; I <= N; ++i) the if(Z[x[i]]) Add (Y[i],x[i],c[i]); + for(inti =1; I <= M; ++i) { - if(!Z[i]) { $D[i] =0; $ Q.push (i); - inch[I] =true; -}ElseD[i] =INF; the } - while(!Q.empty ()) {Wuyi intU =Q.front (); the Q.pop (); - inch[U] =false; Wu for(inti = Head[u]; ~i; i =E[i].next) { - if(D[e[i].to] > D[u] +E[I].W) { AboutD[e[i].to] = D[u] +E[I].W; $ if(!inch[e[i].to]) { - inch[E[i].to] =true; - Q.push (e[i].to); - } A } + } the } -LL ret =0; $ BOOLFlag =true; the for(inti =1; I <= M && flag; ++i) { the if(Z[i] = =2){ the if(D[i] = = INF) flag =false; the ElseRET + =D[i]; - } in } theprintf"Case #%d:%lld\n", cs++,flag?ret:-1LL); the } About return 0; the}
View Code
Cdoj 1220 the Battle of Guandu