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

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

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战场需要的最小代价,只要拉一个人就会胜利

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
#define maxn 100005
long long inf = 999999999999999LL;
int x[maxn];
int y[maxn];
long long c[maxn];
int p[maxn];
int vis[maxn];
long long d[maxn];
vector<pair<int,long long> >G[maxn];
int main()
{
int t;scanf("%d",&t);
for(int cas = ;cas <= t;cas++)
{
int n,m;scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
G[i].clear();
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("%lld",&c[i]);
for(int i=;i<=m;i++)
scanf("%d",&p[i]); for(int i=;i<=n;i++)
{
if(p[x[i]]==)continue;
G[y[i]].push_back(make_pair(x[i],c[i]));
} queue<int> Q;
for(int i=;i<=m;i++)
{
if(p[i]==)
{
d[i]=;
vis[i]=;
Q.push(i);
}
else
{
d[i]=inf;
vis[i]=;
}
} while(!Q.empty())
{
int now = Q.front();
Q.pop();
vis[now]=;
for(int i=;i<G[now].size();i++)
{
int v = G[now][i].first;
if(d[v]>G[now][i].second + d[now])
{
d[v] = G[now][i].second + d[now];
if(vis[v])continue;
Q.push(v);
vis[v]=;
}
}
} long long ans = ;
int flag = ;
for(int i=;i<=m;i++)
{
if(p[i]==)
{
if(d[i]==inf)
{
flag = ;
break;
}
ans += d[i];
}
}
if(flag)
printf("Case #%d: -1\n",cas);
else
printf("Case #%d: %lld\n",cas,ans);
}
}

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. 在android中进行视频的分割

    最近项目有个需求要对录制的视频进行分割,查了很多资料,看到ffmpeg可以对视频进行分割.上网找到别人基于android的开源ffmpeg,终于编译成功ffmpeg.so.但是要使用的话还要查ffmp ...

  2. ASP.NET 经典60道面试题

    转:http://bbs.chinaunix.net/thread-4065577-1-1.html ASP.NET 经典60道面试题 1. 简述 private. protected. public ...

  3. JS 代码编一个倒时器

    有时候在生活中,你需要一个JavaScript倒计时时钟,而不是一个末日装置设备.不管你是否有一次约会,销售.促销.或者游戏,你可以受益于使用原生JavaScript构建一个时钟,而不是拿到一个现成的 ...

  4. hihocoder 1233 Boxes

    题意:类汉诺塔的一个东西……移动规则与汉诺塔一样,但初始状态为题目中给出的每根棍上一个盘子,目标状态为盘子在棍上按大小顺序排列,盘子只能在相邻的棍儿上移动. 解法:广搜并打表记录从目标状态到所有可能的 ...

  5. 2、列表item_圆头像_信息提示

    import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import an ...

  6. Selenium用户扩展

    Selenium用户扩展 这很容易扩展Selenium IDE加入自定义操作,断言和定位,策略,这是通过添加方法,在JavaScript的帮助下Selenium 对象原型.在启动时,Selenium会 ...

  7. Nonlinear Transform

    前文中,我们已经学习了linear classification,linear regression,logistic regression三种线性方法. 如何解决这种问题呢? 其实很好解决,只需要加 ...

  8. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  9. .net,mvc使用bootstrap做一个标准后台

    今天准备搭一个公用后台,使用bootstrap,方便今后开发,顺便mark一下 后期列表页将使用kendo-ui,增强后台的效果 下面是代码... login页面 @{ Layout = null; ...

  10. 文本分类之特征描述vsm和bow

    当我们尝试使用统计机器学习方法解决文本的有关问题时,第一个需要的解决的问题是,如果在计算机中表示出一个文本样本.一种经典而且被广泛运用的文本表示方法,即向量空间模型(VSM),俗称“词袋模型”. 我们 ...