Submission #860645

#TimeUsernameProblemLanguageResultExecution timeMemory
860645NursikCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
218 ms31380 KiB
#include <stdio.h> #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <random> #include <chrono> #include <queue> #include <functional> #include <iostream> #include <queue> //#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define f first #define s second #define ld long double const ll maxn = 1e5 + 1, maxm = 1e7 + 1; const ll mod = 1e9 + 7, mod2 = 998244353, inf = 1e9, block = 550, hb = 31, base = 1000050017; const ld eps = 1e-9; int n, m; int s, t; int ss, tt; bool is[maxn]; int go[maxn]; ll ans = inf * inf, dp[maxn], dp2[maxn], dp3[maxn], was[maxn]; ll pd[maxn][2]; vector<pair<int, int>> g[maxn]; vector<int> g2[maxn], g3[maxn]; void dfsis(int v){ if (v == t){ is[v] = 1; } was[v] = 1; for (auto to : g2[v]){ if (was[to] == 0){ dfsis(to); } is[v] |= is[to]; } } void dijkstra(){ for (int i = 1; i <= n; ++i){ dp[i] = inf * inf; } dp[s] = 0; // set<pair<ll, int>> setik; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>> > setik; setik.push(mp(0, s)); while (setik.size() > 0){ pair<ll, int> cur = setik.top(); setik.pop(); ll cost = cur.f, v = cur.s; if (dp[v] < cost){ continue; } for (auto to : g[v]){ int nv = to.f, w = to.s; if (dp[nv] > cost + w){ dp[nv] = cost + w; setik.push(mp(dp[nv], nv)); } } } for (int i = 1; i <= n; ++i){ for (auto to : g[i]){ int nv = to.f, w = to.s; if (dp[i] > dp[nv]){ continue; } if (dp[nv] == w + dp[i] && dp[i] < dp[nv]){ g2[i].pb(nv); } } } dfsis(s); for (int i = 1; i <= n; ++i){ g2[i].clear(); for (auto to : g[i]){ int nv = to.f, w = to.s; if (dp[i] > dp[nv]){ continue; } if (dp[nv] == w + dp[i] && is[nv] == 1){ g2[i].pb(nv); } } } } void dijkstra2(){ for (int i = 1; i <= n; ++i){ dp2[i] = inf * inf; } dp2[ss] = 0; // set<pair<ll, int>> setik; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>> > setik; setik.push(mp(0, ss)); while (setik.size() > 0){ pair<ll, int> cur = setik.top(); setik.pop(); ll cost = cur.f, v = cur.s; if (dp2[v] < cost){ continue; } for (auto to : g[v]){ int nv = to.f, w = to.s; if (dp2[nv] > cost + w){ dp2[nv] = cost + w; setik.push(mp(dp2[nv], nv)); } } } } void dijkstra3(){ for (int i = 1; i <= n; ++i){ dp3[i] = inf * inf; } dp3[tt] = 0; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>> > setik; setik.push(mp(0, tt)); while (setik.size() > 0){ pair<ll, int> cur = setik.top(); setik.pop(); ll cost = cur.f, v = cur.s; if (dp3[v] < cost){ continue; } for (auto to : g[v]){ int nv = to.f, w = to.s; if (dp3[nv] > cost + w){ dp3[nv] = cost + w; setik.push(mp(dp3[nv], nv)); } } } } void backdfs(int v){ for (int i = 1; i <= n; ++i){ for (auto to : g2[i]){ go[i] += 1; g3[to].pb(i); } pd[i][0] = inf * inf; pd[i][1] = inf * inf; } queue<int> q; q.push(v); while (q.size() > 0){ int u = q.front(); q.pop(); pd[u][0] = min(pd[u][0], dp3[u]); pd[u][1] = min(pd[u][1], dp2[u]); ans = min(ans, pd[u][0] + dp2[u]); ans = min(ans, pd[u][1] + dp3[u]); for (auto to : g3[u]){ go[to] -= 1; pd[to][0] = min(pd[to][0], pd[u][0]); pd[to][1] = min(pd[to][1], pd[u][1]); if (go[to] == 0){ q.push(to); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; cin >> s >> t; cin >> ss >> tt; for (int i = 1; i <= m; ++i){ int a, b, c; cin >> a >> b >> c; g[a].pb(mp(b, c)); g[b].pb(mp(a, c)); } dijkstra(); dijkstra2(); dijkstra3(); backdfs(t); ans = min(ans, dp2[tt]); 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...