【POJ3810】Magina

Magina

Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 545    Accepted Submission(s): 178


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
 

Author
iSea@WHU
 

Source
 

Recommend
lcy

#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;

typedef long long LL;
const LL maxn = 52;

LL T;
LL N,M;
struct Node
{
    LL w;
    LL v;
    bool operator<(const Node&x) const
    {
        if(v == x.v)
        {
            return w > x.w;
        }
        return v < x.v;
    }
}node[maxn];
LL num[maxn];
LL g[maxn][maxn];
LL used[maxn];
LL mp[maxn][maxn];
LL gnodes[maxn];
LL subgnums;
const LL MAX = 1000000000;
priority_queue<Node>qu1,qu2;

void dfs(LL x)
{
    used[x] = true;
    gnodes[subgnums] ++;
    mp[subgnums][gnodes[subgnums]] = x;
    for(LL i=1;i<=N;i++)
    {
        if(!used[i] && g[x][i])
        {
            dfs(i);
        }
    }
}
int main()
{
  //  freopen("1.txt","r",stdin);
    scanf("%I64d",&T);
    LL Case = 1;
    while(T --)
    {
        memset(g,0,sizeof(g));
        memset(used,0,sizeof(used));
        memset(mp,0,sizeof(mp));
        memset(gnodes,0,sizeof(gnodes));
        scanf("%I64d%I64d",&N,&M);
        subgnums = 0;

        for(LL i=1;i<=N;i++)
        {
            scanf("%I64d%I64d%I64d",&node[i].w,&node[i].v,&num[i]);
            LL v;
            for(LL j=0;j<num[i];j++)
            {
                scanf("%I64d",&v);
                g[i][v] = g[v][i] = 1;
            }
        }
        for(LL i=1;i<=N;i++)
        {
            if(!used[i])
            {
                subgnums++;
                dfs(i);

            }
        }

        LL ans = MAX;
        Node next;
        for(LL i=1;i<=subgnums;i++)
        {
            while(!qu1.empty()) qu1.pop();
            while(!qu2.empty()) qu2.pop();
            Node now;
            now.v = now.w = 0;
            qu1.push(now);
            //cout << gnodes[1] << endl;
            for(LL j=1;j<=gnodes[i];j++)
            {
                LL k = mp[i][j];
                while(!qu1.empty())
                {
                    now = qu1.top();
                    qu1.pop();
                    qu2.push(now);
                    next.v = now.v + node[k].v;
                    next.w = now.w + node[k].w;

                    if(next.v >= M)
                    {

                        if(next.w < ans) ans = next.w;
                        continue;
                    }

                    if(next.w >= ans) continue;
                    qu2.push(next);
                }

                LL minn = MAX;
                while(!qu2.empty())
                {
                    now = qu2.top();
                    qu2.pop();
                    if(now.w <= minn)
                    {
                        qu1.push(now);
                        minn = now.w;
                    }
                }
            }

        }

        if(ans == MAX) ans = -1;
        if(ans!=-1)
            printf("Case %I64d: %I64d\n",Case++,ans);

        else
            printf("Case %I64d: Poor Magina, you can't save the world all the time!\n",Case++);

    }
  //  while(1);
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值