The Priest

Source : 计算机学院第二届“光熙杯”程序设计大赛

Time limit : 3 sec Memory limit : 32 M

Submitted : 186, Accepted : 51

Recently bailey playes a role in the game “world of warcraft”.It is a priest. The priest has n skills to heal the friends. Each skill costs different mana and adds different HP to the friends. When fighting with enemies, bailey wants to spend the minimal mana keeping the friend alive(If the friend’s HP<=0,he will die).But he doesn’t know how much is enough.He asks you for help.Let us consider a simple condition:

The priest has infinite mana but can only heal the friends once after a hurt.

Input

The first line of the input file contains a single integer t , the number of test cases, followed by the input data for each test case.

Each test case countains 2 lines as follow:

n m1 h1 m2 h2……mn hn

HP k d1 d2……dk

n gives the number of skills. mi hi means that the ith skill will cost the priest mi mana and add hi HP to the friend.

HP gives the max HP the friend has and in the beginning it’s full. k gives the number of hurt. di means the ith hurt will cost the friend di HP.( 0 < n <= 5 , 0 < mi <= 100 , 0 < HP <= 1000, 0 < k <= 100)

Output

One line for each case. If after k hurts the friend can be alive, output the minimal mana the priest can spend keeping the friend alive.Otherwise output “Lose the friend.”

Sample Input

2

2 1 4 2 10

15 2 8 11

2 1 4 2 10

15 2 14 12

Sample Output

2

Lose the friend.

哎这道题目我wa了19次,因为一条件忽视了吧。多想想,状态转移方程还是比较容易想通的

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
#define MAX 10000000
int dp[105][1005];
int v[10];
int w[10];
int a[105];
int tag[1005];
int hp;
int n,k;
int sum;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&v[i],&w[i]);
}
scanf("%d%d",&hp,&k);
bool live=true;
for(int i=1;i<=k;i++)
{
scanf("%d",&a[i]);
if(a[i]>hp)
live=false;
}
if(!live)
{
printf("Lose the friend.\n");
continue;
}
for(int i=0;i<=101;i++)
for(int j=0;j<=1001;j++)
dp[i][j]=MAX;
a[0]=0;
dp[0][hp]=0;
memset(tag,0,sizeof(tag));
for(int i=1;i<=k;i++)
{
memset(tag,0,sizeof(tag));
for(int j=1;j<=hp;j++)
{
if(j==hp)
{
for(int p=1;p<j;p++)
{
if(dp[i][p]==MAX)
continue;
if(tag[p]==1)
continue;
for(int q=1;q<=n;q++)
{
if(p+w[q]>=hp)
dp[i][hp]=min(dp[i][hp],dp[i][p]+v[q]);
}
} }
if(j+a[i]<=hp&&dp[i-1][j+a[i]]!=MAX)
dp[i][j]=min(dp[i][j],dp[i-1][j+a[i]]);
for(int q=1;q<=n;q++)
{
if(j>w[q]&&!tag[j-w[q]]&&dp[i][j]>dp[i][j-w[q]]+v[q])
{
dp[i][j]=dp[i][j-w[q]]+v[q];
tag[j]=1;
}
} } }
int ans=MAX;
for(int j=1;j<=hp;j++)
{
ans=min(ans,dp[k][j]);
}
if(ans==MAX)
printf("Lose the friend.\n");
else
printf("%d\n",ans);
}
return 0;
}

HOJ 2252 The Priest(动态规划)的更多相关文章

  1. HOJ 2133&POJ 2964 Tourist(动态规划)

    Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...

  2. HOJ 2139 Spiderman's workout(动态规划)

    Spiderman's workout My Tags (Edit) Source : Nordic Collegiate Programming Contest 2003 Time limit : ...

  3. HOJ 13845 Atomic Computer有向无环图的动态规划

    考虑任意一个数字,任何一个都会有奇怪的..性质,就是一个可以保证不重复的方案——直接简单粗暴的最高位加数字..于是,如同上面的那个题:+1.-1.0 但是考虑到65536KB的标准内存限制,会得出一个 ...

  4. HOJ 2124 &POJ 2663Tri Tiling(动态规划)

    Tri Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9016 Accepted: 4684 Descriptio ...

  5. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

  6. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  7. [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总

    本文出自   http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner  打开 这个专题一共有25题,刷完 ...

  8. HOJ 1402 整数划分

    HOJ1402 整数划分 http://acm.hit.edu.cn/hoj/problem/view?id=1402 [题目描述] 整数划分是一个经典的问题.希望这道题会对你的组合数学的解题能力有所 ...

  9. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

随机推荐

  1. 第二百八十九节,MySQL数据库-ORM之sqlalchemy模块操作数据库

    MySQL数据库-ORM之sqlalchemy模块操作数据库 sqlalchemy第三方模块 sqlalchemysqlalchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API ...

  2. ZooKeeper的架构

    ZooKeeper的架构 看看下面的图表.它描述了ZooKeeper的“客户端-服务器架构”. 作为ZooKeeper架构的一部分的每个组件在下表中进行了说明. 部分 描述 Client(客户端) 客 ...

  3. BinarySearchTree二叉搜索树的实现

    /* 二叉搜索树(Binary Search Tree),(又:二叉查找树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; ...

  4. Nginx(三)-- 配置文件之日志管理

    1.日志文件的默认存放位置 默认的日志文件存放位置在:nginx/logs/ 文件夹下,logs文件夹下有:access.log   error.log   nginx.pid 文件 2.nginx. ...

  5. N76E003学习之路(二)

    最近一直在想N76E003和STM8M003的对比情况,在网上找了不少资料,看了不少文档,具体总结如下: STM8S003F3P6:一共20个脚,最多支持16个GPIO,支持16个外部中断:2个16位 ...

  6. PHP常用必备函数

    array_change_key_case — 返回字符串键名全为小写或大写的数组 array_chunk — 将一个数组分割成多个 array_combine — 创建一个数组,用一个数组的值作为其 ...

  7. bootstrap里面的popover组件如何使鼠标移入可以对弹出框进行一系列的操作

    在bootstrap里面,有一个组件很可爱,它就是popover,它是对标签title属性的优化,奉上连接一枚:http://docs.demo.mschool.cn/components/popov ...

  8. 在js中通过call或者apply实现继承

    通过call或者apply可以实现函数里面this的改变,利用这一特点,可以实现继承 代码如下所示: /*父类*/ function Parent(add,net,no,teacher) { this ...

  9. IntersectRect、wcsrchr、CComPtr、GetFileAttributes

    IntersectRect    两矩形相交形成的新矩形 The IntersectRect function calculates the intersection of two source re ...

  10. Visual Studio 2013 如何在停止调试Web程序后阻止IIS Express关闭

    vs2013 调试项目的时候,当停止调试的时候,端口就被断了.之前以为是IIS那边的控制问题,但是其他并行的项目运行都没有出现这种情况. 最初也没在意,直到现在实在忍受不了了,每次重开也太烦了.就去各 ...