제출 #1339931

#제출 시각아이디문제언어결과실행 시간메모리
1339931nicolo_010Commuter Pass (JOI18_commuter_pass)C++20
31 / 100
209 ms25788 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 998244353;

struct Graph {
	vector<vector<pii>> adj;
	vector<ll> dis;
	int n;
	Graph(int _n) {
		n = _n;
		adj.assign(n, {});
	}
	void add_edge(int u, int v, int c) {
		//u->v
		adj[u].push_back({v, c});
	}
	ll dijkstra(int s, int t) {
		priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
		dis.assign(n, 1e18);
		pq.push({0, s});
		dis[s] = 0;
		while (!pq.empty()) {
			auto [w, u] = pq.top();
			pq.pop();
			if (w != dis[u]) continue;
			for (auto [v, p] : adj[u]) {
				if (dis[v] > w+p) {
					dis[v] = w+p;
					pq.push({dis[v], v});
				}
			}
		}
		return dis[t];
	}
};

void solve() {
	int n, m; cin >> n >> m;
	int s, t, u, v; cin >> s >> t;
	s--; t--;
	cin >> u >> v;
	u--; v--;
	Graph g(n);
	vector<pair<pii, int>> edges;
	for (int i=0; i<m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		edges.push_back({{a, b}, c});
		g.add_edge(a, b, c);
		g.add_edge(b, a, c);
	}
	vector<ll> disS, disT;
	ll D = g.dijkstra(s, t);
	disS = g.dis;
	g.dijkstra(t, s);
	disT = g.dis;
	Graph h(n);
	for (auto [e, w] : edges) {
		auto [a, b] = e;
		//cout << a << " " << b << " " << w << endl;
		if (disS[a] + w + disT[b] == D) {
			//cout << a << " " << b << endl;
			h.add_edge(a, b, 0);
		} 
		if (disS[b] + w + disT[a] == D) {
			//cout << b << " " << a << endl;
			h.add_edge(b, a, 0);
		}
		h.add_edge(a, b, w);
		h.add_edge(b, a, w);
	}
	cout << min(h.dijkstra(v, u), h.dijkstra(u, v)) << "\n";
}

//1 2 3 4 0

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	while (t--) {
		solve();
	}
	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...