题目描述
POJ_1849 Two
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.
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.
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
链接
POJ链接: 点这里.
想法
想错了,一开始想的是,从s开始,使用两个机器人遍历s中两个最长的路线(bfs遍历,提前遍历子节点,并记录长度),
错的地方,如果S节点只有一个向下的节点,但是这个
节点会有很多枝,那就会浪费一个机器人。
我想的是,只有一个机器人该怎么跑,然后在根节点上
多减一个最远的就行。哎,,好菜好菜
正解:一个机器人从s出发,到一个直径的端点,并遍历沿途
的边,另一个机器人到另一个直径端点。
思维题,数学题
树形dp都差点忘了,就是 遍历i个节点Max(f1i , f2i)
什么时候需要父节点呢?
::就是需要求每个节点的最长路径之类的。
正解代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<utility>
using namespace std;
const int N = 1e5 + 1e3;
typedef long long int LL;
typedef pair<LL,LL> PLL;//to , val
vector<PLL> vec[N];
LL n , s , f1[N] , f2[N] , res = 0;
void dps(LL u , LL fat){
LL i , j , v , val;
for(i = 0 ; i < vec[u].size() ; i ++ ){
v = vec[u][i].first;
val = vec[u][i].second;
if(v == fat) continue;
dps(v , u );
if(f1[u] < f1[v] + val){
f2[u] = f1[u];
f1[u] = f1[v] + val;
}else if(f2[u] < f1[v] + val){
f2[u] = f1[v] + val;
}
res = max(res , f1[u] + f2[u]);
}
}
int main(){
std::ios::sync_with_stdio(false);
LL i , j , u , v , val , sum = 0 ;
cin>>n>>s;
for(i = 1 ; i < n ; i ++ ){
cin>>u>>v>>val;
sum += val;
vec[u].push_back({v , val});
vec[v].push_back({u , val});
}
dps(1,0);
cout<<(sum*2 - res)<<endl;
return 0;
}
错误代码(只为了给自己提醒)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<utility>
#include<vector>
/*
想错了,因为,如果S节点只有一个向下的节点,但是这个
节点会有很多枝,那就会浪费一个机器人。
我想的是,只有一个机器人该怎么跑,然后在根节点上
多减一个最远的就行。哎,,好菜好菜
正解:一个机器人从s出发,到一个直径的端点,并遍历沿途
的边,另一个到另一个。
*/
using namespace std;
const int Maxn = 0x3f3f3f3f;
const int N = 1e6 + 1e3;
typedef long long int LL;
LL n , s;
vector<pair<LL , LL> > vec[N];//to , val
LL dps(LL u , LL fat){
int i , j , v , val;
LL res = 0;
vector<LL> vece;
for(i = 0 ; i < vec[u].size() ; i ++ ){
v = vec[u][i].first;
val = vec[u][i].second;
if(v == fat) continue;
LL tem = dps(v,u) + val;
vece.push_back(tem);
}
sort(vece.begin() , vece.end());
if(fat == 0 ){
i = 0;
if(vece.size() >= 3)
for(i = 0; i < vece.size()-2 ; i ++ ){
res += 2 * vece[i];
}
for(i ; i < vece.size() ; i ++ ){
res += vece[i];
}
}
else{
i = 0;
if(vece.size() >= 2)
for(i = 0; i < vece.size()-1 ; i ++ ){
res += 2 * vece[i];
}
for(i ; i < vece.size() ; i ++ ) res += vece[i];
}
return res;
}
int main(){
std::ios::sync_with_stdio(false);
LL i , j , u , v , val , res;
cin>>n>>s;
for(i = 1 ; i < n ; i ++ ){
cin>>u>>v>>val;
vec[u].push_back({v,val});
vec[v].push_back({u,val});
}
res = dps(s,0);
cout<<res<<endl;
return 0;
}
最后一片代码为错误的哦,倒数第二个是正解。