您的位置:首页 > 其它

ZOJ Problem Set - 3626 树形背包 水题

2016-01-05 12:18 260 查看
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 mdays, 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.
Author: LI, Chao
Contest: ZOJ Monthly, July 2012

题意:

 给一棵n个节点的树, 节点编号1~n, 每个节点有权值val[i],经过这个节点就可以获取这个价值(不能重复获得)
  每一条边有一个花费值w(i,j), 表示走完i和j点的边要花费w(i,j)
  现在要从k点出发,总花费值为m,问总花费不超过m的情况下并且最终要回到出发点,最多可以获取多少价值?

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
#define ysk(x)  (1<<(x))
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=100+10    ;
const int maxV=200+10   ;
int dp[maxn][maxV];
int n,root,V;
int val[maxn];
struct Edge
{
int from,to,w;
Edge(){}
Edge(int from,int to,int w): from(from),to(to),w(w) {}

};
vector<int >G[maxn];
vector<Edge>edges;

void init()
{
for(int i=1;i<=n;i++) G[i].clear();
edges.clear();

}
void add_edge(int x,int y,int w)
{
edges.push_back(Edge(x,y,w));
edges.push_back(Edge(y,x,w));
int m=edges.size();
G[x].push_back(m-2);
G[y].push_back(m-1);
}

void dfs(int x,int fa)
{
for(int v=0;v<=V;v++)   dp[x][v]= val[x];
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
int y=e.to;if(y==fa)  continue;
dfs(y,x);
int w=e.w;
for(int v=V;v>=0;v--)
{
for(int v2=0;v2+2*w<=v;v2++)
{
dp[x][v]=max(dp[x][v],dp[x][v-v2-2*w]+dp[y][v2]);
}
}

}
}

int main()
{
int x,y,w;
while(~scanf("%d",&n))
{
init();
for(int i=1;i<=n;i++) scanf("%d",&val[i]);
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&w);
add_edge(x,y,w);
}
scanf("%d%d",&root,&V);
dfs(root,-1);
printf("%d\n",dp[root][V]);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: