제출 #685344

#제출 시각아이디문제언어결과실행 시간메모리
685344saayan007Commuter Pass (JOI18_commuter_pass)C++17
15 / 100
2080 ms262144 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<int, int>; using pl = pair<long long, long long>; using vi = vector<int>; using vl = vector<long long>; using vpi = vector<pair<int, int>>; using vpl = vector<pair<long long, long long>>; #define fur(i, a, b) for(ll i = a; i <= (ll)b; ++i) #define ruf(i, a, b) for(ll i = a; i >= (ll)b; --i) #define fr first #define sc second #define mp make_pair #define pb emplace_back #define nl "\n" #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() const ll mxn = 1e5L + 1; const ll inf = 1e16L; ll n, m, s, t, u, v; vpl adj[mxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> s >> t >> u >> v; fur(i, 1, m) { ll a, b, c; cin >> a >> b >> c; adj[a].pb(b, c); adj[b].pb(a, c); } priority_queue<pl> pq; ll dist[n + 1]; fur(i, 1, n) { dist[i] = inf; } ll from[n + 1] = {}; dist[s] = 0; pq.emplace(-dist[s], s); // finding shortest route(s) to t while(!pq.empty()) { ll a = pq.top().sc; if(dist[a] + pq.top().fr != 0) { pq.pop(); continue; } pq.pop(); for(auto [b, c] : adj[a]) { if(dist[b] > dist[a] + c) { dist[b] = dist[a] + c; pq.emplace(-dist[b], b); from[b] = a; } } } // nodes on the shortest route(s) bool zer[n + 1] = {}; pq.emplace(dist[t], t); while(!pq.empty()) { ll a = pq.top().sc; pq.pop(); if(zer[a]) continue; zer[a] = 1; for(auto [b, c] : adj[a]) { if(dist[a] - c == dist[b]) { pq.emplace(dist[b], b); } } } // shortest path from u to a zero node as well as to v ll len[n + 1]; fur(i, 1, n) { len[i] = inf; } priority_queue<tuple<ll, ll, ll>> q; len[u] = 0; q.emplace(-len[u], u, 0); while(!q.empty()) { ll d, a, ten, dir; tie(d, a, ten) = q.top(); if(d + len[a] != 0) { q.pop(); continue; } dir = ten % 2; ten /= 2; q.pop(); for(auto [b, c] : adj[a]) { if(ten == 0) { // use zero if(zer[a] && zer[b]) { if(dist[a] + c == dist[b] && len[a] <= len[b]) { len[b] = len[a]; q.emplace(-len[a], b, 3); } else if(dist[a] - c == dist[b] && len[a] <= len[b]) { len[b] = len[a]; q.emplace(-len[b], b, 2); } } // dont use zero if(len[a] + c < len[b]) { len[b] = len[a] + c; q.emplace(-len[b], b, 0); } } else if(ten == 1) { // continue using if(zer[a] && zer[b]) { // check direction if(dist[a] + c * (dir == 1 ? 1 : -1) == dist[b]) { len[b] = min(len[b], len[a]); q.emplace(-len[b], b, 2 + dir); } } // stop using if(len[a] + c < len[b]) { len[b] = len[a] + c; q.emplace(-len[b], b, 4); } } else if(ten == 2) { if(len[a] + c < len[b]) { len[b] = len[a] + c; q.emplace(-len[b], b, 4); } } } } // cout << to_zer << nl; // fur(i, 1, n) { // cout << dist[i] << ' '; // } // cout << nl; cout << len[v] << nl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...