fork download
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. #define ll long long
  8. #define pb push_back
  9. #define fi first
  10. #define se second
  11.  
  12. const ll MAXN = 1e2 + 5;
  13. const ll MOD = 1e9 + 7;
  14. const ll INF = 1e9 + 7;
  15.  
  16. void FastIO(){
  17. ios_base::sync_with_stdio(false);
  18. cin.tie(NULL); cout.tie(NULL);
  19. }
  20.  
  21. void fre(){
  22. freopen("CAU2.inp", "r", stdin);
  23. freopen("CAU2.out", "w", stdout);
  24. }
  25.  
  26. struct canh{
  27. ll z, x, y;
  28. };
  29.  
  30. class cmp{
  31. public:
  32. bool operator()(canh a, canh b){
  33. return a.z > b.z;
  34. }
  35. };
  36.  
  37. ll n;
  38. vector<canh> adj[MAXN];
  39. vector<ll> check;
  40. priority_queue<canh, vector<canh>, cmp> p;
  41.  
  42. int main()
  43. {
  44. FastIO();
  45. fre();
  46. cin >> n;
  47. ll u, v, c;
  48. while(cin >> u >> v >> c){
  49. adj[v].pb({c, u, v});
  50. adj[u].pb({c, v, u});
  51. }
  52. check.resize(n+1, 0);
  53. check[1] = 1;
  54. for(canh i : adj[1]) p.push(i);
  55. ll s = 0;
  56. for(int i=1; i<n; ++i){
  57. canh k;
  58. do{
  59. k = p.top();
  60. p.pop();
  61. }while(check[k.x] == 1);
  62. check[k.x] = 1;
  63. s += k.z;
  64. for(canh j : adj[k.x])
  65. if(check[j.x] == 0) p.push(j);
  66. }
  67. cout << s;
  68. return 0;
  69. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty