제출 #159900

#제출 시각아이디문제언어결과실행 시간메모리
159900XCoderCommuter Pass (JOI18_commuter_pass)C++14
0 / 100
658 ms17376 KiB
#include <bits/stdc++.h> #define FOR(i,s,e) for(int i=(s);i<(int)(e);i++) #define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++) #define REP(i,n) FOR(i,0,n) #define ALL(x) (x).begin(), (x).end() #define CLR(s) memset(s,0,sizeof(s)) #define PB push_back #define ITER(v) __typeof((v).begin()) #define FOREACH(i,v) for(ITER(v) i=(v).begin();i!=(v).end();i++) #define x first #define y second using namespace std; typedef long long LL; typedef pair<int,int> pii; typedef pair<LL, LL> pll; typedef map<int,int> mii; typedef vector<int> vi; const LL INF = 1LL << 40; using Graph = vector<vector<pii>>; using Dist = vector<LL>; using Node = tuple<LL, int>; // <cost, node_id> int N, M, S, T, U, V; Graph G; Dist compute_shortest_path(Graph &G, vector<int> src_nodes, int N) { Dist D(N + 1, INF); priority_queue<Node, vector<Node>, greater<Node>> pq; for (auto &src : src_nodes) { D[src] = 0; pq.push({0, src}); } while (!pq.empty()) { LL cur; int x; tie(cur, x) = pq.top(); pq.pop(); if (D[x] > cur) continue; for (auto &it : G[x]) { LL cost; int y; tie(y, cost) = it; if (cur + cost < D[y]) { D[y] = cur + cost; pq.push({D[y], y}); } } } return D; } int main() { int A, B, C; cin >> N >> M >> S >> T >> U >> V; G.resize(N + 1); FOR(_, 0, M) { cin >> A >> B >> C; G[A].PB({B, C}); G[B].PB({A, C}); } Dist src_dist = compute_shortest_path(G, {S}, N); Dist dst_dist = compute_shortest_path(G, {T}, N); //cout << src_dist[T] << " " << dst_dist[S] << endl; LL shortest_path_cost = src_dist[T]; assert (src_dist[T] == dst_dist[S]); vector<int> shortest_path_nodes; FOE(x, 1, N) if (src_dist[x] + dst_dist[x] == shortest_path_cost) shortest_path_nodes.PB(x); Dist final_dist = compute_shortest_path(G, shortest_path_nodes, N); LL ans = final_dist[U] + final_dist[V]; cout << ans << 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...