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))。
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = ~0ULL>>;
const int maxn = ;
int x[maxn],y[maxn],c[maxn],z[maxn];
struct arc{
int to,next;
LL w;
arc(int x = ,LL y = ,int z = -){
to = x;
w = y;
next = z;
}
}e[];
int head[maxn],tot;
LL d[maxn];
void add(int u,int v,LL w){
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
queue<int>q;
bool in[maxn];
int main(){
int kase,N,M,cs = ;
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&N,&M);
for(int i = ; i <= N; ++i)
scanf("%d",x + i);
for(int i = ; i <= N; ++i)
scanf("%d",y + i);
for(int i = ; i <= N; ++i)
scanf("%d",c + i);
for(int i = ; i <= M; ++i)
scanf("%d",z + i);
memset(head,-,sizeof head);
memset(in,false,sizeof in);
while(!q.empty()) q.pop();
tot = ;
for(int i = ; i <= N; ++i)
if(z[x[i]]) add(y[i],x[i],c[i]);
for(int i = ; i <= M; ++i){
if(!z[i]){
d[i] = ;
q.push(i);
in[i] = true;
}else d[i] = INF;
}
while(!q.empty()){
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] > d[u] + e[i].w){
d[e[i].to] = d[u] + e[i].w;
if(!in[e[i].to]){
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
LL ret = ;
bool flag = true;
for(int i = ; i <= M && flag; ++i){
if(z[i] == ){
if(d[i] == INF) flag = false;
else ret += d[i];
}
}
printf("Case #%d: %lld\n",cs++,flag?ret:-1LL);
}
return ;
}

CDOJ 1220 The Battle of Guandu的更多相关文章

  1. CDOJ UESTC 1220 The Battle of Guandu

    The 2015 China Collegiate Programming Contest 2015第一届中国大学生程序设计竞赛 F题 本质就是求单源最短路!注意会爆int 对于每一个村庄i,其实就是 ...

  2. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  3. CDOJ 1217 The Battle of Chibi

    The Battle of Chibi Time Limit: 6000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  4. cdoj 树上战争(Battle on the tree) Label:并查集?

    给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜. Input 输入包含多组数据 每组第 ...

  5. hdu 5545 The Battle of Guandu spfa最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...

  6. CDOJ 889 Battle for Silver

    Battle for Silver Time Limit: 2999/999MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others) ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. Codeforces 738D. Sea Battle 模拟

    D. Sea Battle time limit per test: 1 second memory limit per test :256 megabytes input: standard inp ...

  9. 1220 - Mysterious Bacteria--LightOj1220 (gcd)

    http://lightoj.com/volume_showproblem.php?problem=1220 题目大意: 给你一个x,求出满足 x=b^p, p最大是几. 分析:x=p1^a1*p2^ ...

随机推荐

  1. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  2. LN : leetcode 416 Partition Equal Subset Sum

    lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...

  3. T4312 最大出栈顺序

    题目描述 给你一个栈和n个数,按照n个数的顺序入栈,你可以选择在任何时候将数 出栈,使得出栈的序列的字典序最大. 输入输出格式 输入格式: 输入共2行. 第一行个整数n,表示入栈序列长度. 第二行包含 ...

  4. ES6学习笔记(4)----正则的扩展

    参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 正则的扩展 ES6新增的正则表达式修饰符 u修饰符a.能够更准确地匹配unicode大于\uFF ...

  5. Java集合类工具CollectionUtils的操作方法

    集合判断: 例1: 判断集合是否为空:CollectionUtils.isEmpty(null): trueCollectionUtils.isEmpty(new ArrayList()): true ...

  6. Proteus与Keil连接及其仿真(有例子哦!)

    记录一下Proteus仿真的一些设置和使用,以方便自己以后复习和大家交流!如有错误,希望大家指正. 1.Proteus软件的安装,这里就不作说明了.

  7. 用JS获取Html中所有图片文件流然后替换原有链接

    function displayHtmlWithImageStream(bodyHtml) { var imgReg = /<img.*?(?:>|\/>)/gi; var arr ...

  8. Servlet 3.0 新特性详解 (转载)

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性概述 Servlet 3.0 作为 Jav ...

  9. ABAP和XML数据格式互相转换的两种方式

    ABAP和XML数据格式互相转换是广大开发人员经常遇到的需求.本文介绍两种方式. 1. ABAP提供了一个工具类cl_proxy_xml_transform,通过它的两个方法abap_to_xml_x ...

  10. JavaFX Chart设置数值显示

    一.XYChart import javafx.application.Application;import javafx.geometry.NodeOrientation;import javafx ...