Submission #649765

#TimeUsernameProblemLanguageResultExecution timeMemory
649765RealSnakeCommuter Pass (JOI18_commuter_pass)C++17
55 / 100
322 ms23676 KiB
#include "bits/stdc++.h" #define ll long long using namespace std; int n, m, s, t, u, v; int a[100005]; ll dist[305][305]; vector<pair<int, ll>> node[100005]; ll ans; int par[100005]; ll dist2[100005]; void s1() { for(int k = 1; k <= n; k++) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; } } } ans = dist[u][v]; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(dist[s][i] + dist[i][j] + dist[j][t] == dist[s][t] || dist[s][j] + dist[j][i] + dist[i][t] == dist[s][t]) ans = min(ans, dist[u][i] + dist[j][v]); } } cout << ans; exit(0); } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> s >> t >> u >> v; if(n <= 300) { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) dist[i][j] = 1e18; dist[i][i] = 0; } } for(int i = 0; i < m; i++) { int a, b; ll c; cin >> a >> b >> c; node[a].push_back({b, c}); node[b].push_back({a, c}); if(n <= 300) dist[a][b] = dist[b][a] = c; } if(n <= 300) s1(); set<pair<ll, int>> ss; ss.insert({0, t}); // subtask 1 and 2 bool b[n + 1] = {}; b[v] = 1; for(int i = 1; i <= n; i++) dist2[i] = 1e18; dist2[t] = 0; while(ss.size()) { auto [dis, x] = *ss.begin(); ss.erase(ss.begin()); if(dis > dist2[x]) continue; if(x == s) break; for(auto [y, d] : node[x]) { if(dis + d < dist2[y]) { if(y != v) b[y] = b[x]; par[y] = x; dist2[y] = dis + d; ss.insert({dist2[y], y}); } else if(dis + d == dist2[y] && b[x]) { b[y] = b[x]; par[y] = x; } } } swap(s, t); while(t != s) { int sz = node[t].size(); for(int i = 0; i < sz; i++) { if(node[t][i].first == par[t]) { node[t][i].second = 0; break; } } sz = node[par[t]].size(); for(int i = 0; i < sz; i++) { if(node[par[t]][i].first == t) { node[par[t]][i].second = 0; break; } } t = par[t]; } ss.clear(); ss.insert({0, u}); for(int i = 1; i <= n; i++) dist2[i] = 1e18; dist2[u] = 0; while(ss.size()) { auto [dis, x] = *ss.begin(); ss.erase(ss.begin()); if(dis > dist2[x]) continue; if(x == v) { cout << dis; return 0; } for(auto [y, d] : node[x]) { if(dis + d < dist2[y]) { dist2[y] = dis + d; ss.insert({dist2[y], y}); } } } 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...