【BZOJ3875】【AHOI2014】骑士游戏 [Spfa][DP]
骑士游戏
Time Limit: 30 Sec Memory Limit: 256 MB
[Submit][Status][Discuss]
Description
Input
Output
输出一行一个整数,表示最少需要的体力值。
Sample Input
4 27 3 2 3 2
3 5 1 2
1 13 2 4 2
5 6 1 2
Sample Output
HINT
Solution
首先,若是呈现树形结构,我们显然可以得到一个DP:f[i] = min(f[i], Σf[son[i]])(f[i]表示消灭 i 最小花费)。
但是,显然数据会出现有环的情况。所以我们这个DP是有后效性的。
那么我们就可以用Spfa来消除这个后效性,具体就是:若一个点的 f 在某处被更新了,那么把father[i]重新入队计算。
(复杂度BearChild也不会算啊QAQ)
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
using namespace std;
typedef long long s64; const int ONE = ;
const int MOD = 1e9 + ; int n, x;
s64 f[ONE], unit[ONE]; int next[ONE], first[ONE], go[ONE], tot;
int nextop[ONE], firstop[ONE], goop[ONE], totop; queue <int> q; int get()
{
int res=,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} void Add(int u, int v)
{
next[++tot] = first[u], first[u] = tot, go[tot] = v;
nextop[++totop] = firstop[v], firstop[v] = totop, goop[totop] = u;
} void Spfa()
{
for(int i = ; i <= n; i++) q.push(i);
while(!q.empty())
{
int u = q.front(); q.pop(); s64 res = unit[u];
for(int e = first[u]; e; e = next[e])
res += f[go[e]]; if(res < f[u])
{
f[u] = res;
for(int e = firstop[u]; e; e = nextop[e])
q.push(goop[e]);
}
}
} int main()
{
n = get();
for(int i = ; i <= n; i++)
{
scanf("%lld %lld", &unit[i], &f[i]);
x = get();
while(x--) Add(i, get());
} Spfa(); printf("%lld", f[]);
}
【BZOJ3875】【AHOI2014】骑士游戏 [Spfa][DP]的更多相关文章
- BZOJ 3875: [Ahoi2014]骑士游戏 spfa dp
3875: [Ahoi2014]骑士游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3875 Description [故事背景] 长 ...
- [bzoj3875] [Ahoi2014]骑士游戏
3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 844 Solved: 440[Submit][Status ...
- LUOGU P4042 [AHOI2014/JSOI2014]骑士游戏 (spfa+dp)
传送门 解题思路 首先设\(f[x]\)表示消灭\(x\)的最小花费,那么转移方程就是 \(f[x]=min(f[x],\sum f[son[x]] +s[x])\),如果这个转移是一个有向无环图,那 ...
- [BZOJ3875][AHOI2014]骑士游戏(松弛操作)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3875 分析: 类似于spfa求最短路,设d[i]表示完全消灭i号怪物的最小花费,我们对 ...
- BZOJ 3875: [Ahoi2014]骑士游戏 dp+spfa
题目链接: 题目 3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MB 问题描述 [故事背景] 长期的宅男生活中,JYY又挖掘出了一 ...
- BZOJ 3875: [Ahoi2014]骑士游戏
d[i]表示消灭i所需的最小体力值, d[i] = min(S[i], K[i]+Σd[x]), Σd[x]表示普通攻击而产生的其他怪兽. 因为不是DAG, 所以用个队列类似SPFA来更新答案. -- ...
- 【BZOJ3875】[Ahoi2014&Jsoi2014]骑士游戏 SPFA优化DP
[BZOJ3875][Ahoi2014&Jsoi2014]骑士游戏 Description [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会扮演一个英勇的 ...
- bzoj3875 【Ahoi2014】骑士游戏 spfa处理后效性动规
骑士游戏 [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会 扮演一个英勇的骑士,用他手中的长剑去杀死入侵村庄的怪兽. [问题描述] 在这个游戏中,JYY一共有两种攻 ...
- bzoj 3875: [Ahoi2014&Jsoi2014]骑士游戏【dp+spfa】
设f[i]为杀死i的最小代价,显然\( f[i]=min(k[i],s[i]+\sum f[to]) \) 但是这个东西有后效性,所以我们使用spfa来做,具体就是每更新一个f[i],就把能被它更新的 ...
随机推荐
- c# 两个软件传参
1.socket 传参,类似于小型的服务器和客户端,一端发送,另一端保持监听状态. 2.通过第三方 数据库或者文件.
- iOS开发NSDate详解
1. 用于创建NSDate实例的类方法有 + (id)date; 返回当前时间 + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 返回以 ...
- OSG学习:多重纹理映射
#include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...
- Uncaught ReferenceError: wx is not defined
程序的分享功能调用了微信的接口,但是忽然发现就报这个错误, Uncaught ReferenceError: wx is not defined 同时下方还有这个错误 This content sho ...
- django为model设置表名
class redis_data(models.Model): class Meta: db_table='redis_data' key=models.CharFie ...
- 如何在存储过程中执行set命令 我来答
1.EXEC使用EXEC命令两种用种执行存储程另种执行态批处理所讲都第二种用 面先使用EXEC演示例,代码1DECLARE @TableName VARCHAR(50),@Sql NVARCHAR ( ...
- 第68天:原型prototype方法
一.原型prototype方法声明 构造函数有一个prototype属性,指向实例对象的原型对象.通过同一个构造函数实例化的多个对象具有相同的原型对象.经常使用原型对象来实现继承 <!DOCTY ...
- Qt——设计颜色编辑选取对话框
Qt中已经有一些封装好的对话框,比如QMessageBox.QColorDialog等,使用起来快捷方便,但缺点是我们无法为它们自定义样式,所以可能难以“融入”我们的项目.既然如此,那就自己做一个把. ...
- 优先队列实现 大小根堆 解决top k 问题
摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...
- 2017中国大学生程序设计竞赛-哈尔滨站 H - A Simple Stone Game
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...