The Battle of Guandu

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

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 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

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.

Output

For each test case, output
one line containing Case #x:, where x is the test case number (starting
from 1). Then output 4 lines with 4 characters each. indicate the
recovered board.

Sample Input

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

Sample Output

  1. Case #1: 1
    Case #2: -1

HINT

题意

在一场战争中,有m(<=100000)个战场和n(<=100000)个村 庄,每个战场有一个重要度,重要度为0表示在这个战场己方输赢无所谓,重要度为1表示己方不能输,重要度为2表示己方必须胜出,己方获得战争的最终胜利当 且仅当己方在每个战场的战果均不违背其重要度,每个战场输赢的判据只有人数,人多的一方胜出,若两方人数相同则打平,对于第i个村庄,每花费c[i]的代 价能够征用一个人派到己方x[i]战场,同时有一个人会跑到敌方y[i]战场,问己方能否获得战争的最终胜利,若能,求出最小代价。

题解:

建图跑spfa就好,如果你考虑每个战场我方人数-敌方人数的数量的话,整个战场的人数实际上是守恒的。你可以花费c[i]代价,使得x[i]加一个人,y[i]减一个人。那么我们就建边跑最短路就好啦,让不重要的战场当成源点,重要的战场当成你要求的最短路就好了

d[i]表示从不重要战场拉一个人到i战场需要的最小代价,只要拉一个人就会胜利

代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<math.h>
  4. #include<queue>
  5. #include<vector>
  6. using namespace std;
  7. #define maxn 100005
  8. long long inf = 999999999999999LL;
  9. int x[maxn];
  10. int y[maxn];
  11. long long c[maxn];
  12. int p[maxn];
  13. int vis[maxn];
  14. long long d[maxn];
  15. vector<pair<int,long long> >G[maxn];
  16. int main()
  17. {
  18. int t;scanf("%d",&t);
  19. for(int cas = ;cas <= t;cas++)
  20. {
  21. int n,m;scanf("%d%d",&n,&m);
  22. for(int i=;i<=m;i++)
  23. G[i].clear();
  24. for(int i=;i<=n;i++)
  25. scanf("%d",&x[i]);
  26. for(int i=;i<=n;i++)
  27. scanf("%d",&y[i]);
  28. for(int i=;i<=n;i++)
  29. scanf("%lld",&c[i]);
  30. for(int i=;i<=m;i++)
  31. scanf("%d",&p[i]);
  32.  
  33. for(int i=;i<=n;i++)
  34. {
  35. if(p[x[i]]==)continue;
  36. G[y[i]].push_back(make_pair(x[i],c[i]));
  37. }
  38.  
  39. queue<int> Q;
  40. for(int i=;i<=m;i++)
  41. {
  42. if(p[i]==)
  43. {
  44. d[i]=;
  45. vis[i]=;
  46. Q.push(i);
  47. }
  48. else
  49. {
  50. d[i]=inf;
  51. vis[i]=;
  52. }
  53. }
  54.  
  55. while(!Q.empty())
  56. {
  57. int now = Q.front();
  58. Q.pop();
  59. vis[now]=;
  60. for(int i=;i<G[now].size();i++)
  61. {
  62. int v = G[now][i].first;
  63. if(d[v]>G[now][i].second + d[now])
  64. {
  65. d[v] = G[now][i].second + d[now];
  66. if(vis[v])continue;
  67. Q.push(v);
  68. vis[v]=;
  69. }
  70. }
  71. }
  72.  
  73. long long ans = ;
  74. int flag = ;
  75. for(int i=;i<=m;i++)
  76. {
  77. if(p[i]==)
  78. {
  79. if(d[i]==inf)
  80. {
  81. flag = ;
  82. break;
  83. }
  84. ans += d[i];
  85. }
  86. }
  87. if(flag)
  88. printf("Case #%d: -1\n",cas);
  89. else
  90. printf("Case #%d: %lld\n",cas,ans);
  91. }
  92. }

2015南阳CCPC F - The Battle of Guandu 多源多汇最短路的更多相关文章

  1. 2015南阳CCPC C - The Battle of Chibi DP

    C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...

  2. 2015南阳CCPC C - The Battle of Chibi DP树状数组优化

    C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...

  3. 2015 南阳ccpc The Battle of Chibi (uestc 1217)

    题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...

  4. 2015南阳CCPC H - Sudoku 数独

    H - Sudoku Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny g ...

  5. 2015南阳CCPC G - Ancient Go dfs

    G - Ancient Go Description Yu Zhou likes to play Go with Su Lu. From the historical research, we fou ...

  6. 2015南阳CCPC D - Pick The Sticks 背包DP.

    D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...

  7. 2015南阳CCPC A - Secrete Master Plan A.

    D. Duff in Beach Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a ...

  8. 「2015南阳CCPC D」金砖 解题报告

    金砖 Problem 有一个长度为L的板凳,可以放一排金砖,金砖不能重叠.特别的,摆放的金砖可以超出板凳,前提是必须保证该金砖不会掉下去,即该金砖的重心必须在板凳上. 每块金砖都一个长度和价值,且金砖 ...

  9. 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

    Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...

随机推荐

  1. 【转】ios app 应用内购买配置完全指南

    转自:http://blog.sina.com.cn/s/blog_4b55f6860100sbfb.html 第一印象觉得In-App Purchase(简称IAP)非常简单.Apple提供的大量文 ...

  2. shell 删除日志

    一般线上服务的日志都是采用回滚的防止,写一定数量的日志 或是有管理工具定期去转移老旧日志 前几天删除一个测试环境的日志,只保留两天的日志,结果把正在写的日志都给删掉了,不得不重启了服务,经过这一次的错 ...

  3. 安卓dalvik和art区别

    Dalvik模式像是一台折叠自行车,每次骑之前都要组装后才能上路.而ART模式就是一个已经装好的自行车,直接就能上车走人.所以ART模式在效率上肯定是要好于Dalvik. 通过以上这种表格,我们可以直 ...

  4. Linux如何统计进程的CPU利用率

    1.0 概述 在Linux的/proc文件系统,可以看到自启动时候开始,所有CPU消耗的时间片:对于个进程,也可以看到进程消耗的时间片.这是一个累计值,可以"非阻塞"的输出.获得一 ...

  5. Oracle 数据乱码

    原文 Oracle 数据乱码 服务器配置环境变量 NLS_LANG:American_america.ZHS16GBK        

  6. [Everyday Mathematics]20150205

    设 $\phi:[k_0,\infty)\to[0,\infty)$ 是有界递减函数, 并且 $$\bex \phi(k)\leq \sex{\frac{A}{h-k}}^\al\phi(h)^\be ...

  7. <译>Selenium Python Bindings 6 - WebDriver API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  8. OpenGL超级宝典第5版&&开发环境搭建

    参考:http://www.zyh1690.org/build-opengl-super-bible-fifth-edition-development-environment/ 环境搭建的测试环境为 ...

  9. 关于CCSprite改变box2d刚体位置以及角度。

    同事今天在讨论一个事情,box2d中,body不可以直接设置位置,这样是不合理的,因为在物理的世界,你去左右它的物理检测.它就没有存在的必要了.但是,有人就想直接用box2d的碰撞.不用物理模拟.怎么 ...

  10. Python之正则

    从学习Python至今,发现很多时候是将Python作为一种工具.特别在文本处理方面,使用起来更是游刃有余. 说到文本处理,那么正则表达式必然是一个绝好的工具,它能将一些繁杂的字符搜索或者替换以非常简 ...