제출 #939296

#제출 시각아이디문제언어결과실행 시간메모리
939296vjudge1Commuter Pass (JOI18_commuter_pass)C++17
100 / 100
343 ms42192 KiB
#include<bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; #define int ll typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define pb push_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define F first #define S second #define getlast(s) (*s.rbegin()) #define debg cout << "OK\n" const ld PI = 3.1415926535; const int N = 2e5 + 6; const int M = 7e6 + 1; const int mod = 1e9+7; const int infi = INT_MAX; const ll infl = LLONG_MAX; const int P = 31; int mult(int a, int b) { return a * 1LL * b % mod; } int sum(int a, int b) { if (a + b < 0) return a + b + mod; if (a + b >= mod) return a + b - mod; return a + b; } ll binpow(ll a, ll n) { if (n == 0) return 1; if (n % 2 == 1) { return binpow(a, n - 1) * a % mod; } else { ll b = binpow(a, n / 2); return b * b % mod; } } vector<pii> g[N]; vector<int> ng[N], pss[N], poryadok; int dfr[N], dto[N], ds[N]; void dfs(int v, vector<int> &used){ used[v] = 1; for(auto u : pss[v]){ ng[u].pb(v); if(!used[u]) dfs(u, used); } } void dfs1(int v, vector<int> &used){ used[v] = 1; for(auto u : ng[v]){ if(!used[u]) dfs1(u, used); } poryadok.pb(v); } void solve(){ int n, m; cin >> n >> m; int s, t; cin >> s >> t; int fr, to; cin >> fr >> to; for(int i = 0; i < m; i++){ int u, v, w; cin >> u >> v >> w; g[u].pb({v, w}); g[v].pb({u, w}); } set<pii> st; for(int i = 1; i <= n; i++) ds[i] = infl, dfr[i] = infl, dto[i] = infl; ds[s] = 0; st.insert({ds[s], s}); while(!st.empty()){ int v = (*st.begin()).S; st.erase(st.begin()); for(auto e : g[v]){ int u = e.F, w = e.S; if(ds[u] == ds[v] + w) pss[u].pb(v); if(ds[u] > ds[v] + w){ st.erase({ds[u], u}); ds[u] = ds[v] + w; pss[u].clear(); pss[u].pb(v); st.insert({ds[u], u}); } } } dfr[fr] = 0; st.insert({dfr[fr], fr}); while(!st.empty()){ int v = (*st.begin()).S; st.erase(st.begin()); for(auto e : g[v]){ int u = e.F, w = e.S; if(dfr[u] > dfr[v] + w){ st.erase({dfr[u], u}); dfr[u] = dfr[v] + w; st.insert({dfr[u], u}); } } } dto[to] = 0; st.insert({dto[to], to}); while(!st.empty()){ int v = st.begin()->S; st.erase(st.begin()); for(auto e : g[v]){ int u = e.F, w = e.S; if(dto[u] > dto[v] + w){ st.erase({dto[u], u}); dto[u] = dto[v] + w; st.insert({dto[u], u}); } } } vector<int> used(n + 1, 0); dfs(t, used); used.assign(n + 1, 0); dfs1(s, used); reverse(all(poryadok)); vector<int> mn(n + 1, infl); vector<int> mn1(n + 1, infl); for(int i = 1; i <= n; i++) mn[i] = dfr[i], mn1[i] = dto[i]; int ans = dfr[to]; for(auto v : poryadok){ for(auto u : ng[v]){ mn[u] = min(mn[u], mn[v]); mn1[u] = min(mn1[u], mn1[v]); } ans = min(ans, min(mn[v] + dto[v], mn1[v] + dfr[v])); } cout << ans << '\n'; } signed main() { mispertion; int t = 1; //cin >> t; while(t--){ solve(); } 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...