結果
問題 | No.2200 Weird Shortest Path |
ユーザー | Lê Hoàng Ngô |
提出日時 | 2024-06-29 15:38:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 132 ms / 2,000 ms |
コード長 | 2,408 bytes |
コンパイル時間 | 1,856 ms |
コンパイル使用メモリ | 181,468 KB |
実行使用メモリ | 16,380 KB |
最終ジャッジ日時 | 2024-06-29 15:38:47 |
合計ジャッジ時間 | 8,348 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 3 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,944 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,940 KB |
testcase_14 | AC | 2 ms
6,944 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 37 ms
6,964 KB |
testcase_18 | AC | 63 ms
9,864 KB |
testcase_19 | AC | 122 ms
15,104 KB |
testcase_20 | AC | 100 ms
14,804 KB |
testcase_21 | AC | 42 ms
8,976 KB |
testcase_22 | AC | 55 ms
9,028 KB |
testcase_23 | AC | 113 ms
15,608 KB |
testcase_24 | AC | 21 ms
6,940 KB |
testcase_25 | AC | 109 ms
15,396 KB |
testcase_26 | AC | 111 ms
15,208 KB |
testcase_27 | AC | 38 ms
10,020 KB |
testcase_28 | AC | 79 ms
14,868 KB |
testcase_29 | AC | 111 ms
15,540 KB |
testcase_30 | AC | 110 ms
15,044 KB |
testcase_31 | AC | 65 ms
10,136 KB |
testcase_32 | AC | 112 ms
15,644 KB |
testcase_33 | AC | 120 ms
15,508 KB |
testcase_34 | AC | 112 ms
15,824 KB |
testcase_35 | AC | 109 ms
15,328 KB |
testcase_36 | AC | 111 ms
15,656 KB |
testcase_37 | AC | 113 ms
15,424 KB |
testcase_38 | AC | 113 ms
15,580 KB |
testcase_39 | AC | 58 ms
9,464 KB |
testcase_40 | AC | 93 ms
16,380 KB |
testcase_41 | AC | 50 ms
9,464 KB |
testcase_42 | AC | 89 ms
15,724 KB |
testcase_43 | AC | 123 ms
15,788 KB |
testcase_44 | AC | 125 ms
15,560 KB |
testcase_45 | AC | 132 ms
15,508 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(x) x.begin(),x.end() #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep_r(i,a,b) for(int i=a;i>=b;i--) #define each(a,x) for (auto& x : a) using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; #define sz(x) int(x.size()) #define so(x) sort(all(x)) #define so_r(x) sort(all(x),greater<int>()) #define lb lower_bound #define ub upper_bound const char nl = '\n'; int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; int bit_cnt(int x){ return __builtin_popcount(x); } ll bex(ll a, ll b, ll mod = 1e9 + 7){ll res = 1LL; while(b){ if (b&1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;} template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} const int MOD = 998244353; const int N = 1e5 + 5; const ll INF = 1e16; int n,m; vector<vl> edges; // https://yukicoder.me/problems/no/2200 // De bai: Cho do thi vo huong co trong so co N dinh va M canh // Cost de di tu u -> v = min of (max_edge in path) for path in all_paths from u -> v // Solve: Su dung kruskal tim MST // Trong bai nay ta chi quan tam toi cac canh thuoc MST, cac canh khong thuoc MST ta ignore // why? : gia su path (1,3) tren MST = 1 -> 2 -> 3 trong do 1 -> 2 = 4 va 2 -> 3 = 5 // => 1 -> 3 >= 5 vi neu no nho hon 5 thi da co the noi thang tu 1 -> 3 roi vector<int> fa, sz; int find(int u) {return (u == fa[u] ? u : fa[u] = find(fa[u]));} bool same(int u, int v){ u = find(u); v = find(v); return u == v; } void merge(int u, int v){ u = find(u); v = find(v); if (u == v) return; if (sz[u] < sz[v]) swap(u,v); sz[u] += sz[v]; fa[v] = u; } void solve(){ cin >> n >> m; fa.resize(n+1); sz.resize(n+1); rep(i,1,n) fa[i] = i, sz[i] = 1; rep(i,1,m){ int u,v; ll w; cin >> u >> v >> w; edges.push_back({w,u,v}); } sort(all(edges)); ll ans = 0; each(edges,ed){ ll w = ed[0]; int u = ed[1], v = ed[2]; if (same(u,v)) continue; ans += w * sz[find(u)] * sz[find(v)]; merge(u,v); } cout << ans << nl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--){ solve(); } }