【HDU 3810】 Magina (01背包,优先队列优化,并查集)
Magina
Problem DescriptionMagina, also known as Anti-Mage, is a very cool hero in DotA (Defense of the Ancient).
If you have no idea of him, here is some brief introduction of his legendary antecedents:
Twin sons to the great Prophet, Terrorblade and Magina were blessed with divine powers: Terrorblade granted with an unnatural affinity with life forces; Magina gifted with energy manipulation. Magina's eventual overexposure to the magic gradually augmented his elemental resistances and bestowed him the unique ability to move faster than light itself. Now, broken by Terrorblade's fall to the dark side, Magina answers the Sentinel's call in a desperate bid to redeem his brother. Every bitter strike turns the Scourge's evil essences upon themselves, culminating in a finale that forces his enemy to awaken to the void within and spontaneously implode.
Magina has a very IMBA (imbalanced) skill – Blink, yes, move from one place to another place in a wink. Our problem begins at there.
As a formidable hero in the later stage, Magina always farm with the wild monsters for a long time. To make the farming more efficient, Magina use Blink frequently to jump here and there. Here we assume Blink skill has no CD, that is, we can use this skill at any time we want.
There are N spots of the wild monsters, and Magina can choose any one to begin. For every spots, Magina may use Ti time to kill the monsters and gain Gi units money, or he choose blink to other spots, which is known to our brilliant Magina. If the monsters in a spot were killed, it won’t appear any more.
Now Magina want to get M units money to but some excellent equipment, say Battle Fury for example. As a hero to save the world, there is no much time left for Magina, he wonders the minimum time for him to gain at least M units money.InputThe first line contains a single integer T, indicating the number of test cases.
Each test case begins with two integers N, M. Their meanings are the same as the description.
Then N blocks follow, each one describes a spot of wild monsters.
The first line of each block is there integers Ti, Gi and Ki. Ti is the time, Gi is the units of money, Ki is the number of spots Magina can blink to from here.
Then Ki integer Cij follow, indicating the spots’ ID Magina can blink to. You may assume no ID would appear twice.
The spots are described with ID increasing from 1 to N. Input ensure if you can blink from i to j, you can also blink from j to i.Technical Specification
1. 1 <= T <= 50
2. 1 <= N <= 50
3. 1 <= Ti <= 10000000
4. 1 <= M, Gi <= 1000000000
5. 1 <= Ki < N
6. 1 <= Cij <= NOutputFor each test case, output the case number first, then the minimum time for Magina to gain at least M units money, if can’t, output “Poor Magina, you can't save the world all the time!”.Sample Input31 42 5 01 51 4 04 101 9 03 3 13
3 32
2 44 4 13Sample OutputCase 1: 2Case 2: Poor Magina, you can't save the world all the time!Case 3: 10
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define Maxn 60
#define INF 0xfffffff int t[Maxn],g[Maxn],k[Maxn][Maxn];
int fa[Maxn]; struct node
{
int x,y;//time money
friend bool operator < (node x,node y)
{
if(x.y==y.y) return x.x<y.x;
return x.y<y.y;
}
}; priority_queue<node> q[]; int mymin(int x,int y) {return x<y?x:y;} int ffind(int x)
{
if(fa[x]!=x) fa[x]=ffind(fa[x]);
return fa[x];
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++)
{
int k;
scanf("%d%d%d",&t[i],&g[i],&k);
while(k--)
{
int x;
scanf("%d",&x);
fa[ffind(x)]=ffind(i);
}
} int ans=INF; node ft;
ft.x=;ft.y=;
q[].push(ft);
for(int l=;l<=n;l++) if(fa[l]==l)
{
while(!q[].empty()) q[].pop();
while(!q[].empty()) q[].pop();
q[].push(ft);
for(int i=;i<=n;i++) if(ffind(i)==l)
{
while(!q[].empty())
{
node x=q[].top();
q[].pop();
q[].push(x);
node now;
now.y=x.y+g[i];
now.x=x.x+t[i]; if(now.y>=m)
{
ans=mymin(ans,now.x);
continue;
}
if(now.x>=ans) continue; q[].push(now);
}
// 优化减枝
int minn=INF;
while(!q[].empty())
{
node x=q[].top();q[].pop();
if(x.x<minn) q[].push(x),minn=x.x;
if(x.y==m) ans=mymin(ans,x.x);
}
}
}
printf("Case %d: ",++kase);
if(ans==INF) printf("Poor Magina, you can't save the world all the time!\n");
else printf("%d\n",ans);
}
return ;
}
[HDU 3810]
2016-10-14 20:53:14
【HDU 3810】 Magina (01背包,优先队列优化,并查集)的更多相关文章
- hdu 2546 典型01背包
分析:每种菜仅仅可以购买一次,但是低于5元不可消费,求剩余金额的最小值问题..其实也就是最接近5元(>=5)时, 购买还没有买过的蔡中最大值问题,当然还有一些临界情况 1.当余额充足时,可以随意 ...
- [洛谷Luogu]P1141 01迷宫[联通块 并查集]
题目链接 大致题意 相邻格子不同为连通,计算每个点所在的连通块大小. 想法 我采用了并查集的做法. 开一个辅助数组记录连通块大小,每次合并的时候更新父亲节点的大小即可. 一个点先与它上面的点判定,若判 ...
- hdu 3810 Magina 队列模拟0-1背包
题意: 出一些独立的陆地,每片陆地上有非常多怪物.杀掉每一个怪物都须要一定的时间,并能获得一定的金钱.给出指定的金钱m, 求最少要多少时间能够得到m金钱,仅能选择一个陆地进行杀怪. 题解: 这题,假设 ...
- hdu1059 多重背包(转换为01背包二进制优化)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1059 之前写过一个多重背包二进制优化的博客,不懂请参考:http://www.cnblog ...
- ACM HDU Bone Collector 01背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 这是做的第一道01背包的题目.题目的大意是有n个物品,体积为v的背包.不断的放入物品,当然物品有 ...
- hdu 2955 Robberies (01背包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...
- poj1742(多重背包分解+01背包二进制优化)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- HDU 2639(01背包求第K大值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...
- hdu 3466 排序01背包
也是好题,带限制的01背包,先排序,再背包 这题因为涉及到q,所以不能直接就01背包了.因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m], ...
- hdu 2955 Robberies 0-1背包/概率初始化
/*Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- 深入理解计算机系统第二版习题解答CSAPP 2.4
不进行数字转换为二进制和十六进制,计算结果. A. 0x503C + 0x8 = 0x5044 B. 0x503C - 0x40 = 0x4FFC C. 0x503C + 64 = 0x503C + ...
- Sql 随机生成日期时间
DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT SET @dt1='2010-01-01'--开始日期 SET @dt2='2010-0 ...
- javascript-02
1.js的特点2.js的数据类型3.js运算符 4.js的全局变量 |-定义在函数体外部的变量 |-定义在函数体内部没有使用var声明 var和没有var声明变量的区别? |-var ...
- Unity3D 之3D游戏SD快打 3D游戏基础入门开发全(1)
这里记录一个U3D游戏,3D游戏的基本开发. 导入素材 1.首先导入需要的素材.因为FBX格式的素材是通用的,所以尽量导入这样的资源使用 导入后的结果: 然后对人形骨骼进行设置. 看哪里没有映射到骨骼 ...
- OC - 4.OC核心语法
一.点语法 1> 基本使用 点语法本质上是set方法/get方法的调用 2> 使用注意 若出现在赋值操作符的右边,在执行时会转换成get方法 若出现在赋值操作符的左边,在执行时会转换成se ...
- ffmpeg只使用h264编译参数
--disable-everything --enable-decoder=h264 --enable-demuxer=h264 --enable-parser=h264 --disable-ffpl ...
- linux - 创建用户
apt-get update apt-get upgrade root@iZ28t2p7lz9Z:~# adduser cuiAdding user `cui' ...Adding new group ...
- ubuntu 安装flash插件
参考文献: http://wiki.debian.org.hk/w/Install_Flash_Player_with_APTapt-get install adobe-flashplugin
- PHP类中的__get()和__set函数到底有什么用?
当试图获取一个不可达变量时,类会自动调用__get. 同样的,当试图设置一个不可达变量时,类会自动调用__set. 在网站中,这两个并不是什么非用不可的函数. 例如: Class Test { ...
- javascript 节点属性详解
javascript 节点属性详解 根据 DOM,html 文档中的每个成分都是一个节点 DOM 是这样规定的:整个文档是一个文档节点每个 html 标签是一个元素节点包含在于 html 元素中的文本 ...