Submission #676747

#TimeUsernameProblemLanguageResultExecution timeMemory
676747baneCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
388 ms29240 KiB
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <unordered_set> #include <vector> #include <climits> #include <list> #include <stack> using namespace std; using ll = long long; using db = long double; // or double, if TL is tight using llu = unsigned long long; using str = string; // yay python! // pairs using pii = pair<int,int>; using pll = pair<ll,ll>; using pdb = pair<db,db>; #define mp make_pair #define fr first #define sc second #define tcT template<class T #define tcTU tcT, class U // ^ lol this makes everything look weird but I'll try it tcT> using V = vector<T>; tcT, size_t SZ> using AR = array<T,SZ>; using vi = V<int>; using vb = V<bool>; using vl = V<ll>; using vd = V<db>; using vs = V<str>; using vpi = V<pii>; using vpl = V<pll>; using vpd = V<pdb>; #define ms0(x) memset(x , 0, sizeof(x)) // vectors // oops size(x), rbegin(x), rend(x) need C++17 #define sz(x) int((x).size()) #define bg(x) begin(x) #define all(x) bg(x), end(x) #define rall(x) x.rbegin(), x.rend() #define sor(x) sort(all(x)) #define rsz resize #define ins insert #define pb push_back #define eb emplace_back #define ft front() #define bk back() #define lb lower_bound #define ub upper_bound tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); } tcT> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); } // loops #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define rep(a) F0R(_,a) #define each(a,x) for (auto& a: x) const int MOD = (int)1e9+7; // 998244353; const int MX = (int)2e5+5; const ll BIG = 1e18; // not too close to LLONG_MAX const db PI = acos((db)-1); const int dx[4] = {0,1,0,-1}; const int dy[4] = {-1,0,1,0}; long long Commuter_Pass(int N, int M, int S, int T, int U, int V, vector<vector<pair<int,int>>>roads){ ll SPS[N + 1], SPU[N + 1], SPV[N + 1]; long long dp[N + 100][2]; for (int i = 0; i<=N; i++){ SPS[i] = SPU[i] = SPV[i] = 1e18; dp[i][0] = dp[i][1] = 1e18; } SPS[S] = 0; SPU[U] = 0; SPV[V] = 0; priority_queue<pair<ll,int>>q; q.push(mp(0,U)); while(!q.empty()){ ll dst = -q.top().fr; int node = q.top().sc; q.pop(); if (SPU[node] != dst)continue; for (auto edge : roads[node]){ int city = edge.fr; int cost = edge.sc; if (SPU[city] > dst + cost){ SPU[city] = dst + cost; q.push(mp(-SPU[city], city)); } } } q.push(mp(0,V)); while(!q.empty()){ ll dst = -q.top().fr; int node = q.top().sc; q.pop(); if (SPV[node] != dst)continue; for (auto edge : roads[node]){ int city = edge.fr; int cost = edge.sc; if (SPV[city] > dst + cost){ SPV[city] = dst + cost; q.push(mp(-SPV[city], city)); } } } priority_queue<pair<ll,pair<int,int>>>pq; pq.push(mp(0, mp(S,S))); int visited[N + 1]; memset(visited, 0, sizeof(visited)); while(!pq.empty()){ ll dst = -pq.top().fr; int node = pq.top().sc.fr; int par = pq.top().sc.sc; pq.pop(); if (!visited[node]){ visited[node] = 1; SPS[node] = dst; dp[node][1] = min(dp[par][1], SPV[node]); dp[node][0] = min(dp[par][0], SPU[node]); for (auto next : roads[node]){ pq.push(mp(-(dst + next.sc), mp(next.fr,node))); } }else if (dst == SPS[node]){ if (dp[node][1] + dp[node][0] >= min(dp[par][1], SPV[node]) + min(dp[par][0], SPU[node])){ dp[node][1] = min(dp[par][1], SPV[node]); dp[node][0] = min(dp[par][0], SPU[node]); } } } long long ans = SPU[V]; ans = min(ans, dp[T][1] + dp[T][0]); for (int i = 1; i<=N; i++){ SPS[i] = 1e18; dp[i][1] = 1e18; dp[i][0]= 1e18; } SPS[T] = 0; pq.push(mp(0, mp(T,T))); memset(visited, 0, sizeof(visited)); while(!pq.empty()){ ll dst = -pq.top().fr; int node = pq.top().sc.fr; int par = pq.top().sc.sc; pq.pop(); if (!visited[node]){ visited[node] = 1; SPS[node] = dst; dp[node][1] = min(dp[par][1], SPV[node]); dp[node][0] = min(dp[par][0], SPU[node]); for (auto next : roads[node]){ pq.push(mp(-(dst + next.sc), mp(next.fr,node))); } }else if (dst == SPS[node]){ if (dp[node][1] + dp[node][0] >= min(dp[par][1], SPV[node]) + min(dp[par][0], SPU[node])){ dp[node][1] = min(dp[par][1], SPV[node]); dp[node][0] = min(dp[par][0], SPU[node]); } } } return min(ans, dp[S][1] + dp[S][0]); } int main() { ios::sync_with_stdio(0); cin.tie(0); int N, M, S, T, U, V; cin >> N >> M >> S >> T >> U >> V; vector<vector<pair<int,int>>>edges(N + 1); for (int i = 0; i<M; i++){ int a,b,c; cin >> a >> b >> c; edges[a].pb(mp(b,c)); edges[b].pb(mp(a,c)); } cout<<Commuter_Pass(N,M,S,T,U,V,edges); 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...