제출 #670357

#제출 시각아이디문제언어결과실행 시간메모리
670357Reb007Commuter Pass (JOI18_commuter_pass)C++17
100 / 100
527 ms31936 KiB
// clang-format off #include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); //#pragma GCC optimize("Ofast") //#pragma GCC optimize("fast-math") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native") #define int long long #define ll long long #define ld long double #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() const long long INF = 20000000000000000; const int inf = 1000000010; const int maxn = 262144; const int mxlg = 20; const double eps = 1e-13; int mod = 1e9 + 9; const int C = 340; vector<vector<pair<int, int>>> eds; vector<vector<int>> g2; int n; vector<int> dij(int s) { vector<int> d(n, INF); d[s] = 0; set<pair<int, int>> buf; buf.insert({0, s}); while (!buf.empty()) { int v = buf.begin()->second; buf.erase(buf.begin()); for (auto [to, c]: eds[v]) { if (d[v] + c < d[to]) { buf.erase({d[to], to}); d[to] = d[v] + c; buf.insert({d[to], to}); } } } return d; } void solve() { int m, s, t, v, u; cin >> n >> m >> s >> t >> v >> u; --s, --t, --v, --u; eds.resize(n); for (int i = 0; i < m; ++i) { int x, y, c; cin >> x >> y >> c; --x, --y; eds[x].push_back({y, c}); eds[y].push_back({x, c}); } vector<int> st = dij(s); vector<int> ts = dij(t); vector<bool> dag(n); g2.resize(n); dag[s] = dag[t] = 1; for (int i = 0; i < n; ++i) { for (auto [to, w]: eds[i]) { if (st[i] + w + ts[to] == st[t]) { dag[i] = dag[to] = 1; g2[i].push_back(to); } } } vector<int> dv = dij(v); vector<int> du = dij(u); int ans = dv[u]; set<pair<int, int>> buf; vector<int> cost = dv; for (int i = 0; i < n; ++i) { if (dag[i]) { buf.insert({dv[i], i}); } } while (!buf.empty()) { int cur = buf.begin()->second; buf.erase(buf.begin()); ans = min(ans, cost[cur] + du[cur]); for (int to: g2[cur]) { if (cost[cur] < cost[to]) { buf.erase({cost[to], to}); cost[to] = cost[cur]; buf.insert({cost[to], to}); } } } cost = du; for (int i = 0; i < n; ++i) { if (dag[i]) { buf.insert({du[i], i}); } } while (!buf.empty()) { int cur = buf.begin()->second; buf.erase(buf.begin()); ans = min(ans, cost[cur] + dv[cur]); for (int to: g2[cur]) { if (cost[cur] < cost[to]) { buf.erase({cost[to], to}); cost[to] = cost[cur]; buf.insert({cost[to], to}); } } } cout << ans << "\n"; } signed main() { // freopen("kthzero.in", "r", stdin); // freopen("kthzero.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int __ = 1; // cin >> __; while (__--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...