hdu 2616 暴力使用 dfs求最短路径(剪枝有点依稀)

Kill the monster

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1241    Accepted Submission(s): 846

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 100
10 20
45 89
5 40

3 100
10 20
45 90
5 40

3 100
10 20
45 84
5 40

Sample Output

3
2
-1

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int mapp[11][2];
int vis[11];
int minn,n,m,flag;
void dfs(int bloor,int time)
{
 int i,j;
 if(bloor<=0)
 {
 // cout<<".."<<endl;
  if(minn>time) minn=time;
 // cout<<maxx<<endl;
  flag=1;
  return;
 }
 if(time>=minn) return ;//  当次数比以前走过的次数多的时候 剪去
 for(i=1;i<=n;i++)
 {
  if(vis[i]==1) continue;
  vis[i]=1;
 // cout<<".."<<endl;
  if(bloor<=mapp[i][1])
  {
  // cout<<".."<<endl;
      dfs(bloor-2*mapp[i][0],time+1); 
     }
  else
  {
  // cout<<".."<<endl;
   dfs(bloor-mapp[i][0],time+1);
  }
  vis[i]=0;
 }
}
int main()
{
 int i,j;
 while(cin>>n>>m)
 {
  for(i=1;i<=n;i++)
  {
   scanf("%d %d",&mapp[i][0],&mapp[i][1]);
  // cout<<mapp[i][0]<<mapp[i][1]<<endl;
  }
  memset(vis,0,sizeof(vis));
  flag=0;
  minn=999;
  dfs(m,0);
  if(flag==0) printf("-1\n");
  else printf("%d\n",minn);
 }
 return 0;
}

时间: 2024-10-11 18:17:41

hdu 2616 暴力使用 dfs求最短路径(剪枝有点依稀)的相关文章

HDU 2616 Kill the monster (暴力搜索 || 终极暴力全排列)

题目链接:HDU 2616 Kill the monster 题意:有N个技能去打HP有M的怪兽,技能(A,M),技能伤害为A,当怪兽HP<=M时伤害为2*A.求打死怪兽(HP<=0)用的最少技能 方法一:将技能全排列,计算伤害,得到答案. 方法二:搜索,具体看代码. 全排列AC代码: #include<stdio.h> #include<algorithm> using namespace std; struct node { int p,v; }; struct n

hdu 4876 暴力剪枝

hdu 4876 终于过了, 之前写的代码虽然思路是这样的但是有好多可以优化的地方没有注意所以一直超时超时超时!,学习了一下别人的代码,虽然看上去没什么差别但实际上却可以节省很多时间,恩恩又学到了一些技巧~     ^_^ . [题意]:给定一些卡片,每个卡片上有数字,现在选k个卡片,绕成一个环,每次可以再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个L,问能组成[L,R]所有数字的情况下,R的最大值是多少. [思路]:暴力+剪枝  枚举在m个数里选k个数的 C(m,k)种情况,

HDU 2544:最短路( 最短路径入门 &amp;&amp;Dijkstra &amp;&amp; floyd )

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30972    Accepted Submission(s): 13345 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

hdu 1007 Quoit Design 分治求最近点对

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29344    Accepted Submission(s): 7688 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

DFS入门之二---DFS求连通块

用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所在位置相邻(上下左右对角共八个方位),则在一个连通块.典型例题:HDU 1241 Oil Deposits 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目描述:输入m行n列的字符矩阵, 统计字符“@”组成八连块的个数. 题意分析:读入数据

HDU 1269 迷宫城堡(DFS)

迷宫城堡 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间.Gardon需要请你写个程序确认一下是否任意两个房间都是相互连通的,即:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i. Input 输入包含多

hdu 3746 Cyclic Nacklace (KMP求最小循环节)

//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; char a[100010]; int next[100010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[i]==a[j]) i

hdu 2262 高斯消元求期望

Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1070    Accepted Submission(s): 298 Problem Description After a long drastic struggle with himself, LL decide to go for some

poj 1011 Sticks 【DFS】+【剪枝】

题意:有未知根(长度一样)木棒(小于等于n),被猪脚任意的截成n段,猪脚(脑抽了)想知道被截之前的最短长度(我推测猪脚得了健忘症). 这道题光理解题意就花了好久,大意就是任意根被截后的木棒拼到一起,能不能组成s(<=n)根的相同的木棒, 例:数据 9  5 1 2 5 1 2 5 1 2 可以组成最短为6 的(5+1, 2+2+2)3根木棒. 策略:深搜. 不过要是传统的深搜的话,TLE妥妥的.所以要剪枝(所谓剪枝,就是多加几个限制条件). 话不多说直接上代码. 代码1: #include <