Skip to content

Conversation

@kokeunho
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

[BOJ] ์ค‘๋Ÿ‰์ œํ•œ https://www.acmicpc.net/problem/1939

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์–ด์ œ ์˜ค๋žœ๋งŒ์— ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ 
๋น„์Šทํ•œ ์œ ํ˜•์œผ๋กœ ํ’€์–ด๋ณด๋ ค๊ณ  ๊ณ ๋ฅธ ๋ฌธ์ œ์ธ๋ฐ
์ผ๋ฐ˜์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ๋ž‘ ๋‹ฌ๋ผ์„œ ๋ช‡๋ฒˆ ์‹คํŒจํ•˜๊ณ 
๊ฒฐ๊ตญ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ด๋ถ„ ํƒ์ƒ‰ + bfs ํ’€์ด๋กœ ํ’€์–ด์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค.
(๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ๋‹ค์‹œ ํ’€์–ด์„œ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค๋งŒ ์ด๋ฒˆ ํ’€์ด๊ฐ€ ๋” ์ข‹์•„๋ณด์—ฌ์„œ ํ•ด๋‹น ํ’€์ด๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.)

bfs ๋ฉ”์„œ๋“œ๋Š” A์—์„œ B๊นŒ์ง€ mid ์ค‘๋Ÿ‰์„ ์‹ค์€์ฑ„๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ด๋•Œ mid ์ค‘๋Ÿ‰์„ ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ง‰์ƒ ์ˆ˜๋„ ์ฝ”๋“œ ์“ฐ๋ ค๋‹ˆ ์“ธ๊ฒŒ ์—†๋„ค์š”. ์ด์ƒ์ž…๋‹ˆ๋‹ท.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค๋žœ๋งŒ์— ๋…ธ๋“œ ๊ฐ„์„ ์œ ํ–‰์˜ bfs๋ฅผ๋งŒ๋‚˜ ๋ฐ˜๊ฐ€์› ์Šต๋‹ˆ๋‹ค!

๋จผ์ € ๋ฌธ์ œ๋ฅผ๋ณด๊ณ  ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์œผ๋ผํ•ด์„œ dfs๋กœ ๋ชจ๋“ ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ๋‹ค ์ฒดํฌํ•ด๋ณด๊ณ  ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„๋ณด๋ ค ํ–ˆ์ง€๋งŒ ์ตœ๋Œ€๊ฐ’์ด 10์–ต์ธ๊ฑธ๋ณด๊ณ  ์ด๋ถ„ํƒ์ƒ‰์„ ์จ์•ผํ•˜๋‚˜? ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋งŒ ์ด๋ฅผ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ์ตœ๋Œ€๊ฐ’์„ ์ •ํ•˜๊ณ , ์ง์ ‘ bfs๋กœ ๋Œ€์ž…ํ•ด๋ณด๋Š” ๊ตฌ์กฐ๋กœ ํ’€ ์ƒ๊ฐ์„ ํ•˜๊ธฐ๊นŒ์ง€๋Š” ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ๋˜๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ๊ฐ€ ์–ด๋А์ •๋„ ์‰ฌ์› ๋˜ ์ด์œ ๋Š” ์‹œ์ž‘์ง€์ ๊ณผ ๋์ง€์ ์„ ์ด๋ฏธ ์ •ํ•ด์ฃผ์—ˆ๊ธฐ๋•Œ๋ฌธ์— bfs๋ฅผ ๋Œ๋ฆด๋–„๋Š” ์‚ฌ์‹ค ๊ฐ€์ค‘์น˜๋งŒ ๋„ฃ์–ด์ฃผ๋ฉด ๋์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์‚ฌ์‹ค ์ด๋ถ€๋ถ„์ด ์‰ฌ์šฐ๋ฉด์„œ๋„ ์‹œ๊ฐ„์„ ๋งŽ์ด ์žก์•„๋จน์—ˆ๋Š”๋ฐ, bfs๋Š”๋ณดํ†ต ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์—†์„๋–„ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ด๋ฌธ์ œ๋Š” visited์กฐ๊ฑด ์ด์™ธ์—๋„ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฒ„ํ‹ธ์ˆ˜ ์žˆ๋Š”์ง€์˜ ์กฐ๊ฑด์„ ๋”ํ•ด bfs๊ฒฝ๋กœ๋ฅผ ์ค‘๊ฐ„์— ์ฐจ๋‹จํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ–ˆ๊ธฐ ๋–„๋ฌธ์ž…๋‹ˆ๋‹ค.

