码迷,mamicode.com
首页 > 其他好文 > 详细

CDOJ 1220 The Battle of Guandu

时间:2015-10-29 23:12:52      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

The Battle of Guandu

Time Limit: 6000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

In the year of 200, two generals whose names are Cao Cao and Shao Yuan are fighting in Guandu. The battle of Guandu was a great battle and the two 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 strengthen 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 to 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 have to pay ci units of money for the village. There was 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 was considering how to beat Shao Yuan. As we can image, the battlefields would have different level of importance wi. Some of the battlefields with wi=2 were very important, so Cao Cao had to guarantee that in these battlefields, the number of his 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 was greater or equal to Shao Yuan‘s. The other battlefields with wi=0 had no importance, so there were no restriction about the number of warriors in those battlefields. Now, given such conditions, could you help 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 begins with two integers N and M(1≤N,M≤105) in 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 be 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 that Cao Cao had to pay for all the warriors to win the battle. If he couldn‘t win, y=−1.

Sample Input

2
2 3
2 3
1 1
1 1
0 1 2
1 1
1
1
1
2

Sample Output
Case #1: 1
Case #2: -1

解题:由于从i村庄给xi买人会导致yi战场上的敌人增加,由于胜负取决于人数,敌人增多,等同于yi战场的敌人数不变,caocao同学在yi战场上的人数减少。
所以可以这样子认为,我们从yi战场调来了人增援xi战场。但是,只能从不重要的0属性战场调来增援,因为这些战场胜负无关紧要,我们要保证能够胜利,所以以这些属性为0的战场为源点,求到必胜战场的最短路的和即可

下面是Q神的解释,非常清晰合理,完美啊
考虑每个战场的净人数(己方人数-对方人数),那么相当于第i个村庄花费c[i]的代价使得y[i]战场净人数-1,x[i]战场净人数+1,相当于转移了1个人过来。建立如下费用流模型,源向重要度为0的战场连容量INF费用0的弧,重要度为2的战场向汇连容量1费用0的弧,对于第i个村庄,战场y[i]向x[i]连容量INF费用c[i]的弧。如果满流,说明每个重要度为2的战场净人数>0,并且每个重要度为1的战场由于出入流平衡,净人数=0,于是能获胜。但是直接跑费用流是会TLE的,考虑每一次增广都是找一条从源到汇最短路,并且每次增广流量限制总为1,连向汇的费用总为0,因此可以从源出发跑一次单源最短路得到每次增广的费用,复杂度O((n+m)log(n+m))。
技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL INF = ~0ULL>>2;
 5 const int maxn = 100010;
 6 int x[maxn],y[maxn],c[maxn],z[maxn];
 7 struct arc{
 8     int to,next;
 9     LL w;
10     arc(int x = 0,LL y = 0,int z = -1){
11         to = x;
12         w = y;
13         next = z;
14     }
15 }e[1000010];
16 int head[maxn],tot;
17 LL d[maxn];
18 void add(int u,int v,LL w){
19     e[tot] = arc(v,w,head[u]);
20     head[u] = tot++;
21 }
22 queue<int>q;
23 bool in[maxn];
24 int main(){
25     int kase,N,M,cs = 1;
26     scanf("%d",&kase);
27     while(kase--){
28         scanf("%d%d",&N,&M);
29         for(int i = 1; i <= N; ++i)
30             scanf("%d",x + i);
31         for(int i = 1; i <= N; ++i)
32             scanf("%d",y + i);
33         for(int i = 1; i <= N; ++i)
34             scanf("%d",c + i);
35         for(int i = 1; i <= M; ++i)
36             scanf("%d",z + i);
37         memset(head,-1,sizeof head);
38         memset(in,false,sizeof in);
39         while(!q.empty()) q.pop();
40         tot = 0;
41         for(int i = 1; i <= N; ++i)
42             if(z[x[i]]) add(y[i],x[i],c[i]);
43         for(int i = 1; i <= M; ++i){
44             if(!z[i]){
45                 d[i] = 0;
46                 q.push(i);
47                 in[i] = true;
48             }else d[i] = INF;
49         }
50         while(!q.empty()){
51             int u = q.front();
52             q.pop();
53             in[u] = false;
54             for(int i = head[u]; ~i; i = e[i].next){
55                 if(d[e[i].to] > d[u] + e[i].w){
56                     d[e[i].to] = d[u] + e[i].w;
57                     if(!in[e[i].to]){
58                         in[e[i].to] = true;
59                         q.push(e[i].to);
60                     }
61                 }
62             }
63         }
64         LL ret = 0;
65         bool flag = true;
66         for(int i = 1; i <= M && flag; ++i){
67             if(z[i] == 2){
68                 if(d[i] == INF) flag = false;
69                 else ret += d[i];
70             }
71         }
72         printf("Case #%d: %lld\n",cs++,flag?ret:-1LL);
73     }
74     return 0;
75 }
View Code

 

CDOJ 1220 The Battle of Guandu

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4921972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!