Directory
1 Problem Description
2 Solutions
1 problem description Problem Description
Humans have discovered a new metal on Mars! These metals are distributed in strange places, so call it a node. Some of the nodes are connected by roads, and all the nodes and roads form a tree. There are altogether n nodes, these nodes are numbered 1~n. Humans have sent K robots to Mars for the purpose of acquiring these metals. These robots were sent to a designated landing point, the S number node. After each robot is landed, it must walk along the road. When the robot reaches a node, it collects all the metal deposits in the node. When the robot completes its task, it can return to Earth from any node. Of course, a robot returning to Earth could no longer go to Mars. We've measured the information for each road ahead of time, including its two endpoints x and Y, and the energy it takes to spend on the road. We want to spend as little energy as possible on all nodes of metal, and this task is yours.
Input Format
The first line contains three integers n, S and K, each representing the number of nodes, the number of landing points, and the number of robots.
The next line is n-1, each line describing a road. A row contains three integers x, y, and W, representing a path between the X-node and the Y-node, which takes the energy of W units. All roads can be used in both directions.
output Formatoutputs an integer that represents the minimum amount of energy required to acquire the metal for all nodes. Sample Input6 1 3
1 2 1
2 3 1
2 4
2 5
1 6Sample Output3004Sample Description
All robots landed at node 1th.
The first robot's walking path is 1->6, which returns to Earth at number 6th and spends 1000 of its energy.
The second robot's walking path is 1->2->3->2->4, which returns to Earth at number 4th and spends 1003 of its energy.
The first robot's walking path is 1->2->5, which returns to Earth at number 5th and spends 1001 of its energy.
data size and conventions
There are 10 test points.
For test points, N <=, K <= 5.
For test point 3, n <= 100000, k = 1.
For test point 4, n <=, k = 2.
For test point 5~6, N <=, K <= 10.
For test point 7~10, n <= 100000, K <= 10.
The energy W of the road is a positive integer not exceeding 1000.
2 Solutions
The following code in the Blue Bridge System evaluation score of 50 points, with the same thought version of the C code to evaluate to 100 (PS: Specific reference text at the end of reference 1).
The specific code is as follows:
Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMain { Public Static intN, S, K; Public StaticArraylist<edge>[] map; Public Static int[] DP; Static classEdge { Public intA; Public intb; Public intv; Edge (intAintBintv) { This. A =A; This. B =b; This. v =v; } } Public voidDfsintStartintfather) { for(inti = 0;i < Map[start].size (); i++) {Edge to=Map[start].get (i); if(TO.B = =father)Continue; DFS (TO.B, start); for(intj = K;j >= 0;j--) {Dp[start][j]+ = Dp[to.b][0] + to.v * 2; for(intm = 1;m <= j;m++) {Dp[start][j]=math.min (Dp[start][j], dp[start][j-M] + dp[to.b][m] + to.v *m); }}} @SuppressWarnings ("Unchecked") Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); N=In.nextint (); S=In.nextint (); K=In.nextint (); Map=NewArraylist[n + 1]; DP=New int[n + 1] [k + 1]; for(inti = 1;i <= n;i++) Map[i]=NewArraylist<edge>(); for(inti = 1;i < n;i++) { intx =In.nextint (); inty =In.nextint (); intW =In.nextint (); Map[x].add (NewEdge (x, Y, W)); Map[y].add (NewEdge (Y, X, W)); } Test.dfs (S,-1);//start the DFS traversal at the root node s, and the end of the trace is the vertex sSystem.out.println (Dp[s][k]); }}
Resources:
1. Blue Bridge cup algorithm improvement-metal collection
Algorithm Note _166: Algorithm improves metal acquisition (Java)