結果
| 問題 |
No.2200 Weird Shortest Path
|
| ユーザー |
|
| 提出日時 | 2023-02-03 20:58:24 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 163 ms / 2,000 ms |
| コード長 | 11,667 bytes |
| コンパイル時間 | 3,714 ms |
| コンパイル使用メモリ | 274,412 KB |
| 実行使用メモリ | 25,504 KB |
| 最終ジャッジ日時 | 2024-07-02 18:47:32 |
| 合計ジャッジ時間 | 11,866 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 44 |
ソースコード
#include <bits/stdc++.h>
#ifndef LOCAL_TEST
#pragma GCC target ("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif // LOCAL_TEST
using namespace std;
using ll = long long;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
using vs = vector<string>; using vvs = vector<vector<string>>; using vvvs = vector<vector<vector<string>>>;
template <class T> using priority_queue_min = priority_queue<T, vector<T>, greater<T>>;
const double PI = acos(-1.0);
int INF = 100100111; ll INFL = 3300300300300300491LL;
double EPS = 1e-8; double EPSL = 1e-16;
constexpr int MODINT998244353 = 998244353;
constexpr int MODINT1000000007 = 1000000007;
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define rep(i, n) for(ll i = 0, i##_len = ll(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(ll i = ll(s), i##_end = ll(t); i < i##_end; ++i) // s から t まで昇順
#define repis(i, s, t, step) for(ll i = ll(s), i##_end = ll(t); i < i##_end; i+=step) // s から t まで stepずつ
#define repir(i, s, t, step) for(ll i = ll(s), i##_end = ll(t); i > i##_end; i+=step) // s から t まで stepずつ
#define repe(a, v) for(auto& a : (v)) // v の全要素(変更可能)
#define repea(a, v) for(const auto& a : (v)) // v の全要素(変更不可能)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define sdiv(n, m) (((n) - smod(n, m)) / (m)) // 非負div
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
void Yes(bool b) { cout << (b ? "Yes\n" : "No\n"); return; }
ll powm(ll a, ll n, ll mod=INFL) {
ll res = 1;
while (n > 0) {
if (n & 1) res = (res * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return res;
}
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
int digit(ll x, int d=10) { int rev=0; while (x > 0) { rev++; x /= d;}; return rev; } // xのd進数桁数
#include <algorithm>
#include <cassert>
#include <vector>
namespace atcoder {
struct dsu {
public:
dsu() : _n(0) {}
explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}
int merge(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y) return x;
if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0) return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(_n), group_size(_n);
for (int i = 0; i < _n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(_n);
for (int i = 0; i < _n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < _n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(
std::remove_if(result.begin(), result.end(),
[&](const std::vector<int>& v) { return v.empty(); }),
result.end());
return result;
}
private:
int _n;
std::vector<int> parent_or_size;
};
} // namespace atcoder
template <class Cost> struct Graph{
public:
struct Edge {
int to;
Cost cost;
Edge() {};
Edge(int _to, Cost _cost) : to(_to), cost(_cost) {};
};
struct AdjacencyListRange{
using iterator = typename std::vector<Edge>::const_iterator;
iterator begi, endi;
iterator begin() const { return begi; }
iterator end() const { return endi; }
int size() const { return (int)distance(begi, endi); }
const Edge& operator[](int i) const { return begi[i]; }
};
private:
public:
vector<Edge> E;
vector<int> I;
int n;
Graph() : n(0) {}
Graph(int _n) : n(_n) {}
Graph(int _n, const vector<int>& from, vector<int>& to, vector<Cost>& cost, bool rev = false) : n(_n) {
vector<int> buf(n+1, 0);
for(int i=0; i<(int)from.size(); i++){
++buf[from[i]];
if (rev) ++buf[to[i]];
}
for(int i=1; i<=_n; i++) buf[i] += buf[i-1];
E.resize(buf[n]);
for(int i=(int)from.size()-1; i>=0; i--){
int u = from[i];
int v = to[i];
Cost c = cost[i];
E[--buf[u]] = Edge(v, c);
if(rev) E[--buf[v]] = Edge(u, c);
}
I = move(buf);
}
AdjacencyListRange operator[](int u) const {
return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
}
int num_vertices() const { return n; }
int size() const { return num_vertices(); }
int num_edges() const { return E.size(); }
};
template <class T> ostream& operator<<(ostream& os, Graph<T> g) {
bool first = true;
rep(i, g.n) repe(e, g[i]) {
if (first) first = false;
else os << endl;
os << i << "->" << e.to << ": " << e.cost;
}
return os;
}
template<class Cost> Graph<Cost> kruskal(const Graph<Cost>& G){
atcoder::dsu uf(G.n);
priority_queue<tuple<Cost, int, int>, vector<tuple<Cost, int, int>>, greater<tuple<Cost, int, int>>> que;
rep(i, G.n) repe(e, G[i]) { que.emplace(e.cost, i, e.to); }
int count = 0;
vi from,to;
vector<Cost> cost;
while(que.size() && count < G.n - 1 ){
auto [c, u, v] = que.top();
que.pop();
if (!uf.same(u, v)) {
uf.merge(u, v);
from.push_back(u);
to.push_back(v);
cost.push_back(c);
count++;
}
}
if (count != G.n - 1) return Graph<Cost>(0);
return Graph<Cost>(G.n, from, to, cost, true);
}
#include <bits/stdc++.h>
using namespace std;
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p);
template <class T> inline istream& operator>>(istream& is, vector<T>& v);
template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p);
template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v);
template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp);
template <typename T> ostream &operator<<(ostream &os, const set<T> &st);
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st);
template <typename T> ostream &operator<<(ostream &os, queue<T> q);
template <typename T> ostream &operator<<(ostream &os, deque<T> q);
template <typename T> ostream &operator<<(ostream &os, stack<T> st);
template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq);
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repe(x, v) is >> x; return is; }
template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << " " << p.second; return os; }
template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v) { rep(i, sz(v)) { os << v.at(i); if (i != sz(v) - 1) os << " "; } return os; }
template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp) { for (auto &[key, val] : mp) { os << key << ":" << val << " "; } return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> q) { while (q.size()) { os << q.front() << " "; q.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, deque<T> q) { while (q.size()) { os << q.front() << " "; q.pop_front(); } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> st) { while (st.size()) { os << st.top() << " "; st.pop(); } return os; }
template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq) { while (pq.size()) { os << pq.top() << " "; pq.pop(); } return os; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repe(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repe(x, v) ++x; return v; }
#define debug(...) debug_func(0, #__VA_ARGS__, __VA_ARGS__) // debug print
#ifdef LOCAL_TEST
template <typename T>
void debug_func(int i, T name) { (void)i; (void)name; cerr << endl; }
template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, const T2 &a, const T3 &...b) {
for ( ; name[i] != ',' && name[i] != '\0'; i++ ) cerr << name[i];
cerr << ":" << a << " ";
debug_func(i + 1, name, b...);
}
#endif
#ifndef LOCAL_TEST
template <typename... T>
void debug_func(const T &...) {}
#endif
ll ans(0);
template<class Cost> Graph<Cost> kruskal2(const Graph<Cost>& G){
atcoder::dsu uf(G.n);
priority_queue<tuple<Cost, int, int>, vector<tuple<Cost, int, int>>, greater<tuple<Cost, int, int>>> que;
rep(i, G.n) repe(e, G[i]) { que.emplace(e.cost, i, e.to); }
int count = 0;
vi from,to;
vector<Cost> cost;
while(que.size() && count < G.n - 1 ){
auto [c, u, v] = que.top();
que.pop();
if (!uf.same(u, v)) {
ans += c * uf.size(u) * uf.size(v);
uf.merge(u, v);
from.push_back(u);
to.push_back(v);
cost.push_back(c);
count++;
}
}
if (count != G.n - 1) return Graph<Cost>(0);
return Graph<Cost>(G.n, from, to, cost, true);
}
int main() {
int n,m;
cin >> n >> m;
vi from(m), to(m); vl cost(m);
rep(i, m) cin >> from[i] >> to[i] >> cost[i];
--from; --to;
Graph g(n, from, to, cost, true);
Graph g2 = kruskal2(g);
debug(g2);
cout << ans << endl;
}