hdu 5545 The Battle of Guandu 最短路径
来源:互联网 发布:linux 解压tar.gz 编辑:程序博客网 时间:2024/06/07 07:25
The Battle of Guandu
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 394 Accepted Submission(s): 179
Problem Description
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 importancewi . 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?
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
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 integersN and M(1≤N,M≤105) in one line.
The second line containsN 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 containsN 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 containsN 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 containsM integers separated by blanks. The ith number wi(wi∈0,1,2) means the importance level of ith battlefield.
Each test case begins with two integers
The second line contains
The third line also contains
The next line contains
The last line contains
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
22 32 31 11 10 1 21 11112
Sample Output
Case #1: 1Case #2: -1
题目大意:有n个村庄,m个战场。对于村庄i,曹操可以支付c[i]*num元,使得这个村庄派遣num个士兵去战场x[i]为曹操作战,同时,这个村庄也会派遣num个士兵去战场y[i]为袁绍作战。对于每个战场,有3种重要度(0,1,2)。重要度为2表示该战场曹操的士兵数必须大于袁绍的士兵数;重要度为1表示该战场曹操的士兵数必须不小于袁绍的士兵数;重要度为0则对两方士兵数无任何要求。求曹操若想赢得战争,至少要支付多少钱。
由贪心可知,在重要度为2的战场,曹操的士兵恰好比袁绍多一,在重要度为1的战场,两军士兵数相等,否则减去曹操士兵数可以使得花费更低。
对于一个村庄来说,花费w会向x战场增加曹兵,会向y战场增加袁兵,所以一个村庄其实就是一条边x->y,使得x的曹兵+1,是的y的曹兵-1,对于一个重要度为2的战场x,我们向这里增加一个曹兵,那么他对应的y战场曹兵会减一,如果y重要度为0,那么不用管了,如果为1或者2,那么还要在y中加曹兵,对应从y继续往其他点走,其实就是从x走到最近的重要度为0的点,对所有重要度为2的战场跑一遍最短路径,将最短的花费加起来就是答案,但是因为数据量很大,所以把边逆过来,这样只跑一次多源最短路径就好了。
#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;const int maxn = 1e5 + 10;const long long INF = 1e10;struct node { int u, v, w, next;}E[2 * maxn];int n, m;int x[maxn], y[maxn], c[maxn], level[maxn];int vis[maxn];int head[maxn], cnt;long long dist[maxn];void addedge(int u, int v, int w){ E[cnt].u = u; E[cnt].v = v; E[cnt].w = w; E[cnt].next = head[u]; head[u] = cnt++;}void spfa(){ queue<int> Q; Q.push(0); vis[0] = 1; while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for (int i = head[u]; i != -1; i = E[i].next) { int v = E[i].v; int w = E[i].w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; if (vis[v] == 0) { Q.push(v); vis[v] = 1; } } } }}int main(){ int T; int cas = 0; scanf("%d", &T); while (T--) { cas ++; memset(head, -1, sizeof(head)); memset(vis, 0, sizeof(vis)); cnt = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &x[i]); for (int i = 1; i <= n; i++) scanf("%d", &y[i]); for (int i = 1; i <= n; i++) scanf("%d", &c[i]); for (int i = 1; i <= m; i++) { scanf("%d", &level[i]); dist[i] = INF; } for (int i = 1; i <= n; i++) { addedge(y[i], x[i], c[i]); if (level[y[i]] == 0) addedge(0, y[i], 0); } dist[0] = 0; spfa(); //for (int i = 0; i <= m; i++) printf("|%d ", dist[i]); long long ans = 0; for (int i = 1; i <= m; i++) { if (level[i] == 2) { if (dist[i] == INF) { ans = INF; break; } else ans += dist[i]; } } if (ans == INF) printf("Case #%d: -1\n", cas); else printf("Case #%d: %lld\n", cas, ans); } return 0;}
阅读全文
0 0
- hdu 5545 The Battle of Guandu 最短路径
- HDU 5545 The Battle of Guandu(最短路)
- HDU 5545 The Battle of Guandu (最短路Dijkstra)
- HDU 5545 The Battle of Guandu(最短路)
- hdu 5545 The Battle of Guandu
- The Battle of Guandu
- HDU5545-The Battle of Guandu
- (CCPC F题)UESTC 1220 The Battle of Guandu (最短路)
- hdu5545The Battle of Guandu
- hdoj 5545 The Battle of Guandu 【差分约束系统 求解多源最短路】
- The Battle of Chibi HDU
- HDU3986(Harry Potter and the Final Battle)-枚举最短路径+SPFA
- find the longest of the shortest 最短路径 dijkstra
- hdu 最短路径
- hdu 最短路径
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- 【HDU5545 2015 CCPC 南阳国赛F】【差分约束思想 费用流思想】The Battle of Guandu 官渡之战 重要战场人数多 士兵流向转化问题为最短路spfa+dijkstra双写
- 【最短路径-Dis】hdu 2680 Choose the best route
- getElementById与getElementsByTagName的区别
- pycharm import cv2 : no module named cv2
- Xshell与Xftp连接虚拟机
- HDU
- Matrial Design(五) toolbar(1)
- hdu 5545 The Battle of Guandu 最短路径
- Servlet的生命周期
- 字典树Trie总结
- h5页面中虚化文本框默认内容,当输入文字后默认内容消失怎么做?
- hdoj 6050(2017 Multi-University Training Contest
- Spring_泛型依赖注入
- 74. Search a 2D Matrix
- hiredis windows x64编译记录
- 多渠道打包的多种方式实现(炒鸡简单)