题解:P14076 [GESP202509 六级] 货物运输
Solution
很容易发现,该题目给出的图是一棵树。首都为
题目的要求是每个点都走一遍,求最少路径。假如最终要回到首都,那么结果就是所有边权的和再乘上
时间复杂度
AC code
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,x,y,z,ans,maxx;
vector<pair<int,int>> edge[100005];
void dfs(int now,int fa,int sum){
for(auto to:edge[now]) if(to.first!=fa) dfs(to.first,now,sum+to.second);
maxx=max(maxx,sum);
}
signed main(){
cin>>n;
for(int i = 1;i<n;i++) cin>>x>>y>>z,edge[x].push_back({y,z}),edge[y].push_back({x,z}),ans+=z*2;
dfs(1,0,0);
cout<<ans-maxx;
return 0;
}