2015南阳CCPC F - The Battle of Guandu 多源多汇最短路
The Battle of Guandu
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
无
Description
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
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 多源多汇最短路的更多相关文章
- 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 ...
- 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 ...
- 2015 南阳ccpc The Battle of Chibi (uestc 1217)
题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 「2015南阳CCPC D」金砖 解题报告
金砖 Problem 有一个长度为L的板凳,可以放一排金砖,金砖不能重叠.特别的,摆放的金砖可以超出板凳,前提是必须保证该金砖不会掉下去,即该金砖的重心必须在板凳上. 每块金砖都一个长度和价值,且金砖 ...
- 2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大
Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom perio ...
随机推荐
- 【转】Android fill_parent和wrap_content分析
fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕. wrap_content布局指根据视图内部内容自动扩展以适应其大小. 1. wrap_content <?xml versi ...
- angular+rails集成实战
http://start.jcolemorrison.com/setting-up-an-angularjs-and-rails-4-1-project/ 1. 添加gemgem 'sprockets ...
- hdu 4300(kmp)
题意:说实话这个题的题意还真的挺难懂的,我开始看了好久都没看懂,后来百度了下题意才弄懂了,这题的意思就是首先有一个字母的转换表,就是输入的第一行的字符串,就是'a'转成第一个字母,'b'转成转换表的第 ...
- HDU-3280 Equal Sum Partitions
http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 M ...
- Android自带CalendarView类实现日历视图
文章由来:学习一下日历控件的实现,Android3.0以下的版本请查阅其他文章. 本文主要是介绍Android日历视图控件CalendarView相关的内容,然后在后面会给出一个简单的Demo. Ca ...
- duilib combo控件,当鼠标滚动时下拉列表自动关闭的bug的修复
转载请说明出处,谢谢~~ 群里有朋友提到了使用Combo控件时,当下拉列表出现,此时鼠标滚轮滚动,下拉列表就自动消失了.我看了一下源码,这个bug的修复很简单. CComboUI控件被单击时创建CCo ...
- Leave Morningstar
Today, closed 4++ years developer life (2009.11.17-2014.02.28) in MorningStar Shenzhen Office Austra ...
- maven学习系列教程,第一课(web项目的搭建)
1.现在一般eclipse都已经装好了maven板块,无需自行下载安装,所以我们的第一步就是新建一个maven project 2地址使用默认的就行 3这边筛选一下,选择webapp 4. 5.建好后 ...
- 初识HTML 5:关于它的三个三
来源:http://www.ido321.com/949.html 一.HTML 5受欢迎的三个理由 1.IE.Google.Firefox.Safari.Opera等主流浏览器的支持 1.1 微软 ...
- JavaScript中的*top、*left、*width、*Height详解
来源:http://www.ido321.com/911.html html代码 1: <body> 2: <div class="father" id=&quo ...