1 solutions
-
2
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; typedef pair<int,int> PII; vector<PII> g[N]; #define x first #define y second typedef long long LL; LL ans=0; void dfs(int u,int fa,LL sum) { ans=max(ans,sum); //找到走到根节点的最长路径(只走一遍) for(auto t:g[u]) { if(t.x==fa) continue; dfs(t.x,u,sum+t.y); } } int main() { int n; cin>>n; LL s=0; for(int i=1;i<n;i++) { int a,b,c; cin>>a>>b>>c; g[a].push_back({b,c}); g[b].push_back({a,c}); s+=c; //累加所有路径 } dfs(1,-1,0); cout<<2*s-ans; return 0; }
- 1
Information
- ID
- 2895
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 10
- Tags
- # Submissions
- 2
- Accepted
- 2
- Uploaded By