์‹ ๋ฐ•ํ•œ ๋ฌธ์ œ ์ž˜๋ณด๊ณ ๊ฐ‘๋‹ˆ๋‹ค!

import sys
from collections import deque

N, M = map(int, input().split())
edges = [[] for _ in range(N+1)]
for _ in range(M):
    a, b, c = map(int, input().split())
    edges[a].append((b, c))
    edges[b].append((a, c))
start, end = map(int, input().split())

def bfs(weight):
    visited = [False] * (N+1)
    queue = deque()
    queue.append(start)
    visited[start] = True

    while queue:
        now = queue.popleft()
        if now == end:
            return True
        for next, limit in edges[now]:
            if not visited[next] and limit >= weight:
                visited[next] = True
                queue.append(next)
    return False

left = 1
right = 1_000_000_000
answer = 0

while left <= right:
    mid = (left + right) // 2
    # ๊ฐ€๋Šฅํ• ๋•Œ
    if bfs(mid):
        # ์˜ค๋ฅธ์ชฝ ๊ตฌ๊ฐ„์—์„œ ๋‹ค์‹œ ์ด๋ถ„ํƒ์ƒ‰
        answer = mid
        left = mid + 1
    # ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด
    else:
        #์™ผ์ชฝ๊ตฌ๊ฐ„์—์„œ ๋‹ค์‹œ ์ด๋ถ„ํƒ์ƒ‰
        right = mid - 1

print(answer)

์•„ ์ฐธ๊ณ ๋กœ ํŒŒ์ด์„ ์€ ์ •์ˆ˜ ์ค‘๊ฐ„์— ์–ธ๋”๋ฐ”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ๋„ ์ „ํ˜€ ๋ฌธ์ œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค!
1_000_000_000 == 1000000000

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒ˜์Œ์— ๋ฌด์ง€์„ฑ์œผ๋กœ ๊ทธ๋ƒฅ ๋‹ค์ต์ŠคํŠธ๋ผ ๋Œ๋ ธ๋‹ค๊ฐ€ ํ‹€๋ ธ๋„ค์š”.
์ €๋Š” ๋‚œ๋…์ฆ์ด ์žˆ๋‚˜๋ด…๋‹ˆ๋‹ค.
๋ฌธ์ œ ์ดํ•ด๊ฐ€ ํ•œ ๋ฒˆ์— ์•ˆ๋˜๋„ค์š”..

๊ทธ์น˜๋งŒ ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
๋ง์”€ํ•˜์‹  ๋Œ€๋กœ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ์ตœ์ ํ•ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ธ๋ฐ,
์•„๋งˆ ์ด์ „์— ์ €ํฌ๊ฐ€ ์ข€ ํ–ˆ๋˜ ํŒŒ๋ผ๋ฉ”ํŠธ๋ฆญ ์„œ์น˜๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.

๋ญ”๊ฐ€ ํ•œ ๋ฒˆ์˜ bfs ๋งŒ์— 's์—์„œ e ๊นŒ์ง€ ๊ฐ€์ค‘์น˜ ์ค‘์— ๊ฐ€์žฅ ์ž‘์€ ๋…€์„'์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ..
ํ‹€๋ ธ๋„ค์š”.. ๋ฐœ๊ฒฌํ•œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜๋ฅผ ์ง€๋‚˜์ง€ ์•Š๊ณ ๋„ ๋„์‹œ์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋„ ๊ณ ๋ คํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋ ค์šด ๋ฐฉ์‹๊ฐ™๋„ค์š”. ์Šฌํ”•๋‹ˆ๋‹ค.

์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

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

using namespace std;

bool solution(const vector<vector<pair<int, int>>>& graph, int m, int s, int e) {
    vector<bool> visited(graph.size(), false);
    queue<int> q;
    q.push(s);
    visited[s] = true;

    while (!q.empty()) {
        int cur = q.front(); 
        q.pop();

        if (cur == e) return true;

        for (auto [next, dist] : graph[cur]) {
            if (!visited[next] && dist >= m) {
                visited[next] = true;
                q.push(next);
            }
        }
    }
    
    return false;
}

int main() {
    int N, M;
    cin >> N >> M;

    vector<vector<pair<int, int>>> graph(N + 1);
    while (M--) {
        int A, B, C;
        cin >> A >> B >> C;
        graph[A].push_back({B, C});
        graph[B].push_back({A, C});
    }

    int s, e;
    cin >> s >> e;

    long long start = 1, end = 1000000000;
    while (start <= end) {
        long long mid = (start + end) / 2;
        if (solution(graph, mid, s, e)) start = mid + 1;
        else end = mid - 1;
    }

    cout << end;

    return 0;
}

