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. liux下ftp链接服务器的常用命令

    FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用 FTP,都会遇到大量的FTP内部命令.熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍 ...

  2. C++ STL算法系列3---求和:accumulate

    该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ...

  3. [LeetCode] Single Number III ( a New Questions Added today)

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  4. 利用redis分布式锁的功能来实现定时器的分布式

    文章来源于我的 iteye blog http://ak478288.iteye.com/blog/1898190 以前为部门内部开发过一个定时器程序,这个定时器很简单,就是配置quartz,来实现定 ...

  5. Codeforces 367

    A. Sereja and Algorithm 水题不解释. B. Sereja ans Anagrams 模p同余的为一组,随便搞. C. Sereja and the Arrangement of ...

  6. 【sgu282】Isomorphism

    题意: 给出n(n<=53)点的无向完全图 要将每条边染上m(m<=1000)种颜色的一种 只改变顶点编号的图视为同种方案 求本质不同方案数%p(p>n且为质树)的值 题解: 这题貌 ...

  7. Delimiter must not be alphanumeric or backslash 问题及解决

    Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in 正则表达 ...

  8. Gym 100507A About Grisha N. (水题)

    About Grisha N. 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/A Description Grisha N. t ...

  9. HDU 5762 Teacher Bo (暴力)

    Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

  10. Linux系统管理员应该了解的一些I/O统计工具

    作为一个Linux系统管理员,统计各类IO是一项必不可少的工作.其统计工具中iostat显然又是最重要的一个统计手段.但是这里iostat不是本文的重点,因为这个工具的使用在网络上已经有大量的教程,可 ...