Blue Bridge Cup algorithm overstating metal acquisition [tree DP Classic]

Source: Internet
Author: User

Transmission Door

Algorithm improves metal acquisition

Time limit: 1.0s memory limit: 256.0MB 1 brocade sac 2 Jin sac 3 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.

The output format outputs an integer that represents the minimum amount of energy required to acquire the metal 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 Example 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.

Exercises

Ideas from the blog: http://www.ylzx8.cn/kaifayuyan/program/192149.html

Dp[p][m]: Represents the cost of stopping M robots in a subtree that is rooted in P. think of a subtrees tree as a whole.

Very good tree-shaped DP, understand for a long time. Be sure to consider a subtrees number as a whole.

At first, Dfs just to a node, if there is no son node, then the robot can be stopped, Dp[p][m] 0

If there is a son node found, then consider staying at the son node remain a robot.

dp[p][k]+=dp[next][0]+cost*2; The subtree does not have a stop in the robot, so it means full reverse, at least go to one so at least back a

for (remain=1;remain<=k;remain++)

Dp[p][k]=min (Dp[p][k],dp[p][k-remain]+dp[next][remain]+remain*cost);　　　　　　                                                                                                              Note: Dfs to the first son, because temporarily did not find the remaining sons, so stay on the P node machine People are not going to have to spare.

As DFS progresses, every time a new son is found, the update takes into account the state of all previous sons. This is a gradual process.

For example, when the second son, K=1,remain=1,

Dp[p][k]=min (Dp[p][k],dp[p][k-remain]+dp[next][remain]+remain*cost);

Dp[p][k-remain] is no longer 0, because the first son must first be visited before returning to the P node.

After all the sons have been DFS, the resulting dp[p][m] no longer changes.

 518169 [Email protected] Metal Collection 04-08 20:17 1.269KB C++ That's right 100 171ms 13.42MB Evaluation details

1 Evaluation Point serial number2 3 Evaluation Results4 5 score6 7 CPU Usage8 9 Memory UsageTen  One Download Evaluation Data A  - 1That's right10.0015ms6. 949MB input and Output -  2That's right10.000ms6. 949MB VIP Privileges the  3That's right10.00140ms9. 468MB VIP Privileges -  4That's right10.000ms6. 972MB VIP Privileges -  5That's right10.000ms6. 972MB VIP Privileges -  6That's right10.000ms7. 015MB VIP Privileges +  7That's right10.00156ms9. 472MB VIP Privileges -  8That's right10.00171ms9. 472MB VIP Privileges +  9That's right10.00171ms -. 42MB VIP Privileges A  TenThat's right10.00171ms9.468MB VIP Privileges
Evaluation Details

1#include <cstdio>2#include <cmath>3#include <vector>4#include <cstring>5 6 #defineN 1000057 8 using namespacestd;9 Ten intn,s,k; One intdp[n][ A];//Dp[p][m]: Represents the cost of stopping M robots in a subtree that is rooted in P. Think of a subtrees tree as a whole.  A  -typedefstruct - { the     intto ; -     intW; - }pp; -  +Vector<pp>Bian[n]; - intVis[n]; +  A voidDfsintp) at { -vis[p]=1; -     inti; -     intNext; -     intCost ; -     intK; in     intremain; -      for(i=0; I<bian[p].size (); i++) to     { +next=bian[p][i].to; -         if(vis[next]==1)Continue; thecost=BIAN[P][I].W; * Dfs (next); \$          for(k=k;k>=0; k--) {//you should be careful not to lose 0 of the situation here.Panax Notoginsengdp[p][k]+=dp[next][0]+cost*2;//The subtree does not have a stop in the robot, so it means full reverse, at least go to one so at least back a -              for(remain=1; remain<=k;remain++) {//Stop Raim A robot in Son tree son. theDp[p][k]=min (dp[p][k],dp[p][k-remain]+dp[next][remain]+remain*Cost ); +             } A         } the     } + } -  \$ intMain () \$ { -     intx,y,w; - PP te; the     //freopen ("data.in", "R", stdin); -Memset (DP,0,sizeof(DP));Wuyimemset (Vis,0,sizeof(Vis)); the     inti; -scanf"%d%d%d",&n,&s,&K); Wu      for(i=0; i<=n;i++){ - bian[i].clear (); About     }     \$      for(i=0; i<n-1; i++){ -scanf"%d%d%d",&x,&y,&W); -te.w=W; -te.to=y; A bian[x].push_back (TE); +te.to=x; the bian[y].push_back (TE); -     } \$ //printf ("SDF a\n"); the Dfs (s); theprintf"%d\n", Dp[s][k]); the     return 0; the}

Blue Bridge Cup algorithm overstating metal acquisition [tree DP Classic]

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.