fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define all(v) (v).begin(), (v).end()
  4. #define rep(i, l, r) for (int i = (l); i < (r); ++i)
  5. #define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
  6. #define FORD(i, r, l) for (int i = (r); i >= (l); --i)
  7. #define DB(X) { cerr << #X << " = " << (X) << '\n'; }
  8. #define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
  9. #define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
  10. #define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
  11. #define PR(A, l, r) { cerr << '\n'; FOR(_, l, r) DB1(A, _); cerr << '\n';}
  12. #define sz(x) ((int) (x).size())
  13. #define pb push_back
  14. #define eb emplace_back
  15. #define pf push_front
  16. #define F first
  17. #define S second
  18. #define next ___next
  19. #define prev ___prev
  20. #define y1 ___y1
  21. #define left ___left
  22. #define right ___right
  23. #define y0 ___y0
  24. #define div ___div
  25.  
  26. using ll = long long;
  27. using ld = long double;
  28. using ull = unsigned long long;
  29. using namespace std;
  30.  
  31. typedef pair<int, int> ii;
  32. typedef array<int, 3> iii;
  33. typedef vector<int> vi;
  34. typedef vector<ii> vii;
  35. typedef vector<iii> viii;
  36. typedef vector<ll> vl;
  37. const int N = 5002, INF = 1e9 + 7;
  38. int n, m, edgeW[N];
  39. vii g[N];
  40. int d[N];
  41. bool done[N];
  42. int main()
  43. {
  44. #ifdef HynDuf
  45. freopen("inp.txt", "r", stdin);
  46. #else
  47. ios_base::sync_with_stdio(false); cin.tie(nullptr);
  48. #endif
  49. cin >> n >> m;
  50. ll tot = 0;
  51. FOR(i, 1, m)
  52. {
  53. int u, v, w;
  54. cin >> u >> v >> w;
  55. g[u].eb(v, w);
  56. g[v].eb(u, w);
  57. tot += w;
  58. }
  59. FOR(i, 2, n) d[i] = INF;
  60. FOR(i, 1, n)
  61. {
  62. int mn = INF;
  63. int u = 0;
  64. FOR(v, 1, n) if (!done[v] && mn > d[v]) mn = d[v], u = v;
  65. if (u == 0) break;
  66. done[u] = 1;
  67. for (auto &v : g[u]) if (!done[v.F] && d[v.F] > v.S)
  68. d[v.F] = v.S, edgeW[v.F] = v.S;
  69. }
  70. FOR(i, 1, n) tot -= edgeW[i];
  71. cout << tot;
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty