Magina

Problem Description
Magina, 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.
Input
The 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 <= N

Output
For 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 Input
3
1 4
2 5 0
 
1 5
1 4 0
 
4 10
1 9 0
3 3 1
3
3 3
2
2 4
4 4 1
3
Sample Output
Case 1: 2
Case 2: Poor Magina, you can't save the world all the time!
Case 3: 10
 
 
【题意】
  有n堆野兽,每堆野兽屠杀完完需要花费ti时间,可以增加金钱gi,敌法师有瞬移技能,可以从某堆野兽移到另一堆野兽,题目有给定从哪堆可以移到哪堆。最后问在满足打的金钱多余m的情况下的最少时间。
1 <= T <= 50   , 1 <= N <= 50  , 1 <= Ti <= 10000000   ,1 <= M, Gi <= 1000000000
 
【分析】
  注意看数据范围,如果数据小的话,只是一道简单的01背包。
  所以这是一道进化的01背包。
  之前打01背包的时候就觉得有很多状态其实是没有用的,我们可以不记录它的啊!
  但是即使这样,状态还是有2^50,大神说了,要剪枝,于是我就剪了。
  用优先队列。按金钱递减排序,以及花费时间递增排序。我们发现有些状态是没有用的,比如说(5,5)即花费时间5,得到金钱5,以及状态(5,4)花费时间5,得到金钱4,后者明显不如前者,则后者必然可以舍弃。也就是如果金钱少但是时间多,这样的状态可以舍弃。
  两个优先队列,其实本质上是滚动数组,为什么要用优先队列呢?其实我觉得放进去之后排个序剪枝就好啦,(用优先队列总给我一种莫名的不安全感),但是东西不能用数组存,因为你不知道大小,只能用容器,那就优先队列吧,反正时间差不多。
  剪枝剪得不好会MLE的,嗯,打错也会MLE。
  因为联通块的才能一起选,所以就分成几部分做,用并查集求联通块。
 
 
 #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背包,优先队列优化,并查集)的更多相关文章

  1. hdu 2546 典型01背包

    分析:每种菜仅仅可以购买一次,但是低于5元不可消费,求剩余金额的最小值问题..其实也就是最接近5元(>=5)时, 购买还没有买过的蔡中最大值问题,当然还有一些临界情况 1.当余额充足时,可以随意 ...

  2. [洛谷Luogu]P1141 01迷宫[联通块 并查集]

    题目链接 大致题意 相邻格子不同为连通,计算每个点所在的连通块大小. 想法 我采用了并查集的做法. 开一个辅助数组记录连通块大小,每次合并的时候更新父亲节点的大小即可. 一个点先与它上面的点判定,若判 ...

  3. hdu 3810 Magina 队列模拟0-1背包

    题意: 出一些独立的陆地,每片陆地上有非常多怪物.杀掉每一个怪物都须要一定的时间,并能获得一定的金钱.给出指定的金钱m, 求最少要多少时间能够得到m金钱,仅能选择一个陆地进行杀怪. 题解: 这题,假设 ...

  4. hdu1059 多重背包(转换为01背包二进制优化)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1059 之前写过一个多重背包二进制优化的博客,不懂请参考:http://www.cnblog ...

  5. ACM HDU Bone Collector 01背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 这是做的第一道01背包的题目.题目的大意是有n个物品,体积为v的背包.不断的放入物品,当然物品有 ...

  6. hdu 2955 Robberies (01背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...

  7. poj1742(多重背包分解+01背包二进制优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  8. HDU 2639(01背包求第K大值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...

  9. hdu 3466 排序01背包

    也是好题,带限制的01背包,先排序,再背包 这题因为涉及到q,所以不能直接就01背包了.因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m], ...

  10. hdu 2955 Robberies 0-1背包/概率初始化

    /*Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. 混合文件系统(ramdisk+jffs)

    背景知识: 一.Ramdisk文件系统: 1.掉电丢失 2.读写速度高 3.数据存储到内存 二.jffs文件系统 1.掉电不丢失 2.可存储于NOR NAND,但是适用于NOR 3.数据存储于flas ...

  2. 获得服务器硬件信息(CPUID、硬盘号、主板序列号、IP地址等)

    1 // 注意:首先要在项目中添加引用 System.Management using System; using System.Collections.Generic; using System.L ...

  3. 关于cnpm的一点小bug

    在实际工作中,一个项目完成后,在上线前,常常需要把代码进行压缩,一般是用gulp或者 webpack 进行压缩.(小妹是用gulp) gulp是运行在node 环境下的. 所以首先,下载并安装了nod ...

  4. 根据id设置、获取元素的文本和value

    /** * 根据id获取元素文本 * @param {String} id|元素id * return {Integer || String} text */function getText(id){ ...

  5. 重新设置MySQL的root密码

    1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...

  6. HOW TO: Creating your MSI installer using Microsoft Visual Studio* 2008

    Quote from: http://software.intel.com/en-us/articles/how-to-creating-your-msi-installer-using-visual ...

  7. Sdut 2416 Fruit Ninja II(山东省第三届ACM省赛 J 题)(解析几何)

    Time Limit: 5000MS Memory limit: 65536K 题目描述 Haveyou ever played a popular game named "Fruit Ni ...

  8. ubuntu server修改时区

    公司用的是ubuntu server 服务器在美国亚马逊VPS 现在要修改时区 执行:tzselect 或直接修改 /etc/timezone 文件,我是改成(America/Los_Angeles) ...

  9. 深入了解relative

    1.relative是自身定位,距原本位置的偏移 2.无侵入布局: 挪动位置,原本位置还在占据,并不会影响其他元素的布局   应用: 实现鼠标拖拽,比自身api好用 3.top/bottom 和 le ...

  10. Windows加密视频播放器使用教程

    1.   下载文件 http://pan.baidu.com/s/1c2aESQs 2.    操作流程 温馨提示 播放时,请务必保证播放设备联网(原因:用户名权限验证需要网络,播放后10秒即可关闭网 ...