Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13-wnsmir #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

13-wnsmir #54

wants to merge 1 commit into from

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Jan 7, 2025

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

์ด๊ฒƒ์ด ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค ์‹ค์ „๋ฌธ์ œ์ค‘ chapter ์ตœ๋‹จ๊ฒฝ๋กœ์˜ 1๋ฒˆ๋ฌธ์ œ ๋ฐœ์ทŒ์ž…๋‹ˆ๋‹ค.
๊ท ํ˜ธ๋‹˜ 12๋ฒˆ์งธ PR๊ณผ ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ๋ณต์Šต๊ฒธ ํ’€์–ด๋ณด๊ณ ์ž ๊ฐ€์ ธ์™€๋ดค์Šต๋‹ˆ๋‹ค.
๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฅธ์ ‘๊ทผ์œผ๋กœ ํ’€์–ด ์ข€ ์˜ค๋ž˜๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค,,

๋ฏธ๋ž˜๋„์‹œ - ๋‚œ์ด๋„2/3 - ํ’€์ด์‹œ๊ฐ„ 40min - ๋ฉ”๋ชจ๋ฆฌ์ œํ•œ 128MB
๋ฌธ์ œ
๋ฐฉ๋ฌธํŒ๋งค์› A๋Š” ํ˜„์žฌ 1๋ฒˆํšŒ์‚ฌ์— ์œ„์น˜ํ•ด์žˆ์œผ๋ฉฐ X๋ฒˆ ํšŒ์‚ฌ์— ๋ฐฉ๋ฌธํ•ด ๋ฌผ๊ฑด์„ ํŒ๋งคํ•˜๊ณ ์ž ํ•œ๋‹ค. ๋ฏธ๋ž˜๋„์‹œ์—์„œ ํšŒ์‚ฌ๋Š” ํšŒ์‚ฌ๋ผ๋ฆฌ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ํ†ต๋กœ๋กœ๋งŒ ์ด๋™๊ฐ€๋Šฅํ•˜๊ณ , ์ด ํ†ต๋กœ๋Š” ํ•ญ์ƒ ์–ด๋””๋“  1์˜์‹œ๊ฐ„์œผ๋กœ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.
A๋Š” 1๋ฒˆ์—์„œ X๋ฒˆ๊นŒ์ง€ ๊ฐ€์•ผํ•˜๋Š”๋ฐ ๋„์ค‘ ์†Œ๊ฐœํŒ…์„ ํ•ด์•ผํ•œ๋‹ค. ์†Œ๊ฐœํŒ… ์ƒ๋Œ€๋Š” K๋ฒˆ ํšŒ์‚ฌ๋ฅผ ๋ฐฉ๋ฌธํ•œ ๋’ค XํšŒ์‚ฌ๋กœ ๊ฐ€๋Š”๊ฒƒ์ด ๋ชฉํ‘œ์ด๋‹ค.

ํ˜„์œ„์น˜์—์„œ K๋ฅผ๊ฑฐ์ณ X๋กœ ์ด๋™ํ•˜๋Š” A์˜ ์ตœ๋‹จ ์ด๋™์‹œ๊ฐ„์„ ๊ตฌํ•˜์—ฌ๋ผ.

์ž…๋ ฅ์€ ์ฒซ์ค„์— N๊ณผM์ด ๊ณต๋ฐฑ์„ ๋‘๊ณ  ์ฃผ์–ด์ง€๊ณ , ๋‹ค์Œ M๊ฐœ์˜ ์ค„์— ๊ฐ ํšŒ์‚ฌ๋“ค์˜ ํ†ต๋กœ ๊ด€๊ณ„๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋งˆ์ง€๋ง‰์ค„์€ X์™€ K๊ฐ€ ๊ณต๋ฐฑ์„ ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.
๋งŒ์•ฝ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
(1<=N, M<=100), (1<=K<=100)
์ž…๋ ฅ์˜ˆ์‹œ
5 7
1 2
1 3
1 4
2 4
3 4
3 5
4 5
4 5
์ถœ๋ ฅ์˜ˆ์‹œ
3

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

50min

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

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

