Blue Bridge: metal collection algorithms to improve

  Algorithm to improve the collection of metal   

Problem Description

Humans discovered a new metal on Mars! These metals distribution in some strange places, you might call it good node. There are a number of connected road between nodes, and all nodes form a tree path. A total of n nodes, the nodes are numbered 1 ~ n. The human k robots sent to Mars, the purpose is to collect these metals. These robots are sent to a specific drop point, S node number. Each robot after landing, must walk along the road. When the robot arrives at a node, it will collect all the metal ore deposits of this node. When the robot to complete their task, a node can from any return to Earth. Of course, the robot will not be able to return to Earth and then go to Mars. We've already measured information of each road, including its two endpoints x and y, w and energy through this path it takes. We want to spend as little energy harvesting metal of all nodes, this task is given to you.

Input Format

The first line contains three integers n, S and k, representing the number of nodes, the impact point number, and the number of robots.

Next, a total of n-1 lines, each describing a road. Line contains three integers x, y and w, representatives of a number of road between nodes x and y node number, takes energy through w units. All roads are two-way traffic.

Output Format

Output an integer representing the minimum acquisition power metal required for all nodes.

Sample input

6 1 3
1 2 1
2 3 1
2 4 1000
2 5 1000
1 6 1000

Sample Output

3004

Sample Description

All robots landing at No. 1 node.

A robot travel path of 1-> 6, return to earth in the node 6, 1000 to spend energy.

A second traveling path of the robot is 1-> 2-> 3-> 2-> 4, the node 4 back to Earth, the energy cost is 1003.

A robot travel path of 1-> 2-> 5, node returns earth 5, 1001 to spend energy.

Data size and convention

This question has 10 test points.

For the test point 1 ~ 2, n <= 10, k <= 5.

For the test point 3, n <= 100000, k = 1.

For the test point 4, n <= 1000, k = 2.

For the test point 5 ~ 6, n <= 1000, k <= 10.

For the test point 7 ~ 10, n <= 100000, k <= 10.

Energy w roads are no more than a positive integer 1000.

Ideas: d (i, j) represents the j-th minimum cost to the robot i is the root node in the subtree.

           V is a sub-set of node u, i from node k using robots to collect energy root node v,

           State transition equation is d (u, i) = min (d (u, i - k) + d (v, k) + cost * k) 1 <= k <= i.

          Note that d (u, i - k) is represented by i - minimum cost of energy to collect other subtree k robot,

          After traversing all the child nodes, the value of d (u, i) is fixed only.

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 5;
int dp[maxn][12];  
struct node{
	int son, cost; 
	node() {
	}
	node(int son, int cost):son(son), cost(cost) {
	}
};
vector<node>road[maxn]; 
int n, s, cnt; 
void dfs(int u, int pre) { //u-当前节点,pre-它的父节点 
	int n = road[u].size();
	for(int i = 0; i < n; ++i) {
		int v= road[u][i].son, cost = road[u][i].cost;
		if(v == pre) continue;
		dfs(v, u);
		// 只考虑当前节点和已经发现的节点 
		for(int k = cnt; k >= 0; --k) { //该子树停留的机器人总数
		//逆序枚举--如果d(u, k)更新,那么不能影响d(u,k+1),如果顺序枚举会影响  
			dp[u][k] += dp[v][0] + cost * 2; //让一个机器人遍历该子树所有子节点并返回的花费 
			for(int j = 1; j <= k; ++j) {    //至少一个机器人进入子树 
				dp[u][k] = min(dp[u][k], dp[u][k-j] + dp[v][j] + j * cost);
			}
		}
 	} 
}
 
int main() { 
	while(scanf("%d%d%d", &n, &s, &cnt) == 3) {
		memset(dp, 0, sizeof(dp));
		for(int i = 0; i <= n; ++i) road[i].clear();
		int x, y, cost;
		for(int i = 1; i < n; ++i) {
			scanf("%d%d%d", &x, &y, &cost);
			road[x-1].push_back(node(y-1, cost));
			road[y-1].push_back(node(x-1, cost));
		}
		dfs(s - 1, -1);
		printf("%d\n", dp[s - 1][cnt]); 
	}
	return 0;
}


 

Published 736 original articles · won praise 123 · views 80000 +

Guess you like

Origin blog.csdn.net/S_999999/article/details/103298605