Submission #1109246

#TimeUsernameProblemLanguageResultExecution timeMemory
1109246nh0902Commuter Pass (JOI18_commuter_pass)C++17
0 / 100
989 ms262144 KiB
#include <bits/stdc++.h> using namespace std; const int N = 110000; long long const inf = 1e16; int n, m, s, t, u, v; vector<pair<int, long long>> g[N]; vector<int> out[N]; long long min_dist[N]; long long Du[N][2], Dv[N][2]; long long st[4 * N][4]; bool visited[N]; struct cmp{ bool operator() (pair<long long, int> a, pair<long long, int> b) { return a.first > b.first; } }; struct cmp2{ bool operator() (pair<pair<long long, int>, int> a, pair<pair<long long, int>, int> b) { return a.first.first > b.first.first; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> s >> t >> u >> v; int a, b; long long c; for (int i = 1; i <= m; i ++) { cin >> a >> b >> c; g[a].push_back({b, c}); g[b].push_back({a, c}); } priority_queue<pair<long long, int>> pq; for (int i = 1; i <= n; i ++) { min_dist[i] = inf; } min_dist[s] = 0; pq.push({0, s}); while(!pq.empty()) { auto [d, x] = pq.top(); pq.pop(); if (visited[x]) continue; visited[x] = true; for (pair<int, long long> e : g[x]) { if (min_dist[e.first] > e.second + d) { min_dist[e.first] = e.second + d; pq.push({e.second + d, e.first}); } } } priority_queue<int> pr; pr.push(t); while (!pr.empty()) { int x = pr.top(); pr.pop(); for (pair<int, long long> e : g[x]) { if (min_dist[e.first] + e.second == min_dist[x]) { out[e.first].push_back(x); pr.push(e.first); } } } priority_queue<pair<pair<long long, int>, int>, vector<pair<pair<long long, int>, int>>, cmp2> pq2; for (int i = 1; i <= n; i ++) { Du[i][0] = Du[i][1] = Dv[i][0] = Dv[i][1] = inf; } Du[u][0] = 0; pq2.push({{0, u}, 0}); while (!pq2.empty()) { auto [p, t] = pq2.top(); auto [d, x] = p; pq2.pop(); if (d > Du[x][t]) continue; for (int e : out[x]) { if (Du[e][1] > d) { Du[e][1] = d; pq2.push({{d, e}, 1}); } } if (t == 0) { for (auto e : g[x]) { if (Du[e.first][0] > d + e.second) { Du[e.first][0] = d + e.second; pq2.push({{Du[e.first][0], e.first}, 0}); } } } } Dv[v][0] = 0; pq2.push({{0, v}, 0}); while (!pq2.empty()) { auto [p, t] = pq2.top(); auto [d, x] = p; pq2.pop(); if (d > Dv[x][t]) continue; for (int e : out[x]) { if (Dv[e][1] > d) { Dv[e][1] = d; pq2.push({{d, e}, 1}); } } if (t == 0) { for (auto e : g[x]) { if (Dv[e.first][0] > d + e.second) { Dv[e.first][0] = d + e.second; pq2.push({{Dv[e.first][0], e.first}, 0}); } } } } long long ans = Du[v][0]; for (int i = 1; i <= n; i ++) { ans = min(ans, Du[i][0] + Dv[i][1]); ans = min(ans, Du[i][1] + Dv[i][0]); } cout << ans; 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...