Test Instructions Description
Krito finally killed the 99-storey boss, came to the 100th floor. The 100th layer can be represented as a tree with n nodes (numbered from 0 to n-1), and each node in the tree may have many monsters. Krito is now on node No. 0 and now it wants to remove all the monsters from this layer. He now has ATK size of attack. You can defeat a monster only if it is greater than the defense of the Beast, and you will get a certain amount of damage bonus for each time you defeat a monster. A node may have more than one monster, you want to defeat all the monsters of this node can be passed from this node, could he finish this task? Note: It is not required to kill all mobs inside a node at once.
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
15 20 10 22 32 4113 10 21 11 0
Sample Output
Oh yes.
Ideas
Because starting from the root node, you must defeat all monsters in the current node before you can go to the next node. Greedy thinking, the first choice of low-defense monsters are always not worse.
So with a priority queue maintenance we can attack to monsters, once a node monster all killed, then its child node monsters into the queue.
If the current minimum defense monsters can not be destroyed, then must be a failure.
Code
#include <stdio.h>#include <iostream>#include <string.h>#include <stdlib.h>#include <algorithm>#include <stack>#include <queue>using namespace STD;#define LL Long Longstructnode{intID, Def, add;friend BOOL operator< (Node A, Node B) {returnA.def > B.def; }};BOOLg[1009][1009];intcnt[1009]; vector<node >v[1009];BOOLvis[1009];intN, M, K;voidInit () {memset(CNT,0,sizeof(CNT));memset(g,0,sizeof(g));memset(Vis,0,sizeof(VIS)); for(intI=0; i<n; i++) V[i].clear ();}BOOLBFS () {priority_queue<node> q; for(intI=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(intI=0; i<n; i++) {if(!vis[i] && g[t.id][i] = =1) {Vis[i] =1; for(intj=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;}intMain () {intTscanf("%d", &t); while(t--) {init ();scanf("%d%d", &n, &m); for(intI=1; i<n; i++) {intA, B;scanf("%d%d", &a, &b); G[A][B] = G[b][a] =1; }scanf("%d", &k); for(intI=0; i<m; i++) {intA, 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's Crusade (bfs&& priority queue)