제출 #746067

#제출 시각아이디문제언어결과실행 시간메모리
746067vjudge1Cities (BOI16_cities)C++17
14 / 100
383 ms17744 KiB
#include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; using ll = long long; struct edge { int to = 1; ll w; bool operator<(const edge& e)const { return w > e.w; } }; const int maxn = 100010; vector<edge> g[maxn]; void dijkstra(int a, vector<ll>& da) { priority_queue<edge> q; q.push({ a, 0 }); da[a] = 0; while (!q.empty()) { edge e = q.top(); q.pop(); if (da[e.to] < e.w)continue; for (edge& i : g[e.to]) { if (da[i.to] < 0 || da[i.to] > da[e.to] + i.w) { da[i.to] = da[e.to] + i.w; q.push({ i.to, da[i.to] }); } } } return; } int main() { int n, k, m; cin >> n >> k >> m; int a, b, c; if(k==3) cin >> a >> b >> c; else cin >> a >> b; for (int i = 0; i < m; i++) { int a, b; ll w; cin >> a >> b >> w; g[a].push_back({ b, w }); g[b].push_back({ a, w }); } vector<ll> da(n + 10, -1), db(n + 10, -1), dc(n + 10, -1); dijkstra(a, da); dijkstra(b, db); if(k==3)dijkstra(c, dc); else { cout << da[b] << '\n'; return 0; } ll ans = INT64_MAX; for (int i = 1; i <= n; i++) { ans = min(ans, da[i] + db[i] + dc[i]); } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...