Two
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1092 | Accepted: 527 |
Description
The city consists of intersections and streets that connect them.
Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections.
The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection.
Write a program that calculates the total amount of fuel that the snow plovers will spend.
Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections.
The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection.
Write a program that calculates the total amount of fuel that the snow plovers will spend.
Input
The first line of the input contains two integers: N and S, 1 <= N <= 100000, 1 <= S <= N. N is the total number of intersections; S is ordinal number of the snow plovers starting intersection. Intersections are marked with numbers 1...N.
Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000.
Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000.
Output
Write to the output the minimal amount of fuel needed to clean all streets.
Sample Input
5 2 1 2 1 2 3 2 3 4 2 4 5 1
Sample Output
6
Source
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<vector> 6 #include<queue> 7 using namespace std; 8 9 vector<int>Q[100002]; 10 vector<int>val[100002]; 11 int num[100002]; 12 bool use[100002]; 13 int dp[100002][3]; 14 15 void add(int x,int y,int c) 16 { 17 Q[x].push_back(y); 18 val[x].push_back(c); 19 num[x]++; 20 } 21 int min(int x,int y) 22 { 23 return x>y ? y:x; 24 } 25 void dfs(int k) 26 { 27 int i,j,s,t,cur; 28 use[k]=true; 29 for(i=0;i<num[k];i++) 30 { 31 t=Q[k][i]; 32 if(use[t]==true)continue; 33 dfs(t); 34 for(j=2;j>=0;j--) 35 { 36 dp[k][j]=dp[k][j]+2*val[k][i]+dp[t][0]; 37 for(s=1;s<=j;s++) 38 { 39 dp[k][j]=min(dp[k][j],dp[k][j-s]+dp[t][s]+s*val[k][i]); 40 } 41 } 42 } 43 } 44 int main() 45 { 46 int n,s,i; 47 int x,y,c; 48 while(scanf("%d%d",&n,&s)>0) 49 { 50 for(i=0;i<=n;i++) 51 { 52 Q[i].clear(); 53 val[i].clear(); 54 num[i]=0; 55 use[i]=false; 56 } 57 memset(dp,0,sizeof(dp)); 58 for(i=1;i<n;i++) 59 { 60 scanf("%d%d%d",&x,&y,&c); 61 add(x,y,c); 62 add(y,x,c); 63 } 64 dfs(s); 65 printf("%d ",dp[s][2]); 66 } 67 return 0; 68 }