Copy link
Member

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BFS/Dijkstra๋ผ๋Š” ์ƒ๊ฐ์ด ์ฒ˜์Œ ๋”ฑ ๋“ค์–ด์„œ ์šฐ๋‹นํƒ•ํƒ• ์งฐ๋Š”๋ฐ, ์ด๊ฒŒ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ตœ๋Œ€ ์ค‘๋Ÿ‰์„ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์•ˆ๋– ์˜ค๋ฅด๋”๋ผ๊ตฌ์š”. ๊ทธ๋ž˜์„œ ๊ณจ๋จธ๋ฆฌ ์‹ธ๋งค๋‹ค๊ฐ€, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ตœ๋Œ€ ์ค‘๋Ÿ‰์ด 10์–ต์œผ๋กœ ํฐ ๊ฑฐ ๋ณด๊ณ  "๋ญ”๊ฐ€ ์ด์ง„ํƒ์ƒ‰ ๊น”์ด ๋ณด์ด๋Š”๋ฐ"๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์„œ ์•„์˜ˆ ์ด์ง„ํƒ์ƒ‰์œผ๋กœ ๊ธฐ์ค€ ์ค‘๋Ÿ‰์„ ์žก๊ณ , ๊ทธ ์ค‘๋Ÿ‰๊ฐ’์œผ๋กœ ๋ชฉ์ ์ง€์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ง€์— ๋”ฐ๋ผ ๊ธฐ์ค€ ์ค‘๋Ÿ‰๊ฐ’์„ ์กฐ์ ˆํ•˜๋ฉฐ ์ตœ๋Œ€๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š” ์‹์œผ๋กœ ํ•˜๋‹ˆ๊นŒ ๋งž๋”๋ผ๊ตฌ์š”. ์•ฝ๊ฐ„ ๋ฐœ์ƒ์˜ ์ „ํ™˜(?)

์ด์ง„ํƒ์ƒ‰

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, M;
    cin >> N >> M;
    
    int maxWeight = 0;
    vector<vector<pair<int, int>>> graph(N + 1);
    while(M--) {
        int A, B, C;
        cin >> A >> B >> C;
        
        graph[A].emplace_back(B, C);
        graph[B].emplace_back(A, C);
        
        maxWeight = max(maxWeight, C);
    }
    
    int src, dst;
    cin >> src >> dst;
    
    auto Check = [&](const int stdLimit) {
        queue<int> q;
        q.emplace(src);
        
        vector<bool> visited(N + 1, false);
        visited[src] = true;
        
        while(!q.empty()) {
            const int u = q.front();
            q.pop();
            
            if(u == dst) {
                return true;
            }
            
            for(const auto& [v, limit] : graph[u]) {
                if(!visited[v] && limit >= stdLimit) {
                    visited[v] = true;
                    q.emplace(v);
                }
            }
        }
        
        return false;
    };
    
    int ans = 0;
    int lo = 1, hi = maxWeight;
    while(lo <= hi) {
        int mid = (lo + hi) / 2;
        if(Check(mid)) {
            ans = max(ans, mid);
            lo = mid + 1;
        } else {
            hi = mid - 1;
        }
    }
    
    cout << ans;
    
    return 0;
}

๊ทธ๋Ÿฐ๋ฐ ๋ญ”๊ฐ€ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ตœ๋Œ€ ์ค‘๋Ÿ‰์„ ์—…๋ฐ์ดํŠธ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ์ž๊พธ ๋“ค์–ด์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ดค๋Š”๋ฐ, ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๊ฒŒ ๊ฐ€๋Šฅํ•ด์„œ ์ด๊ฒƒ๋„ ๋“ค๊ณ ์™”์Šต๋‹ˆ๋‹ค ใ…Ž

์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์ถ•ํ•  ๋•Œ, ๋ณดํ†ต์€ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๊ฑฐ๊พธ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ํฐ ๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์ด ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ฐพ๋Š” ๊ฑฐ๋‹ˆ๊นŒ์š”.

