4.3.7 Kill the monster

来源:互联网 发布:aster遥感数据 编辑:程序博客网 时间:2024/05/18 11:25

Kill the monster

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 118    Accepted Submission(s): 86

 
Problem Description
There is a mountain near yifenfei’s hometown. On the mountain lived a big monster. As a hero in hometown, yifenfei wants to kill it.
Now we know yifenfei have n spells, and the monster have m HP, when HP <= 0 meaning monster be killed. Yifenfei’s spells have different effect if used in different time. now tell you each spells’s effects , expressed (A ,M). A show the spell can cost A HP to monster in the common time. M show that when the monster’s HP <= M, using this spell can get double effect.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m (2<n<10, 1<m<10^7), express how many spells yifenfei has.
Next n line , each line express one spell. (Ai, Mi).(0<Ai,Mi<=m).
Output
For each test case output one integer that how many spells yifenfei should use at least. If yifenfei can not kill the monster output -1. 
Sample Input
3 10010 2045 895  403 10010 2045 905 403 10010 2045 845 40
Sample Output
32-1
 
#include<cstdio>#include<cstring>#include<stack>using namespace std;const int MAXN = 10;int a[MAXN], m[MAXN];int n, HP;struct node{int hp, t;bool vis[MAXN];};int dfs(){int mint = 1e6;stack<node> s;node cur, next;memset(cur.vis, 0, sizeof(cur.vis));cur.hp = HP;cur.t = 0;while(!s.empty()) s.pop();s.push(cur);while(!s.empty()){cur = s.top();s.pop();for(int i = 0; i < n; i++){next = cur;if(!next.vis[i]){if(next.hp <= m[i]){next.hp -= 2 * a[i];}else next.hp -= a[i];next.t++;next.vis[i] = 1;if(next.hp <= 0){if(mint > next.t) mint = next.t;continue;} s.push(next);}}}if(mint == 1e6)return -1;else return mint;}int main(){while(scanf("%d%d", &n, &HP) != EOF){for(int i = 0; i < n; i++){scanf("%d%d", &a[i], &m[i]);}int ans = dfs();printf("%d\n", ans);}return 1;}

0 0
原创粉丝点击