๋ฌธ์ ๋งํฌ
10217๋ฒ: KCM Travel (acmicpc.net)
10217๋ฒ: KCM Travel
๊ฐ๊ณ ์ ๋ ธ๋ ฅ ๋์ ์ฐฌ๋ฏผ์ด๋ 2014 Google Code Jam World Finals์ ์ง์ถํ๊ฒ ๋์๋ค. ๊ตฌ๊ธ์์ ์จ ์ด๋์ฅ์ ๋ฐ๊ณ ๊ธฐ๋ปํ๋ ๊ฒ๋ ์ ์, ์ฐฌ์ฐฌํ ์ฝ์ด๋ณด๋ ์ฐฌ๋ฏผ์ด๋ ์ค์ํ ์ฌ์ค์ ์์์ฐจ๋ ธ๋ค. ์ต๊ทผ์ ๋์ธ
www.acmicpc.net
DP์ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ํจ๊ป ํ์ฉํด์ผํ๋ ๋ฌธ์ ์๋ค.
ํ์ด
์ฒ์์ Dp์ ๊ทธ ์ง์ ๊น์ง ์์ ๋์ ์๊ฐ ๋๋ ๋น์ฉ์ ์ ์ฅํด์ ๋ค์ ๋ฒ์ ๋ค์ ๊ทธ ์ง์ ์ ๋์ฐฉํ์ ๋
์๊ฐ์ ์ผ๋ก ํน์ ๋น์ฉ์ ์ผ๋ก ๋ ๋นํจ์จ์ ์ด๋ผ๋ฉด ๋ฉ์ถ๊ณ ๋ค๋ฅธ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํ๋ ๋ฐฉ์์ ๋ ์ฌ๋ ธ๋ค.
์์ ์ ๋ฐฑํธ๋ํน ๋ฌธ์ ์์ ์ด๋ ๊ฒ ํ์๋ ๊ธฐ์ต์ด ๋์ ์ด๋ฒ์๋ ๋ฐฑํธ๋ํน์ผ๋ก ์ ๊ทผํ๋ค.
ํ์ง๋ง ๋ฐฑํธ๋ํน์ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ค ํ์ํ๋ค๋ณด๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค. ์ด๋ป๊ฒ๋ ์๊ฐ์ ์ค์ฌ๋ณด๋ ค ํ์ง๋ง ์๋๋ค.
๊ทธ๋์ ๋ค์ต์คํธ๋ผ๋ฅผ ์จ์ผ๊ฒ ๋ค๊ณ ์๊ฐ. Dp๋ฅผ ์ด๋ป๊ฒ ๋ค์ต์คํธ๋ผ์ ์ ๋ชฉ์ํฌ๊น?
์ฌ์ค ๋๊ฐ๋ค.
๊ฐ์ฅ ์งง์ ์์์๊ฐ์ ์ถ๋ ฅํด์ผ ํ๋ฏ๋ก ์ฐ์ ์์ํ๋ ์๊ฐ ๊ธฐ์ค ์งง์ ์์ผ๋ก ์ ๋ ฌ.
๋ง์ฝ ํ ์ง์ ์ ๋๋ฌํ์ ๋ ๊ฐ์ ๋์ ์ฌ์ฉํ๋ค๋ฉด ์๊ฐ์ด ์งง์ ์๋ก ์ข์ ๊ฒฝ๋ก์ด๋ค.
์ฆ Dp[์ ์ ][๋น์ฉ] ๋๋น ์๊ฐ์ด ์ ์ ์๋ก ์ ๋ฆฌํ ๊ฒฝ๋ก.
Dp[now][cost] < ํ์ฌ๊น์ง ๊ฑธ๋ฆฐ์๊ฐ ์ด๋ผ๋ฉด ํ์์ ๋ฉ์ถ๊ณ ๋ค์ ๊ฒฝ์ฐ๋ก ๊ฐ๋ค.
<์ฝ๋>
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;
const int MAX_N = 100 + 5;
const int INF = INT_MAX;
typedef priority_queue <pair<pair<int,int>, int>, vector<pair<pair<int,int>, int>>, greater<pair<pair<int,int>,int>>> Priority_queue;
int T, N, M, K;
vector<pair<pair<int, int>, int>> Edge[MAX_N];
int Dp[MAX_N][10000 + 5];
void dijkstra(int st) {
Priority_queue pq;
pq.push({ { 0,st }, 0 });
while (!pq.empty()) {
int now = pq.top().first.second;
int Time = pq.top().first.first;
int cost = pq.top().second;
pq.pop();
if (Dp[now][cost] < Time)
continue;
Dp[now][cost] = Time;
for (int i = 0; i < Edge[now].size(); i++) {
int next = Edge[now][i].first.first;
int acost = cost + Edge[now][i].first.second;
int atime = Time + Edge[now][i].second;
if (acost > M)
continue;
if (Dp[next][acost] <= atime)
continue;
for (int j = acost; j <= M; j++) {
if (Dp[next][j] > atime)
Dp[next][j] = atime;
}
pq.push({ {atime, next},acost });
}
}
}
void Init() {
for (int i = 0; i < MAX_N; i++) {
Edge[i].clear();
}
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < 10000 + 5; j++) {
Dp[i][j] = INF;
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
cin >> N >> M >> K;
Init();
int ans = INF;
for (int i = 0; i < K; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
Edge[u].push_back({ {v,c},d });
}
dijkstra(1);
for (int i = 0; i <= M; i++) {
ans = min(ans, Dp[N][i]);
}
if (ans == INF)
cout << "Poor KCM\n";
else cout << ans << "\n";
}
return 0;
}
|
cs |
๋ง์ง๋ง์ Dp[N]์ ์ ์ฅ๋ ๊ฐ์ค์ ๋น์ฉ์ M์ดํ๋ฉด ์ผ๋ง๋ ์๊ด์์ผ๋ฏ๋ก ๊ฐ์ฅ ์ ๊ฒ ๊ฑธ๋ฆฐ ์๊ฐ์ ์ถ๋ ฅํ๋ฉด ๋๋ค.
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 1806 : ๋ถ๋ถํฉ (1) | 2022.02.02 |
---|---|
[BOJ] 2470 : ๋ ์ฉ์ก (0) | 2022.02.02 |
[BOJ] 9370 : ๋ฏธํ์ธ ๋์ฐฉ์ง (0) | 2022.01.24 |
[BOJ] 1707 : ์ด๋ถ ๊ทธ๋ํ (1) | 2022.01.21 |
[BOJ] 1579 : ์ฑ (0) | 2022.01.20 |