제출 #1166336

#제출 시각아이디문제언어결과실행 시간메모리
1166336merciless_lassieCommuter Pass (JOI18_commuter_pass)C++20
31 / 100
166 ms29736 KiB
#include "bits/stdc++.h" using namespace std; #define int long long const int INF = 1e18; int n, m; int s, t, u, v; vector<pair<int, int>> graph[100005]; // Find shortest path from src to all nodes vector<int> dijkstra(int src) { vector<int> dist(n + 1, INF); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; dist[src] = 0; pq.push({0, src}); while (!pq.empty()) { int d = pq.top().first; int node = pq.top().second; pq.pop(); if (d > dist[node]) continue; for (auto edge : graph[node]) { int next = edge.first; int weight = edge.second; if (dist[node] + weight < dist[next]) { dist[next] = dist[node] + weight; pq.push({dist[next], next}); } } } return dist; } int solve() { // Get shortest paths from various sources vector<int> dist_s = dijkstra(s); vector<int> dist_t = dijkstra(t); int shortest_s_to_t = dist_s[t]; // Build a new graph where edges on a shortest path from S to T have cost 0 vector<pair<int, int>> new_graph[100005]; for (int i = 1; i <= n; i++) { for (auto [next, weight] : graph[i]) { // Check if this edge (i-next) is on any shortest path from S to T bool on_shortest_path = (dist_s[i] + weight + dist_t[next] == shortest_s_to_t) || (dist_s[next] + weight + dist_t[i] == shortest_s_to_t); // Add the edge with appropriate cost (0 if on a shortest path) new_graph[i].push_back({next, on_shortest_path ? 0 : weight}); } } // Run Dijkstra from U to V on the new graph vector<int> dist(n + 1, INF); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; dist[u] = 0; pq.push({0, u}); while (!pq.empty()) { int d = pq.top().first; int node = pq.top().second; pq.pop(); if (d > dist[node]) continue; for (auto [next, weight] : new_graph[node]) { if (dist[node] + weight < dist[next]) { dist[next] = dist[node] + weight; pq.push({dist[next], next}); } } } return dist[v]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; cin >> s >> t >> u >> v; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; graph[a].push_back({b, c}); graph[b].push_back({a, c}); } cout << solve() << endl; 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...