Two
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1473 | Accepted: 748 |
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
题意:
给出一个n个点和n-1条边,从源点S出发,求花费最少
题解:
在草稿纸上模拟一下就知道了最少花费等于总权值的两倍减去树种的最长路径
最长路径可以用树形dp求解:
dp[i][0]表示第一个工人以i为节点的子树最大路程
dp[i][1]表示第二个工人以i为节点的子树最大路程
因为最长路径肯定是以某一个节点为中心开始搜索最长
if(dp[u][0]<dp[v][0]+w){
dp[u][1]=dp[u][0];
dp[u][0]=dp[v][0]+w;
}else if(dp[u][1]<dp[v][0]+w)
dp[u][1]=dp[v][0]+w;
这些个式子就是表示先尽量让第一个工人走最远,然后让第二个工人走最远,加起来就是最长的
所以这个题可以依照这个模型写出K个工人的树形dp
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 100010
int n,s,cnt;
struct edge
{
int v,w;
int next;
}a[MAXN];
int head[MAXN];
int dp[MAXN][2];
void add(int u,int v,int w)
{
a[cnt].v=v;
a[cnt].w=w;
a[cnt].next=head[u];
head[u]=cnt++;
}
int ans;
void dfs(int u,int last)
{
dp[u][0]=dp[u][1]=0;
for(int i=head[u];i!=-1;i=a[i].next){
int v=a[i].v;
int w=a[i].w;
if(v!=last){
dfs(v,u);
if(dp[u][0]<dp[v][0]+w){
dp[u][1]=dp[u][0];
dp[u][0]=dp[v][0]+w;
}
else if(dp[u][1]<dp[v][0]+w)
dp[u][1]=dp[v][0]+w;
}
}
ans=max(ans,dp[u][1]+dp[u][0]);
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&s)!=EOF)
{
ans=cnt=0;
int sum=0;
int x,y,z;
memset(head,-1,sizeof(head));
for(int i=1;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
sum+=2*z;
}
dfs(s,-1);
printf("%d\n",sum-ans);
}
return 0;
}