๋ฐฑ์ค€/C++

[๋ฐฑ์ค€/C++] 1162๋ฒˆ ๋„๋กœํฌ์žฅ

yulee_to 2022. 7. 16. 18:14

๋ฐฑ์ค€
๋ฐฑ์ค€
1162๋ฒˆ ๋„๋กœํฌ์žฅ


๐Ÿค”๋ฌธ์ œ ์ดํ•ด

1๋ฒˆ ์ •์ ์—์„œ N๋ฒˆ ์ •์ ๊นŒ์ง€ ๊ฐ€๋Š” ๋ชจ๋“  ๊ฒฝ๋กœ ์ค‘์— ์ตœ๋Œ€ k๊ฐœ์˜ ๋„๋กœ ์ด๋™ ์‹œ๊ฐ„์ด 0์ด ๋˜์—ˆ์„ ๋•Œ ์‹œ๊ฐ„์ด ๊ฐ€์žฅ ์ ๊ฒŒ ๋“œ๋Š” ๊ฒฝ๋กœ์˜ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.


๐Ÿ’ก์ฒซ๋ฒˆ์งธ ์•„์ด๋””์–ด

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

 

๐Ÿ”ฅํ’€์ด๐Ÿ”ฅ

์ตœ๋‹จ๊ฒฝ๋กœ ๋ฌธ์ œ์—์„œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๋Š” 1์ฐจ์› ๋ฐฐ์—ด์„ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ์–ด dist[i][j]์—์„œ j๋Š” ๋„๋กœ๋ฅผ ํฌ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ๊ตฌํ˜„ํ•˜๋˜ j๊ฐ’์ด ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ํฌ์žฅ๊ฐ€๋Šฅํ•œ ๋„๋กœ์˜ ๊ฐœ์ˆ˜ k๋ณด๋‹ค ์ž‘์„ ๋•Œ ๋„๋กœ๋ฅผ ํ•œ ๋ฒˆ ํฌ์žฅํ•œ ๊ฐ’์„ j+1์— ๋„ฃ์–ด์ฃผ๊ณ  ํฌ์žฅํ•˜์ง€ ์•Š์•˜์„ ๋•Œ์˜ ๊ฐ’์„ j์— ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.

์ •์  n์— ๋„์ฐฉํ•˜๋ฉด breakํ•ด์ฃผ๊ณ  ๋‚˜์„œ๋Š” ๋„๋กœ๋ฅผ ๋ช‡๋ฒˆ ํฌ์žฅํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ์ตœ์†Œ์ธ์ง€๋ฅผ ๋น„๊ตํ•ด์„œ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„์ค€๋‹ค. (k๋ฅผ ๋‹ค ์“ฐ์ง€ ์•Š๊ณ ๋„ ์ตœ์†Ÿ๊ฐ’์ผ ์ˆ˜ ์žˆ์Œ)

 

โŒํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ œ๋Œ€๋กœ ํ™•์ธํ•˜์ง€ ์•Š์•„ ๊ฐ’์ด ์ œ๋Œ€๋กœ ๋‹ด๊ธฐ์ง€ ์•Š์•˜์Œ


#include <iostream>
#include <vector>
#include <queue>
#include <cmath>

#define INF 90000000000
using namespace std;

int n, m, k;
long long dist[10002][21];
vector<vector<pair<int, int>>> edge;

int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= k; j++) {
            dist[i][j] = INF;
        }
    }
    int a, b, c;
    edge.resize(n + 1);

    for (int i = 0; i < m; i++) {
        cin >> a >> b >> c;
        edge[a].push_back({b, c});
        edge[b].push_back({a, c});
    }
    priority_queue<pair<long long, pair<int, int>>> pq; //๊ฑฐ๋ฆฌ, ์ •์ , ํฌ์žฅ๊ฐœ์ˆ˜
    pq.push({0, {1, 0}});

    while (!pq.empty()) {
        long long weight = -pq.top().first;
        int vertex = pq.top().second.first;
        int packed = pq.top().second.second;
        pq.pop();
        if (vertex == n) break;
        if (weight > dist[vertex][packed]) continue;

        for (auto e: edge[vertex]) {
            int next = e.first;
            long long cost;
            if (packed < k) {
                cost = 0;
                if (dist[next][packed + 1] > weight + cost) {
                    dist[next][packed + 1] = weight + cost;
                    pq.push({-(weight + cost), {next, packed + 1}});
                }
            }
            cost = e.second;
            if (dist[next][packed] > weight + cost) {
                dist[next][packed] = weight + cost;
                pq.push({-(weight + cost), {next, packed}});
            }
        }
    }
    long long min_cost = INF;
    for (int i = 0; i <= k; i++) {
        if (min_cost > dist[n][i]) min_cost = dist[n][i];
    }
    cout << min_cost;
}

 

๐Ÿ”—์ฐธ๊ณ ํ•  ๋ฌธ์ œ

 

[๋ฐฑ์ค€/C++] 1753๋ฒˆ ์ตœ๋‹จ๊ฒฝ๋กœ

๐Ÿค”๋ฌธ์ œ ์ดํ•ด ์‹œ์ž‘์ ์—์„œ ๊ฐ ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๐Ÿ’ก์ฒซ๋ฒˆ์งธ ์•„์ด๋””์–ด ๋ฌธ์ œ๊ฐ€ ๊ทธ๋ƒฅ ๋”ฑ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ๋ผ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ด์šฉํ–ˆ๋‹ค. ๐Ÿ”ฅํ’€์ด๐Ÿ”ฅ ๋‹ค์ต์ŠคํŠธ๋ผ

yulee.tistory.com

 

728x90