• HDU 4276 The Ghost Blows Light 第37届ACM/ICPC长春赛区1010题 (树形DP)


    The Ghost Blows Light

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


    Problem Description

    My name is Hu Bayi, robing an ancient tomb in Tibet. The tomb consists of N rooms (numbered from 1 to N) which are connected by some roads (pass each road should cost some time). There is exactly one route between any two rooms, and each room contains some treasures. Now I am located at the 1st room and the exit is located at the Nth room.
    Suddenly, alert occurred! The tomb will topple down in T minutes, and I should reach exit room in T minutes. Human beings die in pursuit of wealth, and birds die in pursuit of food! Although it is life-threatening time, I also want to get treasure out as much as possible. Now I wonder the maximum number of treasures I can take out in T minutes.
     
    Input
    There are multiple test cases.
    The first line contains two integer N and T. (1 <= n <= 100, 0 <= T <= 500)
    Each of the next N - 1 lines contains three integers a, b, and t indicating there is a road between a and b which costs t minutes. (1<=a<=n, 1<=b<=n, a!=b, 0 <= t <= 100)
    The last line contains N integers, which Ai indicating the number of treasure in the ith room. (0 <= Ai <= 100)
     
    Output
    For each test case, output an integer indicating the maximum number of treasures I can take out in T minutes; if I cannot get out of the tomb, please output "Human beings die in pursuit of wealth, and birds die in pursuit of food!".
     
    Sample Input
    5 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
     
    Sample Output
    11
     
    Source
     
    Recommend
    liuyiding
     
    比较典型的树形DP题。比赛时没有做出来,还要加强下树形DP;
    思路就是先找出1到N的路径,然后将这些路径的时间置0,之后进行树形DP
    /*
    树形DP
    先找出1到N的边,将这些边的时间修改为0.然后就是简单的树形DP了
    */
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int MAXN=110;
    
    struct Node
    {
        int next;
        int to;
        int val;
    }edge[MAXN*2];
    int tol;
    int head[MAXN];
    
    int dp[MAXN][550];//dp[i][j]表示从第i个点开始,回到i点,花费j时间得到的最大财富值
    
    int value[MAXN];//每个点的财富值
    
    int time1;//从1到N需要的时间
    int n;
    
    void init()
    {
        tol=0;
        memset(head,-1,sizeof(head));
    }
    
    void add(int a,int b,int val)
    {
        edge[tol].to=b;
        edge[tol].next=head[a];
        edge[tol].val=val;
        head[a]=tol++;
        edge[tol].to=a;
        edge[tol].next=head[b];
        edge[tol].val=val;
        head[b]=tol++;
    }
    
    bool dfs1(int u,int pre)
    {
        if(u==n)return true;//找到了
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(v==pre)continue;
            if(dfs1(v,u))
            {
                time1+=edge[i].val;
                edge[i].val=0;
                return true;
            }
        }
        return false;
    }
    int t;
    void dfs2(int u,int pre)
    {
        for(int i=0;i<=t;i++) dp[u][i]=value[u];
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(v==pre)continue;
            dfs2(v,u);
            int cost=edge[i].val*2;//要走两遍
            for(int i=t;i>=cost;i--)
               for(int j=0;j<=i-cost;j++)
                 dp[u][i]=max(dp[u][i],dp[v][j]+dp[u][i-j-cost]);
        }
    }
    int main()
    {
       // freopen("in.txt","r",stdin);
       // freopen("out.txt","w",stdout);
        int u,v,w;
        while(scanf("%d%d",&n,&t)!=EOF)
        {
            init();
            for(int i=1;i<n;i++)
            {
                scanf("%d%d%d",&u,&v,&w);
                add(u,v,w);
            }
            for(int i=1;i<=n;i++)scanf("%d",&value[i]);
            time1=0;
            dfs1(1,-1);//找从1到N的最短时间
            if(t<time1)
            {
                printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
                continue;
            }
            t-=time1;
            dfs2(1,-1);
            printf("%d\n",dp[1][t]);
        }
        return 0;
    }
  • 相关阅读:
    fastjson的@JSONField注解
    Java 日期比较大小
    linux 查看文件显示行号
    Java double 加、减、乘、除
    Java 身份证判断性别获取年龄
    linux 查看端口被占用
    Unable to complete the scan for annotations for web application [/wrs] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies.
    nginx 返回数据不完整
    linux redis 启动 overcommit_memory
    IntelliJ IDEA 常用设置
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2679874.html
Copyright © 2020-2023  润新知