๋ฐฑ์ค€/C++

[๋ฐฑ์ค€/C++] 1939๋ฒˆ ์ค‘๋Ÿ‰์ œํ•œ

yulee_to 2022. 7. 18. 00:29

๋ฐฑ์ค€

 

1939๋ฒˆ ์ค‘๋Ÿ‰์ œํ•œ
1939๋ฒˆ ์ค‘๋Ÿ‰์ œํ•œ


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

ํ•œ ๊ณต์žฅ์—์„œ ๋‹ค๋ฅธ ๊ณต์žฅ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ ์ค‘์—์„œ ๊ทธ ๊ฒฝ๋กœ ์ƒ์— ์žˆ๋Š” ์ค‘๋Ÿ‰ ์ œํ•œ ๊ฐ’ ์ค‘์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ๊ณ , ๊ทธ ์ตœ์†Ÿ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฒƒ์„ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ฌธ์ œ์ด๋‹ค.


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

DFS๋ฅผ ํ†ตํ•ด ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•ด์„œ ๊ทธ ๊ฒฝ๋กœ์˜ ๊ฐ€์žฅ ์ž‘์€ ์ค‘๋Ÿ‰์ œํ•œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ๊ทธ ๊ฐ’๋“ค๋ผ๋ฆฌ ๋น„๊ตํ•ด์„œ ์ ค ํฐ ๊ฐ’์„ ์ฐพ์•„์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. 

 

โŒ์ฒซ๋ฒˆ์งธ ์ œ์ถœ

์„ฌ์ด 1๋งŒ๊ฐœ, ๋„๋กœ๊ฐ€ 10๋งŒ๊ฐœ๋กœ 10์–ต๋ฒˆ์„ ๋‹ค ๋Œ๊ฒŒ ๋˜๋Š” ์ฒซ๋ฒˆ์งธ ์•„์ด๋””์–ด ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.

 

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

์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผํ• ์ง€ ๋ง‰๋ง‰ํ•ด์„œ ์งˆ๋ฌธ์„ ํ–ˆ๋‹ค. ๋ฌธ์ œ์—์„œ ๋‚˜์™€์žˆ๋“ฏ ์ง์˜ ๋ฌด๊ฒŒ๋ณด๋‹ค ์ค‘๋Ÿ‰์ œํ•œ์ด ์ž‘์€ ๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๊ฐ€๋ ค๋Š” ๊ฒฝ์šฐ์—” ๋‹ค๋ฆฌ๊ฐ€ ๋ฌด๋„ˆ์ง„๋‹ค๊ณ  ํ–ˆ๋‹ค. ์ด ์ ์„ ํ™œ์šฉํ•ด์„œ ์ด๋ถ„ํƒ์ƒ‰์„ ์ด์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ด์ฃผ๋ฉด 1์ดˆ ์•ˆ์— ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค.

 

 

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

DFS + ์ด๋ถ„ํƒ์ƒ‰

๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•  ํ•„์š” ์—†์ด ์ค‘๋Ÿ‰ ์ œํ•œ๊ฐ’์„ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š” ๊ฒฝ๋กœ๋งŒ ์ฐพ์•„์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์„ ์ด์šฉํ•œ๋‹ค.

์ค‘๋Ÿ‰ ์ œํ•œ ๊ฐ’์„ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ /2์”ฉ ์ค„์—ฌ๊ฐ€๋ฉด์„œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„์ค€๋‹ค.

 

 

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

dfs๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ•  ๋•Œ ๋‹ค์Œ ์„ฌ, limit์„ ๋„˜๊ฒจ์ค˜์•ผ ํ•˜๋Š”๋ฐ ์ฒซ๋ฒˆ์งธ ์•„์ด๋””์–ด์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ dfs์˜ ์Šต๊ด€(?)์ด ๋‚จ์•„์„œ limit์ž๋ฆฌ์— ๋‹ค์Œ ์„ฌ์˜ ์ค‘๋Ÿ‰์ œํ•œ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋‹ค.


#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

vector<vector<pair<int, int>>> v;
bool visited[10002];
int n, m, a, b, c;

bool dfs(int now, int limit) {
    visited[now] = true;

    if (now == b) return true;

    for (auto x: v[now]) {
        int next = x.first;
        int cost = x.second;
        if (!visited[next] && limit <= cost) {
            if (!dfs(next, limit))continue;
            else return true;
        }
    }
    return false;
}

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

    cin >> n >> m;
    v.resize(n + 2);
    for (int i = 0; i < m; i++) {
        cin >> a >> b >> c;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }
    cin >> a >> b;

    int left = 0;
    int right = 1000000000;
    int mid, ans;
    while (left <= right) {
        memset(visited, false, sizeof(visited));
        mid = (left + right) / 2;
        if (dfs(a, mid)) {
            ans = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    cout << ans;
}

 

728x90