Submission #1293768

#TimeUsernameProblemLanguageResultExecution timeMemory
1293768notbrainingCommuter Pass (JOI18_commuter_pass)C++20
15 / 100
413 ms327680 KiB
//#pragma GCC optimize ("O3") #include<iostream> #include<vector> #include<set> #include<map> #include<iomanip> #include <cassert> #include<algorithm> #include <cstring> #include<unordered_set> #include<queue> #include <array> #include<cmath> #include<unordered_map> #include<queue> #include <list> #include <bitset> using namespace std; #define int long long #define pii pair<int,int> #define LL long long //#pragma GCC optimize("O3,unroll-loops") //#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt”) int n, m, s, t, u, v; int INF = 1e18 / 3; vector<vector<pii>>adj; void dijkstras(vector<int>& startnodes, vector<int>& dists, vector<vector<int>>& reachedfrom){ reachedfrom = vector<vector<int>>(n + 1); dists = vector<int>(n + 1, INF); priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>>pq; for(int j : startnodes){ dists[j] = 0; pq.push({0, {j, -1}}); } while(!pq.empty()){ auto [d, foo] = pq.top(); auto [curr, visfrom] = foo; pq.pop(); if(d <= dists[curr]){ dists[curr] = d; reachedfrom[curr].push_back(visfrom); for(auto [j, C] : adj[curr]){ pq.push({C + d, {j, curr}}); } } } } vector<int>t_dists, s_dists, v_dists; vector<vector<int>>t_reachedfrom, s_reachedfrom, v_reachedfrom; void solve0(){ vector<int>tt = {t}; vector<int>ss = {s}; vector<int>vv = {v}; dijkstras(tt, t_dists, t_reachedfrom); dijkstras(ss, s_dists, s_reachedfrom); dijkstras(vv, v_dists, v_reachedfrom); } void solve1(){ //S =U, so int totaldist = s_dists[t]; cout << totaldist << endl; int ans = INF; for(int i = 1; i <= n; ++i){ if(s_dists[i] + t_dists[i] == totaldist){ ans = min(ans, v_dists[i]); } } cout << ans; } void solve2(){ //multi-source Dijkstras after finding S-T nodes int totaldist = s_dists[t]; vector<int>multisource; for(int i = 1; i <= n; ++i){ if(s_dists[i] + t_dists[i] == totaldist){ multisource.push_back(i); } } vector<int>multisource_dists; vector<vector<int>>multisource_visfrom; dijkstras(multisource, multisource_dists, multisource_visfrom); cout << min(multisource_dists[u] + multisource_dists[v], v_dists[u]); } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> s >> t >> u >> v; adj = vector<vector<pii>>(n + 1); for(int i = 0; i < m; ++i){ int a, b, c; cin >> a >> b >> c; adj[a].push_back({b, c}); adj[b].push_back({a, c}); } solve0(); if(s == u){ solve1(); } else{ solve2(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...