๋ฐฑ์ค€/Gold

[BOJ] 1800 : ์ธํ„ฐ๋„ท ์„ค์น˜

Jaeguk 2022. 4. 20. 13:17
๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1800

 

1800๋ฒˆ: ์ธํ„ฐ๋„ท ์„ค์น˜

์ฒซ ๋ฒˆ์งธ ์ค„์— N(1 ≤ N ≤ 1,000), ์ผ€์ด๋ธ”์„ ์˜ ๊ฐœ์ˆ˜ P(1 ≤ P ≤ 10,000), ๊ณต์งœ๋กœ ์ œ๊ณตํ•˜๋Š” ์ผ€์ด๋ธ”์„ ์˜ ๊ฐœ์ˆ˜ K(0 ≤ K < N)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ P๊ฐœ์˜ ์ค„์—๋Š” ์ผ€์ด๋ธ”์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ปดํ“จํ„ฐ ๋ฒˆํ˜ธ์™€ ๊ทธ ๊ฐ€๊ฒฉ์ด ์ฐจ

www.acmicpc.net

1๋ฒˆ๊ณผ N๋ฒˆ ์ปดํ“จํ„ฐ๋ฅผ ์—ฐ๊ฒฐ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋น„์šฉ์ค‘ ์ตœ๋Œ€๊ฐ’์˜ ์ตœ์†Œ๊ฐ€ ๋˜๋„๋กํ•˜๋Š” ๋ฌธ์ œ.

ํ’€์ด

์ผ๋‹จ 1๋ฒˆ๊ณผ N๋ฒˆ ์ปดํ“จํ„ฐ๋ฅผ ์ตœ๋Œ€ ํšจ์œจ๋กœ ์—ฐ๊ฒฐ์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ธฐ์กด ๋‹ค์ต์ŠคํŠธ๋ผ๋Š” ๋น„์šฉ์ด ๊ณ„์†ํ•ด์„œ ๋ˆ„์ ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ˆ„์  ๋น„์šฉ์ด ๊ฐ€์ž‘ ์ ๊ฒŒ ๋“ค๋„๋ก N์— ๋„๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ๋ณดํ†ต์ธ๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ๋น„์šฉ๋“ค ์ค‘ ๊ฐ€์žฅ ๋น„์‹ผ ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋งŒ ๋น„์šฉ์„ ์ง€๋ถˆํ•œ๋‹ค. ์ฆ‰ 1 -> N์„ ์—ฐ๊ฒฐ์‹œํ‚ค๋Š”๋ฐ ๊ฐ 4, 5, 6 ์˜ ๋น„์šฉ์ด ๋“ค์—ˆ๋”ฐ๊ณ  ํ•˜๋ฉด ์ œ์ผ ๋น„์‹ผ 6์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฐ’์„ ์ง€๋ถˆํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  K๊ฐœ์— ๋Œ€ํ•ด์„œ๋Š” ๋น„์šฉ์„ ๋ฐ›์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— K๊ฐœ๋Š” ๋น„์šฉ ์ƒ๊ด€์—†์ด ์—ฐ๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

์ผ๋‹จ ๋จผ์ € ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์ •๋ ฌ ์กฐ๊ฑด์€

1. ๊ฐ€๊ฒฉ์ด ์‹ผ ๊ฒƒ ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ์‹œํ‚จ๋‹ค.

2. ์—ฐ๊ฒฐํ•˜๋Š” ์„ ์˜ ์ˆ˜๊ฐ€ ์ ๋„๋ก ์—ฐ๊ฒฐ์‹œํ‚จ๋‹ค.

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

ํ•˜์ง€๋งŒ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ ๋ชจ๋‘ ์˜ˆ์ œ 1์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•˜์ง€ ๋ชปํ•œ๋‹ค.

1. ๊ฐ€๊ฒฉ์ด ์‹ผ ์ˆœ์œผ๋กœ ์ธํ„ฐ๋„ท์„ ์—ฐ๊ฒฐํ•œ๋‹ค.

1 -> 3 -> 4-> 5 ๋กœ ์—ฐ๊ฒฐํ•˜๊ฒŒ ๋˜๊ณ  ์ด๋•Œ ๋น„์šฉ์ด ๊ฐ๊ฐ 4, 7, 6 ์ธ๋ฐ 1๊ฐœ๋Š” ๊ณต์งœ๋กœ ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ 6์˜ ๋น„์šฉ์„ ์ง€๋ถˆํ•˜๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ 1 -> 3 -> 2 -> 5 ๋กœ ์—ฐ๊ฒฐํ•˜๋ฉด ๋น„์šฉ์ด 4, 3, 9 ์ธ๋ฐ 9์› ์งœ๋ฆฌ๋Š” ๊ณต์งœ๋กœ ํ•˜๋ฉด 4์˜ ๋น„์šฉ์„ ์ง€๋ถˆํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ์‹ผ ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์€ ํ•ด๊ฒฐ์ฑ…์ด ๋  ์ˆ˜ ์—†๋‹ค.

