-
[9370] 미확인 도착지BOJ 2022. 3. 29. 00:10
https://www.acmicpc.net/problem/9370
9370번: 미확인 도착지
(취익)B100 요원, 요란한 옷차림을 한 서커스 예술가 한 쌍이 한 도시의 거리들을 이동하고 있다. 너의 임무는 그들이 어디로 가고 있는지 알아내는 것이다. 우리가 알아낸 것은 그들이 s지점에서
www.acmicpc.net
풀이는 크게 2가지가 가능한데, 구간을 잘라서 다익스트라를 그대로 여러번 돌리는 방법이 있고
그래프를 적절히 조작하여 "[1445] 일요일 아침의 데이트"처럼 푸는 방법이 있다.
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<ll, ll>;constexpr ll MAX_N = 2000, INF = (ll)1e17;ll n, m, t, s, g, h, e, path[MAX_N + 1];double dst[MAX_N + 1];typedef struct {ll idx;double cost;} state;vector<state> adj[MAX_N + 1];void dijkstra(ll s) {auto cmp = [&](state& A, state& B) -> bool { return A.cost > B.cost; };priority_queue<state, vector<state>, decltype(cmp)> q(cmp);q.push({s, 0});for (ll i = 0; i <= MAX_N; i++) dst[i] = (double)(10000000.0);dst[s] = 0;
while (!q.empty()) {state cur = q.top();q.pop();if (cur.cost > dst[cur.idx]) continue;
for (auto& nxt : adj[cur.idx]) {if (dst[nxt.idx] > dst[cur.idx] + nxt.cost) {dst[nxt.idx] = dst[cur.idx] + nxt.cost;q.push({nxt.idx, dst[nxt.idx]});path[nxt.idx] = cur.idx;}}}return;}void findPath(vector<ll>& ret) {ll cur = e;path[s] = -1;for (;; cur = path[cur]) {if (cur <= 0) break;ret.push_back(cur);}reverse(ret.begin(), ret.end());return;}void init(void) {ll i;for (i = 0; i <= MAX_N; i++) adj[i].clear();}int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);ll tc, i, j;cin >> tc;while (tc--) {init();cin >> n >> m >> t >> s >> g >> h;for (i = 0; i < m; i++) {ll A, B;double C;cin >> A >> B >> C;if (A == g && B == h || B == g && A == h) C -= (0.5);adj[A].push_back({B, C});adj[B].push_back({A, C});}dijkstra(s);vector<ll> p;findPath(p);vector<ll> ans;for (i = 0; i < t; i++) {ll tar;cin >> tar;if (dst[tar] - (int)dst[tar] > 0.3) ans.push_back(tar);}sort(ans.begin(), ans.end());for (auto& i : ans) cout << i << ' ';cout << '\n';}return 0;}
'BOJ' 카테고리의 다른 글
[3649] 로봇 프로젝트 (0) 2022.03.30 [2458] 키 순서 (0) 2022.03.29 [1719] 택배 (0) 2022.03.29 [1038] 감소하는 수 (0) 2022.03.26 [5637] 가장 긴 단어 (0) 2022.03.25