๊ทธ๋ฆฌ๊ณ  ๊ฐ„์„ ๋“ค์— ๋Œ€ํ•ด Union์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์ค‘์น˜๊ฐ€ ํฐ ๊ฐ„์„ ๋“ค ๋จผ์ € Union๋˜๊ฒ ์ฃ ? ๊ทธ๋Ÿฌ๋ฉด ์–ด๋А ์ˆœ๊ฐ„ ์ถœ๋ฐœ ๊ณต์žฅ ๋…ธ๋“œ์™€ ๋„์ฐฉ ๊ณต์žฅ ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋Š”(๋‘ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๊ฐ€ ๊ฐ™์€) ์ˆœ๊ฐ„์ด ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ๋•Œ ํ™•์ธํ•˜๋Š” ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‘ ๊ณต์žฅ์ด ์—ฐ๊ฒฐ๋˜๋Š” ์ˆœ๊ฐ„์˜ ๊ฒฝ๋กœ๊ฐ€ ๊ฐ€์žฅ ํฐ ํ•˜์ค‘ ์ œํ•œ์„ ๊ฐ–๋Š” ๊ฒฝ๋กœ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํฐ ์ˆœ์„œ๋Œ€๋กœ ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, M;
    cin >> N >> M;
    
    using Edge = tuple<int, int, int>;
    vector<Edge> edges(M);
    for(auto& [u, v, weight] : edges) {
        cin >> u >> v >> weight;
    }
    
    int src, dst;
    cin >> src >> dst;
    
    vector<int> p(N + 1);
    iota(p.begin(), p.end(), 0);
    
    sort(edges.begin(), edges.end(), [](const Edge& lhs, const Edge& rhs) {
        return get<2>(lhs) > get<2>(rhs);
    });
    
    auto Find = [&](int x) {
        while(x != p[x]) {
            p[x] = p[p[x]];
            x = p[x];
        }
        
        return p[x];
    };
    
    auto Union = [&](int u, int v) {
        u = Find(u);
        v = Find(v);
        
        p[u] = v;
    };
    
    for(const auto& [u, v, weight] : edges) {
        Union(u, v);
        if(Find(src) == Find(dst)) {
            cout << weight;
            break;
        }
    }
    
    return 0;
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ์ฒจ์—” ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ๊ฐ€์žฅ ํฐ ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ฉด์„œ ๊ฐ€๋ฉด ๋˜๊ฒ ๋‹ค! ํ–ˆ๋Š”๋ฐ ๋ฐ˜๋ก€๋ฅผ ๋ฐ”๋กœ ์ฐพ์•„์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ ๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ์ „๋ถ€๋‹ค ํ•ด์•ผํ•˜๋‚˜? ์‹ถ์—ˆ๋Š”๋ฐ n์ด 10000์ธ๋ฐ ๋ง์ด ์•ˆ๋˜์ฃ ..

10๋ถ„์„ ๋” ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์šฐ์—ฐ์ฐฎ๊ฒŒ ์ด๋ถ„ ํƒ์ƒ‰์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋– ์˜ฌ๋ž์”๋‹ˆ๋‹ค. 1 ~ 10000000๊นŒ์ง€ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ทธ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ์ฐพ์œผ๋ฉด ๋œ๋‹ค ์ƒ๊ฐํ–ˆ์–ด์š”. ๊ทผ๋ฐ up & down ์กฐ๊ฑด์„ ์ž˜ ๋งž์ถ”์ง€ ๋ชปํ•ด ๋‹ต์ง€๋ฅผ ๋ดค์Šต๋‹ˆ๋‹ค.

๊ฑฐ์˜ ๋‹ค ์™”๋Š”๋ฐ ์•„์‰ฝ๋„ค์š”ใ…œ ์ž…๋ ฅ ๊ด€๋ จํ•ด์„œ ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์„ ๋ชฐ๋ผ ์ค€์šฉ๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์น˜๋‹ค๊ฐ€ ๊ทธ๋ƒฅ ๊ฐ™์•„์กŒ์Šด๋‹ˆ๋‹ค..

import sys
from collections import deque
input = sys.stdin.readline

def bfs(weight):
    visited = [False for _ in range(n+1)]
    q = deque()
    q.append(s)
    visited[s] = True
    while q:
        now = q.popleft()
        if now == e:
            return True
        
        for next, edge in graph[now]:
            if not visited[next] and edge >= weight:
                visited[next] = True
                q.append(next)
    return False

n, m = map(int, input().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
    a, b, w = map(int, input().split())
    graph[a].append((b, w))
    graph[b].append((a, w))
s, e = map(int, input().split())

left = 1
right = 1_000_000_000
answer = 0
while left <= right:
    mid = (left + right) // 2
    if bfs(mid):
        answer = mid
        left = mid + 1
    else:
        right = mid - 1
print(answer)

@kokeunho kokeunho merged commit 79f96a5 into main Aug 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants