제출 #368213

#제출 시각아이디문제언어결과실행 시간메모리
368213ijxjdjdCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
449 ms23656 KiB
#include <bits/stdc++.h> #define FR(i, N) for (int i = 0; i < int(N); i++) #define all(x) begin(x), end(x) #define f first #define s second using namespace std; using ll = long long; const int MAXN = 100000; vector<pair<int, int>> adj[MAXN]; const ll INF = (ll)(1e18); int N, M; int pass[2]; ll dist[4][MAXN]; ll distPass[2][MAXN]; int start, stop; void dijkstraPass(int s) { priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; FR(i, MAXN) { distPass[s][i] = INF; } distPass[s][pass[s]] = 0; pq.push({0, pass[s]}); while (pq.size()) { auto [d, n] = pq.top(); pq.pop(); if (distPass[s][n] == d) { for (auto [e, w] : adj[n]) { if (distPass[s][e] > distPass[s][n] + w) { distPass[s][e] = distPass[s][n] + w; pq.push({distPass[s][e], e}); } } } } } int main() { cin.tie(0); cin.sync_with_stdio(0); cin >> N >> M; cin >> pass[0] >> pass[1]; cin >> start >> stop; pass[0]--, pass[1]--, start--, stop--; FR(i, M) { int u, v, w; cin >> u >> v >> w; u--, v--; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } dijkstraPass(0); dijkstraPass(1); priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; FR(i, 4) FR(j, MAXN) dist[i][j] = INF; dist[0][start] = 0; pq.push({0, start}); ll minDist = distPass[0][pass[1]]; while (pq.size()) { auto[d, n] = pq.top(); pq.pop(); int state = n/N; n%=N; if (dist[state][n] == d) { if (state == 0 || state == 3) { for (auto [e, w] : adj[n]) { if (dist[state][e] > dist[state][n] + w) { dist[state][e] = dist[state][n] + w; pq.push({dist[state][e], state*N + e}); } } } if (state == 0) { for (auto [e, w] : adj[n]) { if (distPass[0][n] + distPass[1][e] + w == minDist && dist[2][e] > d) { dist[2][e] = d; pq.push({d, 2*N + e}); } if (distPass[1][n] + distPass[0][e] + w == minDist && dist[1][e] > d) { dist[1][e] = d; pq.push({d, N + e}); } } } if (state == 1 || state == 2) { for (auto [e, w] : adj[n]) { if ((state == 1 ? distPass[1][n] + distPass[0][e] + w : distPass[0][n] + distPass[1][e] + w) == minDist && dist[state][e] > d) { dist[state][e] = d; pq.push({d, state*N + e}); } } } if (dist[3][n] > d) { pq.push({d, 3*N + n}); dist[3][n] = d; } } } cout << dist[3][stop] << '\n'; 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...