Submission #676734

#TimeUsernameProblemLanguageResultExecution timeMemory
676734baneCommuter Pass (JOI18_commuter_pass)C++14
15 / 100
2073 ms31184 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>
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 + 1];
	for (int i = 0; i<=N; i++){
		SPS[i] = SPU[i] = SPV[i] = 1e18;
		dp[i] = 1e18;
	}
	SPS[S] = 0;
	SPU[U] = 0;
	SPV[V] = 0;
	vector<int>parents[N + 1];
	priority_queue<pair<ll,int>>q;
	q.push(mp(0,S));
	while(!q.empty()){
		ll dst = -q.top().fr;
		int node = q.top().sc;
		q.pop();
		if (SPS[node] != dst)continue;
		for (auto edge : roads[node]){
			int city = edge.fr;
			int cost = edge.sc;
			if (SPS[city] > dst + cost){
				SPS[city] = dst + cost;
				parents[city].clear();
				parents[city].pb(node);
				q.push(mp(-SPS[city], city));
			}else if (SPS[city] == dst + cost){
				parents[city].pb(node);
			} 
		}
	}
	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));
			} 
		}
	}
	long long ans = SPU[V];
	//fix a node p, ans = min(ans, min(SPU[parents of p]) + SPV[p])
	//since we only know the parents of each node, this implies that we cannot use a bottom-up dynamic programming approach, 
	//we will have to use recursion
	//keep track of calculated nodes to avoid time limit or overflow problems
	int visited[N + 1];
	memset(visited, 0, sizeof(visited));
	
	function<void(int p, int from)>dinamicko = [&](int p, int from){
		//cout<<p<<"   "<<childs[p].size()<<endl;
		if (visited[p] >= N)return;
		visited[p]++;
		dp[p] = min(dp[p], SPU[p]);
		for (int parent : parents[p]){
			dp[parent] = min(dp[parent], dp[p]);
			dinamicko(parent, p);
			dp[p] = min(dp[p], dp[parent]); 
		}
		ans = min(ans, dp[p] + SPV[p]);
	};
	dinamicko(T, T);
	return ans;
}
 
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...