Submission #1031759

#TimeUsernameProblemLanguageResultExecution timeMemory
1031759NonozeCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
529 ms87708 KiB
/* * Author: Nonoze * Created: Tuesday 23/07/2024 */ #include <bits/stdc++.h> using namespace std; #ifndef _IN_LOCAL #define dbg(...) #endif #define endl '\n' #define endlfl '\n' << flush #define quit(x) return (void)(cout << x << endl) template<typename T> void read(T& x, bool write=0, bool write2=1) { if (write && write2) cout << x << endl; else if (write) cout << x << ' '; else cin >> x;} template<typename T1, typename T2> void read(pair<T1, T2>& p, bool write=0, bool write2=1) { read(p.first, write, 0), read(p.second, write, 0); if (write && write2) cout << endl;} template<typename T> void read(vector<T>& v, bool write=0) { for (auto& x : v) read(x, write, 0); if (write) cout << endl; } template<typename T1, typename T2> void read(T1& x, T2& y, bool write=0) { read(x, write), read(y, write); if (write) cout << endl; } template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z, bool write=0) { read(x, write), read(y, write), read(z, write); if (write) cout << endl; } template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz, bool write=0) { read(x, write), read(y, write), read(z, write), read(zz, write); if (write) cout << endl; } template<typename T> void print(T x) { read(x, 1); } template<typename T1, typename T2> void print(T1 x, T2 y) { read(x, y, 1); } template<typename T1, typename T2, typename T3> void print(T1 x, T2 y, T3 z) { read(x, y, z, 1); } template<typename T1, typename T2, typename T3, typename T4> void print(T1 x, T2 y, T3 z, T4 zz) { read(x, y, z, zz, 1); } #define sz(x) (int)(x.size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define mp make_pair #define fi first #define se second #define cmin(a, b) a = min(a, b) #define cmax(a, b) a = max(a, b) #define int long long const int inf = numeric_limits<int>::max() / 4; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1e9+7, LOG=25; void solve(); signed main() { ios::sync_with_stdio(0); cin.tie(0); int tt=1; // cin >> tt; while(tt--) solve(); return 0; } int n, m; vector<vector<pair<int, int>>> adj, prec, nxt; vector<bool> used; void dfs(int u) { used[u]=1; for (auto [v, w]: prec[u]) if (!used[v]) dfs(v); } void dijkstra(int s, int t) { vector<int> dist(n, inf); vector<bool> visited(n); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; pq.push({0, s}); dist[s]=0; while (!pq.empty()) { auto [d, u]=pq.top(); pq.pop(); if (visited[u]) continue; visited[u]=1; for (auto [v, w]: adj[u]) { if (dist[v]>dist[u]+w) { dist[v]=dist[u]+w; prec[v].clear(); prec[v].pb({u, w}); pq.push({dist[v], v}); } else if (dist[v]==dist[u]+w) { prec[v].pb({u, w}); } } } dfs(t); } void solve() { read(n, m); int s, t, s2, t2; read(s, t, s2, t2); s--, t--, s2--, t2--; adj.resize(n); for (int i=0; i<m; i++) { int a, b, c; read(a, b, c); a--, b--; adj[a].pb({b, c}); adj[b].pb({a, c}); } prec.resize(n), nxt.resize(n), used.resize(n); dijkstra(t, s); swap(prec, nxt); dijkstra(s, t); // dbg(prec); // dbg(nxt); // dbg(used); s=s2, t=t2; vector<vector<vector<int>>> dist(n, vector<vector<int>>(3, vector<int>(3, inf))); vector<vector<vector<bool>>> visited(n, vector<vector<bool>>(3, vector<bool>(3))); priority_queue<vector<int>, vector<vector<int>>, greater<> > pq; pq.push({0, s, 0, 0}); dist[s][0][0]=0; while (!pq.empty()) { auto vec=pq.top(); pq.pop(); int d=vec[0], u=vec[1], already=vec[2], dir=vec[3]; if (visited[u][already][dir]) continue; visited[u][already][dir]=1; if (u==t) { cout << d << endl; return; } if (used[u] && already!=2) { if (dir!=1) { for (auto [v, w]: prec[u]) { if (d<dist[v][1][2]) dist[v][1][2]=d, pq.push({d, v, 1, 2}); } } if (dir!=2) { for (auto [v, w]: nxt[u]) { if (d<dist[v][1][1]) dist[v][1][1]=d, pq.push({d, v, 1, 1}); } } } if (already==1) already=2, dir=0; for (auto [v, w]: adj[u]) { if (dist[v][already][dir]>d+w) { dist[v][already][dir]=d+w; pq.push({d+w, v, already, dir}); } } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...