xtu summer individual 6 E - Find Metal Mineral

Find Metal Mineral

Time Limit: 1000ms

Memory Limit: 65768KB

This problem will be judged on HDU. Original ID: 4003
64-bit integer IO format: %I64d      Java class name: Main

Humans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots on Mars to collect them, and due to the unknown reasons, the landing site S of all robots is identified in advanced, in other word, all robot should start their job at point S. Each robot can return to Earth anywhere, and of course they cannot go back to Mars. We have research the information of all paths on Mars, including its two endpoints x, y and energy cost w. To reduce the total energy cost, we should make a optimal plan which cost minimal energy cost.

Input

There are multiple cases in the input. 
In each case: 
The first line specifies three integers N, S, K specifying the numbers of metal mineral, landing site and the number of robots. 
The next n‐1 lines will give three integers x, y, w in each line specifying there is a path connected point x and y which should cost w. 
1<=N<=10000, 1<=S<=N, 1<=k<=10, 1<=x, y<=N, 1<=w<=10000.

Output

For each cases output one line with the minimal energy cost.

Sample Input

3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1

Sample Output

3
2

Hint

In the first case: 1->2->1->3 the cost is 3; In the second case: 1->2; 1->3 the cost is 2;

Source

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

解题:传说中的树形dp。。。嗯 待会再讲吧。。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 struct arc {
17     int to,w;
18 };
19 vector<arc>g[10010];
20 int dp[10010][15],n,s,k;
21 void dfs(int u,int fa){
22     for(int i = 0; i < g[u].size(); i++){
23         int v = g[u][i].to;
24         if(v == fa) continue;
25         dfs(v,u);
26         for(int t = k; t >= 0; t--){
27             dp[u][t] += dp[v][0] + 2*g[u][i].w;
28             for(int j = 1; j <= t; j++)
29                 dp[u][t] = min(dp[u][t],dp[u][t-j]+dp[v][j]+j*g[u][i].w);
30         }
31     }
32 }
33 int main(){
34     while(~scanf("%d%d%d",&n,&s,&k)){
35         for(int i = 0; i <= n; i++)
36             g[i].clear();
37         for(int i = 1; i < n; i++){
38             int u,v,w;
39             scanf("%d %d %d",&u,&v,&w);
40             g[u].push_back((arc){v,w});
41             g[v].push_back((arc){u,w});
42         }
43         memset(dp,0,sizeof(dp));
44         dfs(s,-1);
45         printf("%d\n",dp[s][k]);
46     }
47     return 0;
48 }

xtu summer individual 6 E - Find Metal Mineral

时间: 2024-09-29 23:28:12

xtu summer individual 6 E - Find Metal Mineral的相关文章

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

xtu summer individual 6 D - Checkposts

Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 427C64-bit integer IO format: %I64d      Java class name: (Any) Your city has n junctions. There are m one-way roads between the junctions. A

xtu summer individual 5 D - Subsequence

Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 353064-bit integer IO format: %I64d      Java class name: Main There is a sequence of integers. Your task is to find the longest subsequence that sa

xtu summer individual 1 A - An interesting mobile game

An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o

xtu summer individual 2 C - Hometask

Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 154A64-bit integer IO format: %I64d      Java class name: (Any) Sergey attends lessons of the N-ish language. Each lesson he receives a hometas

xtu summer individual 5 F - Post Office

Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 116064-bit integer IO format: %lld      Java class name: Main There is a straight highway with villages alongside the highway. The highway is repres

xtu summer individual 5 A - To Add or Not to Add

To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 231C64-bit integer IO format: %I64d      Java class name: (Any) A piece of paper contains an array of n integers a1, a2, ..., an. Y

xtu summer individual 5 E - Burning Bridges

Burning Bridges Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 258864-bit integer IO format: %lld      Java class name: Main Ferry Kingdom is a nice little country located on N islands that are connected by