제출 #860620

#제출 시각아이디문제언어결과실행 시간메모리
860620NursikCommuter Pass (JOI18_commuter_pass)C++17
15 / 100
2048 ms29780 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]; ll ans = inf * inf, dp[maxn], dp2[maxn], dp3[maxn], was[maxn]; vector<pair<int, int>> g[maxn], g3[maxn], g4[maxn]; vector<int> g2[maxn]; void dfsis(int v){ if (v == t){ is[v] = 1; } for (auto to : g2[v]){ 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; setik.insert(mp(0, s)); while (setik.size() > 0){ pair<ll, int> cur = *setik.begin(); setik.erase(cur); 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.insert(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; setik.insert(mp(0, ss)); while (setik.size() > 0){ pair<ll, int> cur = *setik.begin(); setik.erase(cur); 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.insert(mp(dp2[nv], nv)); } } } } void dijkstra3(){ for (int i = 1; i <= n; ++i){ dp3[i] = inf * inf; } dp3[tt] = 0; set<pair<ll, int>> setik; setik.insert(mp(0, tt)); while (setik.size() > 0){ pair<ll, int> cur = *setik.begin(); setik.erase(cur); 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.insert(mp(dp3[nv], nv)); } } } } void dfs(int v, ll mn = inf * inf, ll mn2 = inf * inf){ was[v] = 1; mn = min(mn, dp2[v]); mn2 = min(mn2, dp3[v]); ans = min(ans, mn + dp3[v]); ans = min(ans, mn2 + dp2[v]); /*if (ans == 3){ cout << v << '\n'; cout << mn << " " << dp3[v] << '\n'; cout << mn2 << " " << dp2[v] << '\n'; exit(0); }*/ for (auto to : g2[v]){ if (was[to] == 0){ dfs(to, mn, mn2); } } } 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(); dfs(s); 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...