๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 9370 : ๋ฏธํ™•์ธ ๋„์ฐฉ์ง€

by Jaeguk 2022. 1. 24.
๋ฌธ์ œ ๋งํฌ

9370๋ฒˆ: ๋ฏธํ™•์ธ ๋„์ฐฉ์ง€ (acmicpc.net)

 

9370๋ฒˆ: ๋ฏธํ™•์ธ ๋„์ฐฉ์ง€

(์ทจ์ต)B100 ์š”์›, ์š”๋ž€ํ•œ ์˜ท์ฐจ๋ฆผ์„ ํ•œ ์„œ์ปค์Šค ์˜ˆ์ˆ ๊ฐ€ ํ•œ ์Œ์ด ํ•œ ๋„์‹œ์˜ ๊ฑฐ๋ฆฌ๋“ค์„ ์ด๋™ํ•˜๊ณ  ์žˆ๋‹ค. ๋„ˆ์˜ ์ž„๋ฌด๋Š” ๊ทธ๋“ค์ด ์–ด๋””๋กœ ๊ฐ€๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์•Œ์•„๋‚ธ ๊ฒƒ์€ ๊ทธ๋“ค์ด s์ง€์ ์—์„œ

www.acmicpc.net

์–ด๋Š ๋ถ€๋ถ„์—์„œ ํ‹€๋ ธ๋Š”์ง€ ๊ฐ์ด ์•ˆ์™€์„œ ํ•œ์ฐธ์„ ํ—ค๋งจ ๋ฌธ์ œ์˜€๋‹ค.

 

ํ’€์ด

์ฒ˜์Œ์— ๋‚˜๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•œ ๋ฒˆ์œผ๋กœ ํ’€๋ ค๊ณ  ์‹œ๋„ํ–ˆ๋‹ค. ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ๋Œ๋ฉด์„œ ์ง€์ •๋œ ๋„๋กœ๋ฅผ ์ง€๋‚˜๊ฐ€๊ธฐ

์ „์—๋Š” ๋„์ฐฉ์ ์— false๋ฅผ ํ‘œ๊ธฐํ•˜๊ณ  ์ง€์ •๋œ ๋„๋กœ๋ฅผ ์ง€๋‚˜๊ฐ„ ํ›„ ๋ถ€ํ„ฐ๋Š” true๋ฅผ ํ‘œ๊ธฐํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ๋งŒ์•ฝ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋กœ ๋ชฉ์ ์ง€๋ฅผ ๊ฐ”์„ ๋•Œ true๋ผ๊ณ  ํ‘œ๊ธฐ๋ผ์žˆ์œผ๋ฉด ๊ทธ ๋ชฉ์ ์ง€๋Š” ์œ ํšจํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋‹จ ํ•˜๋ ค๊ณ 

ํ–ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;
 
int T;
int n, m, t;
int s, g, h;
const int MAX_N = 2000 + 5;
const int INF = INT_MAX;
vector<pair<intint>> Edge[MAX_N];
vector<int> goal;
vector<int> dist(MAX_N, INF);
bool is_passed[MAX_N];
 
void dijkstra(int st) {
    priority_queue<pair<pair<intint>bool>vector<pair<pair<intint>bool>>, greater< pair<pair<intint>,bool>>> pq;
    pq.push({{0,st}, false});
    while (!pq.empty()) {
        int now = pq.top().first.second;
        int cost = pq.top().first.first;
        bool is_pass = pq.top().second;
        pq.pop();
        if (dist[now] != INF) {
            if (dist[now] < cost)
                continue;
            else {
                is_passed[now] = is_pass;
            }
        }
        else {
            dist[now] = cost;
            is_passed[now] = is_pass;
        }
        for (int i = 0; i < Edge[now].size(); i++) {
            int next = Edge[now][i].first;
            int acost = cost + Edge[now][i].second;
            bool next_passed;
            if (now == g && next == h)
                next_passed = true;
            else if (now == h && next == g)
                next_passed = true;
            else next_passed = is_pass;
            if (dist[next] != INF) {
                if (dist[next] < acost)
                    continue;
                if (is_passed[next])
                    continue;
            }
            pq.push({ { acost,next }, next_passed });
        }
    }
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> T;
    while (T--) {
        cin >> n >> m >> t; //์ •์ , ๋…ธ๋“œ, ๋ชฉ์ ์ง€ ๊ฐœ์ˆ˜
        cin >> s >> g >> h; //์‹œ์ž‘์ , ์ •์ 1, ์ •์ 2
        int from, to, distance;
        for (int i = 0; i < m; i++) {
            cin >> from >> to >> distance;
            Edge[from].push_back({ to,distance });
            Edge[to].push_back({ from, distance });
        }
        for (int i = 0; i < t; i++) {
            cin >> to;
            goal.push_back(to);
        }
        fill(dist.begin(), dist.end(), INF);
        dijkstra(s);
        sort(goal.begin(), goal.end());
        for (int i = 0; i < goal.size(); i++) {
            if (dist[goal[i]] != INF && is_passed[goal[i]]) {
                cout << goal[i] << " ";
            }
        }
        cout << "\n";
        for (int i = 0; i <= n; i++) {
            Edge[i].clear();
        }
        goal.clear();
        memset(is_passed, falsesizeof(is_passed));
    }
    return 0;
}
cs

