Algorithm/BOJ

[백준 10217] KCM Travel (C++)

Henu 2021. 8. 12. 21:54
 

10217번: KCM Travel

각고의 노력 끝에 찬민이는 2014 Google Code Jam World Finals에 진출하게 되었다. 구글에서 온 초대장을 받고 기뻐했던 것도 잠시, 찬찬히 읽어보던 찬민이는 중요한 사실을 알아차렸다. 최근의 대세

www.acmicpc.net

다익스트라, 다이나믹 프로그래밍


 

 

[백준 17182] 우주 탐사선 (C++)

17182번: 우주 탐사선 우주 탐사선 ana호는 어떤 행성계를 탐사하기 위해 발사된다. 모든 행성을 탐사하는데 걸리는 최소 시간을 계산하려 한다. 입력으로는 ana호가 탐색할 행성의 개수와 ana호가

hyeo-noo.tistory.com

위 문제와 비슷한 다익스트라+DP문제였다. (주유소_13308 도 비슷하다)

 

하지만 이번 KCM문제는 최단 시간을 구하는것 뿐만 아니라 이동간의 비용이 추가되어 특정 비용 이내에 있는 최단 시간을 구해야 한다.

그래서 N에 도달했다고 바로 끝내지 않고 N에 도달할 수 있는 모든 비용에 해당하는 시간을 계산한 후 0부터 M까지의 비용중 가장 적게 걸린 시간을 출력하는식으로 답을 도출했다.

 

 

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
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<intint>
 
typedef long long ll;
// typedef pair<int, int> pii;
 
using namespace std;
 
struct IF{
    int dest, cost, time;
};
 
int N, M, K;
vector<IF> adj[10001];
int dist[101][10001];
 
void input(){
    for(int i = 0; i < 10001; i++){
        adj[i].clear();
    }
    
    cin >> N >> M >> K;
    int u, v, c, d;
    for(int i = 0; i < K; i++){
        cin >> u >> v >> c >> d;
        adj[u].push_back({v, c, d});
    }
    for(int i = 0; i <= 100; i++){
        for(int j = 0; j < 10001; j++){
            dist[i][j] = INF;
        }
    }
}
 
struct cmp{
    bool operator()(IF &a, IF &b){
        return a.cost > b.cost;
    }
};
 
void solve(){
    priority_queue<IF, vector<IF>, cmp> pq;
    pq.push({100});
    dist[1][0= 0;
    
    while(!pq.empty()){
        int now = pq.top().dest;
        int now_cost = pq.top().cost;
        int now_time = pq.top().time;
        pq.pop();
        
        if(dist[now][now_cost] < now_time || now_cost > M) continue;
        
        for(int i = 0; i < adj[now].size(); i++){
            int next = adj[now][i].dest;
            int next_cost = adj[now][i].cost + now_cost;
            int next_time = adj[now][i].time + now_time;
            
            if(dist[next][next_cost] <= next_time || next_cost > M) continue;
            dist[next][next_cost] = next_time;
            pq.push({next, next_cost, next_time});
            
        }
    }
}
 
int main(){
    int T;
    cin >> T;
    while(T--){
        input();
        solve();
        int res = INF;
        for(int i = 1; i <= M; i++){
            res = min(res, dist[N][i]);
        }
        if(res != INF) cout << res << "\n";
        else cout << "Poor KCM\n";
    }
    
    return 0;
}
 
cs