Submission #1265466

#TimeUsernameProblemLanguageResultExecution timeMemory
1265466khoile08Commuter Pass (JOI18_commuter_pass)C++20
100 / 100
383 ms18260 KiB
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1e18;
const int N = 1e5 + 5;

int n, m, S, T, U, V;
long long ans;
vector<pair<int,int>> g[N];
long long d[4][N], dp[N];
bool vis[N];

void Dijkstra(int sr, int t) {
	priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> pq;
	pq.push({0, sr});
	for(int i = 1; i <= n; i++) d[t][i] = INF;
	d[t][sr] = 0;
	while(!pq.empty()) {
		long long cost = pq.top().first;
		int u = pq.top().second;
		pq.pop();
		if(cost != d[t][u]) continue;
		for(pair<int,int> H : g[u]) {
			int v = H.first;
			int w = H.second;
			if(d[t][v] > d[t][u] + w) {
				d[t][v] = d[t][u] + w;
				pq.push({d[t][v], v});
			}
		}
	}
}

void Dfs(int u, int t) {
	dp[u] = d[0][u];
	vis[u] = 1;
	for(pair<int,int> H : g[u]) {
		int v = H.first;
		int w = H.second;
		if(d[t][u] == d[t][v] + w) {
			if(!vis[v]) Dfs(v, t);
			dp[u] = min(dp[u], dp[v]);
		}
	}
	ans = min(ans, dp[u] + d[1][u]);
}

int main() {
    cin >> n >> m >> S >> T >> U >> V;
    for(int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }

	Dijkstra(U, 0);
	Dijkstra(V, 1);
	Dijkstra(S, 2);
	Dijkstra(T, 3);

	ans = INF;
	for(int i = 1; i <= n; i++) dp[i] = INF, vis[i] = 0;
	Dfs(T, 2);
	for(int i = 1; i <= n; i++) dp[i] = INF, vis[i] = 0;
	Dfs(S, 3);
	cout << min(d[0][V], 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...