fork download
  1. #include <bits/stdc++.h>
  2. #define M 1000000007
  3. #define pb push_back
  4. typedef long long ll;
  5. using namespace std;
  6. int atoi(string s)
  7. {
  8. int num = 0;
  9. for(int i=0;i<s.length();i++)
  10. num = num*10 + (s[i]-'0');
  11. return num;
  12. }
  13. vector<pair<ll,ll> > *ls;
  14. int comp(ll a,ll b)
  15. {
  16. return (a>b);
  17. }
  18. ll maxi;
  19. void dfs(ll nod, bool vst[], ll cst)
  20. {
  21. vst[nod] = true;
  22. maxi = max(maxi,cst);
  23. for(int i=0;i<ls[nod].size();i++)
  24. {
  25. if(!vst[ls[nod][i].first])
  26. dfs(ls[nod][i].first,vst,cst + ls[nod][i].second);
  27. }
  28. }
  29. int main()
  30. {
  31. //if (in.is_open())
  32. {
  33. int tc;
  34. cin >> tc;
  35. //tc = atoi(str);
  36. while(tc--)
  37. {
  38. ll n,sum=0;
  39. cin >> n;
  40. ls = new vector<pair<ll,ll> >[n+1];
  41. for(int i=0;i<n-1;i++)
  42. {
  43. ll a,b,c;
  44. cin >> a >> b >> c;
  45. sum += c;
  46. ls[a].push_back(make_pair(b,c));
  47. ls[b].push_back(make_pair(a,c));
  48. }
  49. //out << sum <<endl;
  50. bool vst[n+1];
  51. memset(vst,false,sizeof(vst));
  52. maxi = INT_MIN;
  53. dfs(1,vst,0);
  54. //cout<<maxi<<endl;
  55. cout << (2*sum - maxi) << endl;
  56. }
  57. }
  58. //in.close();
  59. //out.close();
  60. return 0;
  61. }
  62.  
Runtime error #stdin #stdout #stderr 0s 3768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc