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 ...
随机推荐
- Mybatis学习——一对多关联表查询
1.实体类 public class Student { private int id; private String name; } public class Classes { private i ...
- 【转】自定义iOS的Back按钮(backBarButtonItem)和pop交互手势(interactivepopgesturerecognizer) --- 不错
原文网址:http://blog.csdn.net/joonsheng/article/details/41362499 序 说到自定义UINavigetionController的返回按钮,iOS7 ...
- 【转】Android SwitchButton(滑动开关)
原文网址:http://blog.csdn.net/wangjinyu501/article/details/27961303 版本:1.0 日期:2014.5.17 2014.6.1 版权:© 20 ...
- js基础学习第一天(关于DOM和BOM)一
关于BOM和DOM BOM 下面一幅图很好的说明了BOM和DOM的关系 BOM提供了一些访问窗口对象的一些方法,我们可以用它来移动窗口位置,改变窗口大小,打开新窗口和关闭窗口,弹出对话框,进行导航以及 ...
- 增加SharePoint2010修改域密码功能
转:http://blog.163.com/hr_test/blog/static/16485210720137953131694/ 前提 SharePoint2010的用户基于AD的,因此修改密码是 ...
- OracleHelper(for produce)
OracleHelper中,有一个用存储过程实现的Insert方法. 然后我把执行存储过程的方法 封装成了可以执行任何存储过程,参数是 存储过程名称 以及存错过程中的传入.传出参数 using Ora ...
- Unable to execute dex: method ID not in [0, 0xffff]: 65536
http://ingramchen.io/blog/2014/09/prevention-of-android-dex-64k-method-size-limit.html
- Android下载速度计算
long startTime = System.currentTimeMillis(); // 开始下载时获取开始时间 long curTime = System.currentTimeMillis( ...
- MyBatis一对多双向关联——MyBatis学习笔记之七
处理has-one关系需要用到association元素,而处理has many关系则需要用到collection元素.例如本例中,假设一 名教师可同时指导多名学生,下面就来介绍如何使用collect ...
- CABasicAnimation 使用
1. 基本使用 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50,50)]; view.backgroundColo ...