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

[BOJ] 13308 : ์ฃผ์œ ์†Œ

by Jaeguk 2022. 3. 15.
๋ฌธ์ œ ๋งํฌ

13308๋ฒˆ: ์ฃผ์œ ์†Œ (acmicpc.net)

 

13308๋ฒˆ: ์ฃผ์œ ์†Œ

ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‹ค์Œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋„์‹œ์˜ ์ˆ˜์™€ ๋„๋กœ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ N(2 ≤ N ≤ 2,500)๊ณผ ์ •์ˆ˜ M(1 ≤ M ≤ 4,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์— ๊ฐ ๋„์‹œ ์ฃผ์œ ์†Œ์˜ ๋ฆฌํ„ฐ๋‹น ๊ฐ€๊ฒฉ์ด ๋„

www.acmicpc.net

DP์™€ ๊ทธ๋ž˜ํ”„ํƒ์ƒ‰ ์ค‘ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ฒฐํ•ฉ๋œ ๋ฌธ์ œ.

์‚ฌ์‹ค DP๋ณด๋‹ค๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ฐ€๊นŒ์šด ๊ฒƒ๊ฐ™๋‹ค.

ํ’€์ด

์ฒ˜์Œ์— ์›๋ž˜ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฒ˜๋Ÿผ dist๋ผ๋Š” 1์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ตœ์†Œ๋น„์šฉ์„ ์ €์žฅํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์˜ˆ์ œ์ฒ˜๋Ÿผ

์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ dist[3] ์—๋Š” 1->3 ๊ฒฝ๋กœ๋กœ ์ธํ•ด 15๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๊ณ , 1 -> 2 -> 3 -> 4 ๋กœ ๊ฐ€๋Š” ๋ฃจํŠธ๊ฐ€ ์ตœ์†Œ๋น„์šฉ์œผ๋กœ 4์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ๋กœ์ด์ง€๋งŒ 1 ->2-> 3 ๊ฒฝ๋กœ๋กœ 3์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๋ฉด 16์˜ ๋น„์šฉ์ด ๋“œ๋Š”๋ฐ ๊ธฐ์กด์— ์ €์žฅ๋œ ๊ฐ’๋ณด๋‹ค ํฌ๊ธฐ๋•Œ๋ฌธ์— ํƒ์ƒ‰์„ ๋ฉˆ์ถ”๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ž˜์„œ ๋‹จ์ง€ ๊ทธ ์ง€์ ์— ๋„์ฐฉํ–ˆ์„ ๋•Œ ์ตœ์†Œ๋น„์šฉ๋งŒ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ธฐ๋ฆ„์˜ ๊ฐ’๋„ ๊ฐ™์ด ๋น„๊ต๋ฅผ ํ•ด์„œ ํƒ์ƒ‰์„ ๊ณ„์†ํ•  ๊ฒƒ์ธ์ง€ ์ค‘๋‹จํ•  ๊ฒƒ์ธ์ง€ ํŒ๋‹จํ•ด์•ผํ–ˆ๋‹ค.

dist ๋ฐฐ์—ด์„ 2์ฐจ์›์œผ๋กœ ๋งŒ๋“ค์–ด์„œ dist[ํ˜„์žฌ ๋…ธ๋“œ][ํ˜„์žฌ ๊ธฐ๋ฆ„ ๊ฐ’] ์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ์ด ๋น„์šฉ๊ณผ ๊ธฐ๋ฆ„๊ฐ’ ๋‘ ๊ฐ€์ง€๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํšจ์œจ์ ์ธ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ–ˆ๋‹ค.

๋งŒ์•ฝ ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ์˜ ์ด ๋น„์šฉ์ด ๊ธฐ์กด ํƒ์ƒ‰๋œ ๊ฒฝ๋กœ๋ณด๋‹ค ๋งŽ์•„๋„ ํ˜„์žฌ ๊ธฐ๋ฆ„๊ฐ’์ด ๊ธฐ์กด ๊ฒฝ๋กœ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๋ฉ€๋ฆฌ๋ดค์„ ๋•Œ๋Š” ๋” ํšจ์œจ์ ์ธ ๊ฒฝ๋กœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef priority_queue<pair<pair<ll, int>int>vector<pair<pair<ll, int>int>>, greater<pair<pair<ll, int>int>>> Priority_queue;
const ll INF = LLONG_MAX;
const int MAX_N = 2500 + 5;
int oil_price[MAX_N];
int N, M;
vector<pair<intint>> Edge[MAX_N];
ll dist[MAX_N][MAX_N];
void dijkstra(int st, int ed);
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    for (int i = 1; i <= N; i++) {
        cin >> oil_price[i];
    }
    for (int i = 0; i < M; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        Edge[u].push_back({ v,w });
        Edge[v].push_back({ u,w });
    }
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= 2500; j++)
            dist[i][j] = INF;
    }
    dijkstra(1, N);
    return 0;
}
 
void dijkstra(int st, int ed) {
    //ํ์— ํ˜„์žฌ์ , ์—ฌํƒœ ์ง€๋‚˜์˜จ ์  ์ค‘์— ๊ธฐ๋ฆ„์ด ๊ฐ€์žฅ ์ŒŒ๋˜๊ณณ, ๋น„์šฉ ์ด ๋“ค์–ด์žˆ์–ด์•ผํ•จ
    Priority_queue pq;
    pq.push({ {0,st},oil_price[1] });
    while (!pq.empty()) {
        ll cost = pq.top().first.first;
        int now = pq.top().first.second;
        int price = pq.top().second;
        pq.pop();
        if (dist[now][price] < cost)
            continue;
        if (now == ed) {
            cout << cost << "\n";
            return;
        }
        for (int i = 0; i < Edge[now].size(); i++) {
            int next = Edge[now][i].first;
            int distance = Edge[now][i].second;
            ll acost = cost + (distance * price);
            if (dist[next][price] <= acost)
                continue;
            int min_price = min(price, oil_price[next]);
            dist[next][price] = acost;
            pq.push({ { acost,next }, min_price});
        }
    }
}
cs

728x90