• ZOJ 3626 Treasure Hunt I(树形dp)


    Treasure Hunt I

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named CC finds a treasure map, and he wants to get as much as possible.

    Akiba consists of n towns and n-1 roads. There is a way from each town to any other. Each town contains some treasure values Vi. CC starts from town k(his hometown), at day 0. After m days, the bloodsucker will appear and CC would be killed if he hasn't been back yet, it means CC has m days for hunting the treasure at most. It takes CC Ti days to move from one town to another neighbour town.(Two towns called neighbour if they are the endpoint of one road.) You can assume CC will get the treasure immediately as he arrives at that town. CC wants to obtain as much value as possible, keeping him alive at the same time.

    Input

    There are multiple cases, about 50 cases.
    The first line of each case contains an integer n, indicating there are n towns.
    The following line describe the treasure's value in each town. "V1 V2 ... Vn". Vi is the value of the treasure in ith town. Each value is separated by one blank.
    The next n-1 lines describe the n-1 roads in Akiba. "i j Ti" Means the ith town and the jth town are endpoints of that road. It takes Ti days to get through this road.
    The last line has two integer k and m as described above.

    1<=n<=100, 0<=Vi<=1000 , 1<=Ti<=10
    1<=k<=n, 1<=m<=200
    All the inputs are integers.

    Output

    Just output the max value CC can get, and you should keep CC alive after m days.

    Sample Input

    2
    1 3
    1 2 1
    1 2
    2
    1 3
    2 1 1
    2 1
    2
    3 3
    1 2 1
    2 5

    Sample Output

    4
    3
    6

    Hint



    题意:给一棵有n个结点的树。每一个点有点权表示在这个点上的价值。每条边有边权表示走这条路所须要的时候,给一个时间m。问在时间m从点k出发再回到点k所能得到的最大的价值和。

    题解:dp[i][j]:表示以i为根的子树花费j能达到的最大值。

    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #define ll long long
    #define N 110
    
    using namespace std;
    
    struct Edge {
        int to,cost;
    } ;
    
    vector<Edge>G[N];
    int val[N],dp[N][N];
    int n,m,k;
    
    void Addedge(int u,int v,int c) {
        Edge it;
        it.to=v;
        it.cost=c;
        G[u].push_back(it);
    }
    
    void dfs(int fa,int u) {
        dp[u][0]=val[u];
        for(int i=0; i<G[u].size(); i++) {
            int v=G[u][i].to;
            int w=G[u][i].cost;
            if(v==fa)continue;
            dfs(u,v);
            for(int j=m; j>=w; j--) {
                for(int p=0; p<=j-w; p++) {
                    dp[u][j]=max(dp[u][j],dp[v][p]+dp[u][j-w-p]);
                }
            }
        }
    }
    
    int main() {
        //freopen("test.in","r",stdin);
        while(cin>>n) {
            for(int i=0; i<N; i++)G[i].clear();
            for(int i=1; i<=n; i++) {
                scanf("%d",val+i);
            }
            for(int i=1; i<n; i++) {
                int u,v,c;
                scanf("%d %d %d",&u,&v,&c);
                Addedge(u,v,c);
                Addedge(v,u,c);
            }
            scanf("%d %d",&k,&m);
            m/=2;
            memset(dp,0,sizeof dp);
            dfs(-1,k);
            int ans=0;
            for(int i=0; i<=m; i++)
                ans=max(ans,dp[k][i]);
            cout<<ans<<endl;
        }
        return 0;
    }


  • 相关阅读:
    node.js--express学习之路(三)
    node.js--express学习之路(二)
    node.js--express学习之路(一)
    js简单实现获取验证码倒计时
    HTML5新特性postMessage解决跨域
    跨域通信—JSONP
    前端跨域的那些事
    前后端通信—CORS(支持跨域)
    前后端通信—webSocket(支持跨域)
    JavaScript的运行机制
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6794923.html
Copyright © 2020-2023  润新知