fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define F first
  5. #define S second
  6. #define pb push_back
  7. #define all(a) a.begin(), a.end()
  8.  
  9. typedef long long ll;
  10. typedef pair<int, int> ii;
  11.  
  12. const int N = 100000 + 5;
  13.  
  14. vector<ii> g[N];
  15. int n, s;
  16. long long maxPath = 0;
  17. long long sumPath = 0;
  18.  
  19. void dfs(int u, int p, long long sum)
  20. {
  21. maxPath = max(maxPath, sum);
  22. for(ii &x : g[u])
  23. if(x.F != p) dfs(x.F, u, sum + x.S);
  24. }
  25.  
  26. void solve()
  27. {
  28. cin >> n >> s;
  29. for(int i = 1; i < n; i++)
  30. {
  31. int u, v, w;
  32. cin >> u >> v >> w;
  33. g[u].pb({v, w});
  34. g[v].pb({u, w});
  35. sumPath += w;
  36. }
  37. dfs(s, 0, 0);
  38. cout << sumPath * 2 - maxPath;
  39. }
  40.  
  41. signed main()
  42. {
  43. cin.tie(0)->sync_with_stdio(0);
  44. int t = 1;
  45. // cin >> t;
  46. while(t--) solve();
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5788KB
stdin
Standard input is empty
stdout
Standard output is empty