题目:
Two
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1554 | Accepted: 803 |
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
给定一棵树 两个人从同一点出发,要将所有边都遍历到,遍历过后不一定回到同一个点 每走一米花费一单位的油 问最少耗油量
分析:
画图瞎比划(+看题解)得 无论两个人一开始在哪一点,若想遍历所有的边,则除了一条链上的边只需要走一次,其他边必然要走两次
同时要求边权和最小,那么空出来的那条链必然是最长链 即树的直径
所以答案是边权总数*2-数的直径
代码:
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
const int maxn=1e5+20;
const int inf=0x3f3f3f3f;
struct edge{
int t,w;
edge(int tt=0,int ww=0):t(tt),w(ww){}
};
vector<edge> G[maxn];
int n,m,d[maxn];
//bool vis[maxn];
//int cnt;
void bfs(int s){
for(int i=0;i<=n;++i) d[i]=inf;
queue<int> q;
q.push(s);
d[s]=0;
int u;
while(!q.empty()){
u=q.front(); q.pop();
for(int i=0;i<G[u].size();++i){
edge e=G[u][i];
if(d[e.t]==inf){
d[e.t]=d[u]+e.w;
q.push(e.t);
}
}
}
}
int solve(){
bfs(m);//任选一个节点出发 如果下标是从1开始的不能bfs(0).....!!!
int maxv=0;
int maxi=0;
for(int i=0;i<=n;++i){
if(d[i]==inf) continue;
if(maxv<d[i]){
maxv=d[i];
maxi=i;
}
}
bfs(maxi);
maxv=0;
for(int i=0;i<=n;++i){
if(d[i]==inf) continue;
maxv=max(maxv,d[i]);
}
return maxv;
}
int main(){//2280K 188MS
int a,b,c;
int ans;
while(scanf("%d%d",&n,&m)==2){
for(int i=0;i<=n;++i) G[i].clear();
ans=0;
for(int i=1;i<n;++i){
scanf("%d%d%d",&a,&b,&c);
//cout<<a<<" "<<b<<" "<<c<<" "<<s<<endl;
G[a].push_back(edge(b,c));
G[b].push_back(edge(a,c));
ans+=c;
}
printf("%d\n",ans+ans-solve());
}
return 0;
}