์–ธ๋œป ๋ณด๋ฉด ๋งž๋Š” ์ฝ”๋“œ๋ผ ๊ณ„์† ๋งž์™œํ‹€์ธ์ค„ ์•Œ์•˜๋‹ค. ๊ทธ๋Ÿฌ๋‹ค ์งˆ๋ฌธ๊ฒ€์ƒ‰์—์„œ ๋ฐ˜๋ก€๋ฅผ ์ฐพ๋‹ค๊ฐ€ ๊นจ๋‹ฌ์•˜๋‹ค. ๋งŒ์•ฝ ์ง€์ •๋œ ๋„๋กœ๋ฅผ ์ง€๋‚˜์„œ ๋ชฉ์ ์ง€๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ๊ฑฐ๋ฆฌ์™€ ์ง€๋‚˜์ง€ ์•Š๊ณ  ๋ชฉ์ ์ง€๊นŒ์ง€ ๋„๋‹ฌํ•œ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด? ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” true๊ฐ€ ๊ธฐ๋ก๋˜๊ณ  ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” false๊ฐ€ ๊ธฐ๋ก๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” false ์œ„์— true๋ฅผ ๋ง์น ํ•˜๋ ค ํ–ˆ์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์ ธ์„œ

๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.

๊ฒฐ๊ตญ ์•„์ด๋””์–ด๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค.

1. s๋ฅผ ์ถœ๋ฐœ์ ์œผ๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋Œ๋ฆฐ ํ›„ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ S[] ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

2. g๋ฅผ ์ถœ๋ฐœ์ ์œผ๋กœ G[] ๋ฐฐ์—ด์— ์ €์žฅ

3. h๋ฅผ ์ถœ๋ฐœ์ ์œผ๋กœ H[] ๋ฐฐ์—ด์— ์ €์žฅ

๊ทธ ํ›„ S->G->H->๋ชฉ์ ์ง€ ๋˜๋Š” S->H->G->๋ชฉ์ ์ง€ ์ด ๋‘ ์ฝ”์Šค์˜ ๊ฑฐ๋ฆฌ์™€ ์ตœ๋‹จ๊ฒฝ๋กœ์ธ S[๋ชฉ์ ์ง€]๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ทธ๋•Œ๋Š” ์œ ํšจํ•œ ๋ชฉ์ ์ง€๋‹ค ๋ผ๊ณ  ํŒ๋‹จ.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
#define INF  987654321
#define MAX_N 2010
using namespace std;
 
int T;
int n, m, t;
int s, g, h;
vector<pair<intint>> Edge[MAX_N];
vector<int> goal;
int S[MAX_N];
int H[MAX_N];
int G[MAX_N];
 
void dijkstra(int st, int Arr[MAX_N]) {
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    pq.push({ 0,st });
    while (!pq.empty()) {
        int now = pq.top().second;
        int cost = pq.top().first;
        pq.pop();
        if (Arr[now] != INF)
            continue;
        else Arr[now] = cost;
        for (int i = 0; i < Edge[now].size(); i++) {
            int next = Edge[now][i].first;
            int acost = cost + Edge[now][i].second;
            if (Arr[next] != INF)
                continue;
            else pq.push({ acost, next });
        }
    }
}
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> T;
    while (T--) {
        for (int i = 0; i < MAX_N; i++) {
            Edge[i].clear();
            S[i] = INF;
            G[i] = INF;
            H[i] = INF;
        }
        goal.clear();
 
        cin >> n >> m >> t; //์ •์ , ๋…ธ๋“œ, ๋ชฉ์ ์ง€ ๊ฐœ์ˆ˜
        cin >> s >> g >> h; //์‹œ์ž‘์ , ์ •์ 1, ์ •์ 2
 
        int from, to, distance;
        for (int i = 0; i < m; i++) {
            cin >> from >> to >> distance;
            Edge[from].push_back({ to,distance });
            Edge[to].push_back({ from, distance });
        }
        for (int i = 0; i < t; i++) {
            int a;  cin >> a;
            goal.push_back(a);
        }
        dijkstra(s,S);
        dijkstra(g,G);
        dijkstra(h,H);
        sort(goal.begin(), goal.end());
        for (int i = 0; i < goal.size(); i++) {
            int Goal = goal[i];
            if (S[Goal] == S[g] + G[h] + H[Goal]) // S->G->H->Goal
                cout << Goal << " ";
            else if (S[Goal] == S[h] + H[g] + G[Goal]) // S->H->G->Goal
                cout << Goal << " ";
        }
        cout << "\n";
    }
    return 0;
}
cs

๊ณ„์† ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋‚˜์™€ ํ•œ์ฐธ์„ ๊ณ ์ณ์„œ ๊ฒฐ๊ตญ AC๋ฅผ ๋ฐ›์•˜๋‹ค. ์•„์ง๋„ ์™œ ํ‹€๋ ธ์—ˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. 

728x90

'๋ฐฑ์ค€ > Gold' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] 2470 : ๋‘ ์šฉ์•ก  (0) 2022.02.02
[BOJ] 10217 : KCM Travel  (0) 2022.01.27
[BOJ] 1707 : ์ด๋ถ„ ๊ทธ๋ž˜ํ”„  (0) 2022.01.21
[BOJ] 1579 : ์•ฑ  (0) 2022.01.20
[BOJ] 10942 : ํŒฐ๋ฆฐ๋“œ๋กฌ?  (0) 2022.01.20