ํ”Œ๋ฃจ์ด๋“œ ์œ„์…œ์€ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๋™์ ๋ฐฐ์—ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ผ์ข…์ธ๋ฐ,
์ธ์ ‘ํ–‰๋ ฌ์„ ํ™œ์šฉํ•˜์—ฌ ์ถœ๋ฐœ๋…ธ๋“œ์™€ ๋„์ฐฉ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ N๋ฒˆ์”ฉ ๋Œ์•„๊ฐ€๋ฉฐ ์ตœ์†Œํ•œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š”๋ฐ, ์ด๋•Œ ์ œ์ผ ๋ฐ”๊นฅ ๋ฐ˜๋ณต๋ฌธ K๋ฅผ ํ™œ์šฉํ•ด ์ถœ๋ฐœ๊ณผ ๋„์ฐฉ๋…ธ๋“œ ์‚ฌ์ด์— ๋‹ค๋ฅธ๋…ธ๋“œ ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ•ด ์—…๋ฐ์ดํŠธํ•ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
์ฆ‰ 1๊ณผ 3์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 3์ธ๋ฐ 1->2->3์ด 2๋ผ๋ฉด 1๊ณผ2, 2์™€3์˜ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์œผ๋กœ 1->3์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋ฎ์–ด์”Œ์šฐ๋Š” ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๋ฐ˜๋ณต๋ฌธ 3๋ฒˆ N^3์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

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.

์ €๋Š” ์ด์ „ ๋ฆฌ๋ทฐ์—์„œ๋„ ์ผ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ทธ๋Œ€๋กœ ๋‹ค์‹œ ์จ๋ดค์Šต๋‹ˆ๋‹ค.
(๋‹ค์Œ์— ์ด๋Ÿฐ๊ฒŒ ๋‚˜์˜ค๋ฉด ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ๋กœ....)

ํŠน๋ณ„ํ•œ ๊ธฐ์ˆ ์€ ์—†์ง€๋งŒ ์ด ๋ฌธ์ œ์— ์ ํ•ฉํ•œ ํ˜•ํƒœ๋กœ ์กฐ๊ธˆ ๋ณ€ํ˜•ํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ์—์„œ ๋…ธ๋“œ๋ฅผ ๊บผ๋‚ผ๋•Œ

if (cur == e) return distance;

์ด๋ ‡๊ฒŒ ๋…ธ๋“œ๋งˆ๋‹ค ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋ฉด์„œ ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•˜๋ฉด ๋ฐ”๋กœ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ทธ๋Ÿฐ ๋กœ์ง์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ € if๋ฌธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š๊ณ  ํ•จ์ˆ˜๊ฐ€ return๋˜๋ฉด ๋ชฉ์ ์ง€๋กœ๊ฐ€๋Š” ๊ธธ์ด ์—†๋‹ค๋Š” ๋œป์ด๊ธฐ์— -1์„ return ํ–ˆ์Šต๋‹ˆ๋‹ค.

CPP CODE
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int N, M;
vector<vector<int>> graph;

int bfs(int s, int e) {
    vector<bool> visited(N + 1, false);

    queue<int> q;
    q.push(s);
    visited[s] = true;

    int distance = 0;
    while (!q.empty()) {
        int size = q.size();
        while (size--) {
            int cur = q.front();
            q.pop();

            if (cur == e) return distance;
            for (int v : graph[cur]) {
                if (!visited[v]) {
                    q.push(v);
                    visited[v] = true;
                }
            }
        }
        distance++;
    }

    return -1;
}

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

    graph = vector<vector<int>>(N + 1);
    for (int i = 1; i <= M; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    int X, K;
    cin >> X >> K;

    int temp1 = bfs(1, K);
    int temp2 = bfs(K, X);
    if (temp1 == -1 || temp2 == -1) cout << "-1\n";
    else cout << temp1 + temp2;

    return 0;
}

Comment on lines +20 to +23
for k in range(1, N+1):
for a in range(1, N+1):
for b in range(1, N+1):
graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b])
Copy link
Collaborator

Choose a reason for hiding this comment

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

์˜ค ์ด๊ฒŒ ํ”Œ๋กœ์ด๋“œ-์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ตฐ์š” ๋‹ค์Œ์—๋Š” ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์จ๋ด์•ผ๊ฒ ๋„ค์š”...

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.

2 participants