zoukankan      html  css  js  c++  java
  • ZOJ Problem Set 3626 Treasure Hunt I

    ZOJ Problem Set - 3626
    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

    Sample 1: CC can go to town 2 and return at day 2.
    Sample 2: CC can't come back within 1 day. So he can only take the treasure in his hometown.
    Sample 3: CC only need 2 days to collect all the treasure.

    //树状dp+01背包

    //dp[k][j]表示以k的根的树用j天最多可以得到多少宝藏


    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #define N 102 using namespace std; struct Edge { int to,next; }ed[N<<1]; int vc[N]; int T[N][N]; bool visit[N]; int dp[N][N]; int val[N]; int m; void dfs(int k) { visit[k]=1; int i,j,vx,e; for(e=vc[k];;e=ed[e].next) { vx=ed[e].to; if(!visit[vx]) { dfs(vx); for(j=m;j>=T[k][vx];j--) for(i=0;i<=j-T[k][vx];i++) { dp[k][j]=max(dp[k][j],dp[k][j-T[k][vx]-i]+dp[vx][i]); // printf("t %d %d %d\n",dp[k][j],k,j); } } if(ed[e].next==-1) return; } } int main() { int n,k; int i,j,u,v; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) scanf("%d",&val[i]); memset(vc,-1,sizeof(vc)); memset(visit,0,sizeof(visit)); for(j=i=1;i<n;i++) { scanf("%d%d",&u,&v); scanf("%d",&T[u][v]); T[v][u]=T[u][v]; ed[j].to=v; ed[j].next=vc[u]; vc[u]=j++; ed[j].to=u; ed[j].next=vc[v]; vc[v]=j++; } scanf("%d%d",&k,&m); m=m/2; for(i=1;i<=n;i++) { //dp[i][0]=val[i]; for(j=0;j<=m;j++) dp[i][j]=val[i]; } dfs(k); printf("%d\n",dp[k][m]); } return 0; }
  • 相关阅读:
    leetcode -- Add Binary
    leetcode -- 4sum
    leecode -- 3sum Closet
    C++单例模式
    MapReduce的模式、算法和用例
    react 学习笔记
    css3 文字过长用...代替
    scss/css 中添加ie hack
    springmvc 解决跨域CORS
    springmvc 添加Junit4
  • 原文地址:https://www.cnblogs.com/372465774y/p/2623131.html
Copyright © 2011-2022 走看看