2. ์—ฐ๊ฒฐํ•˜๋Š” ์„ ์˜ ์ˆ˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ์—ฐ๊ฒฐํ•œ๋‹ค.

1 -> 2-> 5 ๋กœ ์—ฐ๊ฒฐํ•˜๋ฉด 2๋ฒˆ๋งŒ์— N์— ๋„๋‹ฌํ•œ๋‹ค. ์ด๋•Œ ๋น„์šฉ์€ 5, 9 ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  9์›์งœ๋ฆฌ๋ฅผ ๊ณต์งœ๋กœ ํ•˜๋ฉด 5์›์„ ์ง€๋ถˆํ•˜๊ฒŒ ๋œ๋‹ค. ์ด ์—ญ์‹œ 4์›๋ณด๋‹ค ๋น„์šฉ์ด ๋งŽ์ด๋“œ๋‹ˆ๊นŒ ์ •๋‹ต์ด ์•„๋‹ˆ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋งŒ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค. ์ด๋ถ„ํƒ์ƒ‰์„ ๊ฐ™์ด ์จ์„œ ํ’€์–ด์•ผํ•œ๋‹ค. ์ง€๋ถˆํ•˜๋Š” ๋น„์šฉ์„ mid๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. ๋ฏธ๋ฆฌ ์ •ํ•œ ๋น„์šฉ(mid)๋ณด๋‹ค ๋น„์‹ผ ์„ ์„ K๊ฐœ ์ดํ•˜๋กœ ์—ฐ๊ฒฐํ•˜๊ฒŒ ๋˜๋ฉด ๊ทธ๋•Œ mid ๊ฐ’์€ ์œ ํšจํ•œ ๊ฐ’์ด๋‹ค.

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
#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 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<intpair<int,int>>vector<pair<intpair<int,int>>>, greater<pair<intpair<int,int>>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 1000 + 5;
vector<pair<intint>> Edge[MAX_N];
vector<int> dist(MAX_N, INF);
int N, K, P;
 
bool dijkstra(int st, int boundary) {
    Priority_queue pq;
    pq.push({ 0 ,{st, 0} });
    while (!pq.empty()) {
        int now = pq.top().second.first;
        int over = pq.top().first;
        int cost = pq.top().second.second;
        pq.pop();
        if (cost > boundary)
            over++;
        if (over > K)
            continue;
        if (dist[now] <= over)
            continue;
        dist[now] = over;
        if (now == N)
            return true;
        for (int i = 0; i < Edge[now].size(); i++) {
            int next = Edge[now][i].first;
            int acost = Edge[now][i].second;
            if (dist[next] <= over)
                continue;
            pq.push({ over,{next, acost} });
        }
    }
    return false;
}
int main(void)
{
    int L = 0int R = -1;
    cin >> N >> P >> K;
    for (int i = 0; i < P; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        R = max(R, w);
        Edge[a].push_back({ b,w });
        Edge[b].push_back({ a,w });
    }
 
    int ans = INF;
    while (L <= R) {
        fill(dist.begin(), dist.end(), INF);
        int mid = (L + R) / 2;
        if (dijkstra(1, mid)) {
            R = mid - 1;
            ans = min(ans, mid);
        }
        else L = mid + 1;
    }
    if (ans == INF)
        cout << "-1\n";
    else cout << ans << "\n";
    return 0;
}
cs

์•„๋ฌด๋ฆฌ ๋งŽ์€ ์„ ์„ ์—ฐ๊ฒฐํ•˜๋”๋ผ๋„ ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘” ๊ฐ’ mid ๋ณด๋‹ค ๊ฐ’์ด ์‹ธ๋‹ค๋ฉด ๋ช‡ ๊ฐœ๋ฅผ ์—ฐ๊ฒฐํ•˜๋“  ์ƒ๊ด€์ด ์—†๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ์„ ์ˆœ์œ„ ํ ์ •๋ ฌ ๊ธฐ์ค€์„ mid ๋ณด๋‹ค ๋น„์‹ผ ์„ ์„ ์—ฐ๊ฒฐํ•œ ์ˆ˜๊ฐ€ ์ ์€ ๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ํ•ด์คฌ๋‹ค.

 

728x90