Submission #486046

#TimeUsernameProblemLanguageResultExecution timeMemory
486046Jarif_RahmanOlympic Bus (JOI20_ho_t4)C++17
0 / 100
61 ms9248 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; const ll inf = 1e17; vector<ll> dijkstra(vector<vector<pair<ll, int>>> v, int source){ int n = v.size(); vector<ll> dis(n, inf); vector<bool> bl(n, 0); priority_queue<pair<ll, int>> pq; dis[source] = 0; pq.push({0, source}); while(!pq.empty()){ int nd = pq.top().sc; pq.pop(); if(bl[nd]) continue; bl[nd] = 1; for(auto [w, x]: v[nd]) if(dis[nd]+w < dis[x]){ dis[x] = dis[nd]+w; pq.push({-dis[x], x}); } } return dis; } vector<ll> d1, d2, d3, d4; vector<vector<pair<ll, int>>> v, vr; map<array<int, 3>, int> mp; vector<bool> s1, s2; vector<bool> bl1, bl2; vector<int> ddd1, ddd2; void dfs_1(int nd, int d){ if(bl1[nd]){ return; } bl1[nd] = 1; ddd1[nd] = d; for(auto [w, x]: v[nd]) dfs_1(x, d+1); } void dfs_2(int nd, int d){ if(bl2[nd]){ return; } bl2[nd] = 1; ddd2[nd] = d; for(auto [w, x]: v[nd]) dfs_2(x, d+1); } void dfs1(int nd){ for(auto [w, x]: vr[nd]) if(d1[x] < d1[nd] || (d1[x] == d1[nd] && ddd1[x] < ddd1[nd])){ s1[mp[{x, nd, (int)w}]] = 1; dfs1(x); break; } } void dfs2(int nd){ for(auto [w, x]: vr[nd]) if(d3[x] < d3[nd] || (d3[x] == d3[nd] && ddd2[x] < ddd2[nd])){ s2[mp[{x, nd, (int)w}]] = 1; dfs2(x); break; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<array<int, 4>> e(m); v.resize(n); vr.resize(n); for(int i = 0; i < m; i++){ cin >> e[i][0] >> e[i][1] >> e[i][2] >> e[i][3]; e[i][0]--, e[i][1]--; mp[{e[i][0], e[i][1], e[i][2]}] = i; v[e[i][0]].pb({e[i][2], e[i][1]}); vr[e[i][1]].pb({e[i][2], e[i][0]}); } s1.assign(m, 0), s2.assign(m, 0); d1 = dijkstra(v, 0), d2 = dijkstra(vr, n-1), d3 = dijkstra(v, n-1), d4 = dijkstra(vr, 0); bl1.assign(n, 0); bl2.assign(n, 0); ddd1.assign(n, 0); ddd2.assign(n, 0); dfs_1(0, 0); dfs_2(n-1, 0); dfs1(n-1); dfs2(0); ll ans = inf; ans = min(ans, d1[n-1]+d3[0]); for(int i = 0; i < m; i++){ int a = e[i][0], b = e[i][1]; ll w = e[i][2], c = e[i][3]; if(!s2[i]){ ans = min(ans, c+d1[b]+w+d2[a]+d3[0]); continue; } vector<vector<pair<ll, int>>> v0(n); for(int j = 0; j < m; j++){ if(j == i) v0[e[j][1]].pb({e[j][2], e[j][0]}); else v0[e[j][0]].pb({e[j][2], e[j][1]}); } auto dd1 = dijkstra(v0, 0), dd2 = dijkstra(v0, n-1); ans = min(ans, dd1[n-1]+dd2[0]); } for(int i = 0; i < m; i++){ int a = e[i][0], b = e[i][1]; ll w = e[i][2], c = e[i][3]; if(!s1[i]){ ans = min(ans, c+d1[n-1]+d3[b]+w+d4[a]); continue; } vector<vector<pair<ll, int>>> v0(n); for(int j = 0; j < m; j++){ if(j == i) v0[e[j][1]].pb({e[j][2], e[j][0]}); else v0[e[j][0]].pb({e[j][2], e[j][1]}); } auto dd1 = dijkstra(v0, 0), dd2 = dijkstra(v0, n-1); ans = min(ans, dd1[n-1]+dd2[0]); } if(ans >= inf) ans = -1; cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...