题意

Description

Krito最终干掉了99层的boss,来到了第100层。

第100层能够表示成一颗树。这棵树有n个节点(编号从0到n-1),树上每个节点可能有非常多仅仅怪物。 Krito如今在0号节点,如今它想要区清除这一层全部的怪物。他如今有atk大小的攻击力。

仅仅有当你的攻击力大于这仅仅怪物的防御力时,你才干够打败他。同一时候每打败仅仅怪物,你会获得一定的攻击力加成。一个节点可能存在着不止一仅仅怪兽,你要打败这个节点的全部怪物才干能够从这个节点通过,请问他能不能完毕这个任务?注意:不要求一次性杀光一个节点里面的全部怪物。

Input

第1行:一个数T,表示有T个測试例子(0<=T<=50) ,接下来有T个測试例子

对于每个測试例子:

第1行:两个整数n。m表示这棵树有n个节点,m仅仅怪兽(0<=n<=1000 ,0<=m <=100)

第2至n-1行: 两个整数u。v表示编号为u,v之间的节点有一条无向边,题目保证不会成环。(0<=u,v<n , u!=v)

>第3行: 一个整数atk,表示Krito的初始化攻击力(0<=atk<=100)

第4至3+m行:两个整数id,def。add_atk,表示在编号为id的点上,有一仅仅防御力为def的怪物,打败后能够添加add_atk点的攻击力。(0<=add_atk,def<=100)

Output

对于每个測试例子。假设Krito能够清除全部的怪物,则输出“Oh yes.” 否则。输出“Good Good Study,Day Day Up.”

Sample Input

1
5 2
0 1
0 2
2 3
2 4
11
3 10 2
1 11 0

Sample Output

Oh yes.

思路

由于从根节点開始,必须打败当前节点的全部怪物,才干够进入下一节点。贪心思想,先选择防御力低的怪物总是不会更坏。

所以用一优先队列维护我们能够攻击到到怪物,一旦某节点怪物全杀完,则将其子节点怪物添加队列。

假设当前最小防御力怪物都不能消灭。那么一定是失败的。

代码

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
#define LL long long struct Node
{
int id, def, add;
friend bool operator < (Node a, Node b)
{
return a.def > b.def;
}
}; bool g[1009][1009];
int cnt[1009];
vector<Node > v[1009];
bool vis[1009];
int n, m, k; void init()
{
memset(cnt, 0, sizeof(cnt));
memset(g, 0, sizeof(g));
memset(vis, 0, sizeof(vis));
for(int i=0; i<n; i++)
v[i].clear();
} bool bfs()
{
priority_queue<Node> q;
for(int i=0; i<v[0].size(); i++)
q.push(v[0][i]); if(cnt[0] == 0)
{
Node t = {0, -1, 0};
q.push(t);
}
vis[0] = 1;
while(!q.empty())
{
Node t = q.top();
q.pop(); if(t.def == -1)
{
for(int i=0; i<n; i++)
{
if(!vis[i] && g[t.id][i] == 1)
{
vis[i] = 1;
for(int j=0; j<cnt[i]; j++)
q.push(v[i][j]);
if(cnt[i] == 0)
{
Node x = {i, -1, 0};
q.push(x);
}
}
} continue;
}
if(t.def < k)
{
k += t.add;
if(--cnt[t.id] == 0)
{ t.def = -1;
q.push(t);
}
}
else
return false;
}
return true;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d", &n, &m);
for(int i=1; i<n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
g[a][b] = g[b][a] = 1;
} scanf("%d", &k); for(int i=0; i<m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
Node t={a, b, c};
v[a].push_back(t);
cnt[a]++;
} if(bfs())
printf("Oh yes.\n");
else
printf("Good Good Study,Day Day Up.\n");
}
return 0;
}

GDUT Krito的讨伐(bfs&amp;&amp;优先队列)的更多相关文章

  1. GDUT——1169: Krito的讨伐(优先队列BFS)

    1169: Krito的讨伐 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 619  Solved: 102 Description Krito终于干 ...

  2. 广东工业大学2016校赛决赛-网络赛 1169 Problem A: Krito的讨伐 优先队列

    Problem A: Krito的讨伐 Description Krito终于干掉了99层的boss,来到了第100层.第100层可以表示成一颗树,这棵树有n个节点(编号从0到n-1),树上每一个节点 ...

  3. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. NYOJ 284 坦克大战 【BFS】+【优先队列】

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...

  5. UVALive 2035 The Monocycle(BFS状态处理+优先队列)

    这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...

  6. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  7. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  8. poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】

    题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...

  9. URAL 1930 Ivan's Car(BFS)

    Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are d ...

随机推荐

  1. Ubuntu下搭建repo服务器(二): 配置git-daemon-run

    git-daemon-run实际是一个脚本管理工具,用来启动git-daemon. 1 安装git-daemon-run(A端) apt-get install git-daemon-run 2. 配 ...

  2. json 新用

    如果使用struts2的action,可以省去属性赋值的工夫. 但是假如你没有使用struts2,而且使用的是ajax请求,通过json来传递参数.那我下面所说的对你可能是一个很好的解脱,从此告别re ...

  3. Tcl之Lab1

    Task 1. Use help 1) What is the default switch for the redirect command? -file help -v redirect # or ...

  4. python 将中文转拼音后填充到url做参数并写入excel

    闲着没事写了个小工具,将中文转拼音后填充到url做参数并写如excel 一.先看下演示,是个什么东西 二.代码 代码用到一个中文转拼音的库,库是网上下的,稍微做了下修改,已经找不原来下载的地址了,然后 ...

  5. Codeforces_732D_(二分贪心)

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  6. Python 索引切片

    #负数开头,只有比负数大才有数据 num = [1,2,3,4,5,6,7,8,9,10] print(num[-5:5]) num = [1,2,3,4,5,6,7,8,9,10] print(nu ...

  7. c# 图片资料

  8. 面向对象:__getitem__、__setitem__、__delitem__

    item系列 class Person(object): def __init__(self, name): self.name = name def __getitem__(self, item): ...

  9. Charles 下载-破解-安装-配置

    我当前使用版本为V4.2.7 最新版本下载地址 Charles 在线破解工具 下载完之后,先进行安装,安装完之后,根据破解链接中的步骤来就ok了. 比较费劲配置在下面,不过跟着一步步来就一定能好的 点 ...

  10. img标签和background-image的区别和具体使用时机

    最近在使用图片过程中,纠结到底使用img标签还是使用background-image属性,翻阅资料和百度后作出下列理解. 简单来说img是内容部分的东西,background-image是修饰性的东西 ...