前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HOJ 2252 The Priest(动态规划)

HOJ 2252 The Priest(动态规划)

作者头像
ShenduCC
发布2018-04-26 12:01:35
5130
发布2018-04-26 12:01:35
举报
文章被收录于专栏:算法修养算法修养

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次,因为一条件忽视了吧。多想想,状态转移方程还是比较容易想通的

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档