๋ฌธ์ ๋งํฌ
9370๋ฒ: ๋ฏธํ์ธ ๋์ฐฉ์ง (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<int, int>> 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<int, int>, bool>, vector<pair<pair<int, int>, bool>>, greater< pair<pair<int, int>,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, false, sizeof(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<int, int>> 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<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> 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๋ฅผ ๋ฐ์๋ค. ์์ง๋ ์ ํ๋ ธ์๋์ง ๋ชจ๋ฅด๊ฒ ๋ค.
'๋ฐฑ์ค > 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 |