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

26-SeongHoonC #200

Merged
merged 2 commits into from
May 30, 2024
Merged

26-SeongHoonC #200

merged 2 commits into from
May 30, 2024

Conversation

SeongHoonC
Copy link
Collaborator

@SeongHoonC SeongHoonC commented May 26, 2024

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

์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™

์ผ€๋นˆ ๋ฒ ์ด์ปจ

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

40๋ถ„

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

๋ชจ๋“  ๊ณณ์—์„œ ๋ชจ๋“  ๊ณณ์œผ๋กœ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผํ•œ๋‹ค๋Š”๊ฒŒ ๋ณด์—ฌ์„œ

๋ฌธ์ œ ์ž์ฒด๋Š” ๊ทธ๋ƒฅ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค...

๋‹ค๋งŒ ์ˆœ์„œ๋ฅผ ์ž˜๋ชป ์ƒ๊ฐํ•ด์„œ ์กฐ๊ธˆ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋„ค์š”.

  1. ๋‚˜ -> ๋‚˜ ๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ 0 ์ด๋ฏ€๋กœ i == j ๋Š” 0 ์œผ๋กœ ์ดˆ๊ธฐํ™”
  2. k ๋ฅผ ๊ฑฐ์ณ์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
for k in 1..n:
 for i in 1..n:
  for j in 1..n:
    if graph[i][j] > graph[i][k] + graph[k][j]:
     graph[i][j] = graph[i][k] + graph[k][j]

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

ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ์€ k, i, j ์ˆœ์„œ๋‹ค ๋ช…์‹ฌ..!

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

์ด ๋ฌธ์ œ ๋ณด์ž๋งˆ์ž

#74 ๊ฐ€ ์ƒ๊ฐ๋‚˜์„œ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์ธ๊ฐ€? ํ–ˆ๋Š”๋ฐ

N์˜ ์ตœ๋Œ€ ๊ฐ’์ด 100์ธ๊ฑฐ ๋ณด์ž๋งˆ์ž ๋ฐ”๋กœ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์ด๊ตฌ๋‚˜! ํ•˜๊ณ  ์Šฅ์Šฅ ํ’€์—ˆ์”๋‹ˆ๋‹ค!

from collections import defaultdict
import sys

def input(): return sys.stdin.readline().rstrip()
INF = int(1e9)

N, M = map(int, input().split())

floyd = [[INF for _ in range(N+1)] for _ in range(N+1)]
for init in range(N+1):
    if init == 0: continue
    floyd[init][init] = 0
    
for _ in range(M):
    first, second = map(int, input().split())
    floyd[first][second] = 1
    floyd[second][first] = 1

for mid in range(1,N+1):
    for start in range(1,N+1):
        for end in range(1,N+1):
            floyd[start][end] = min(floyd[start][end], floyd[start][mid] + floyd[mid][end])

min_value = INF
min_idx = -1
for idx, info in enumerate(floyd):
    if idx == 0: continue

    temp = sum(info[1:])
    if temp < min_value:
        min_value = temp
        min_idx = idx
        
print(min_idx)

์—ฌ๋Ÿฌ๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋‚˜์˜ค๋‹ˆ๊นŒ ์žฌ๋ฐŒ๋„ค์š” ํํํ

image

Comment on lines +18 to +26
for (k in 1..n) {
for (i in 1..n) {
for (j in 1..n) {
if (graph[i][j] > graph[i][k] + graph[k][j]) {
graph[i][j] = graph[i][k] + graph[k][j]
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

k, i, j ๋ณด๋‹ค ์ €๋Š” mid, start, end ๋กœ ์™ธ์šฐ๋‹ˆ๊นŒ ํŽธํ•˜๋“œ๋ผ๊ณ ์šฉ ํ‚คํ‚ผ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mid, start, end ๋กœ ์™ธ์›Œ์•ผ๊ฒ ๋„ค์š”!

Copy link
Collaborator

Choose a reason for hiding this comment

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

์˜คํ˜ธ? min start end.. ์ค์ค

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๊ฐ€ ๊น”๊ผผํ•˜๋„ค์šฉ
๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ตฌํ•ด์•ผํ•˜๋‹ˆ๊นŒ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์ด๋‹ค! ํ•˜๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค

ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ + ๋‹ค์ต์ŠคํŠธ๋ผ ์ž˜ ๋ณต์Šตํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค !!
๋ฌธ์ œํ‘ธ๋Š๋ผ ์ˆ˜๊ณ ํ•˜์…จ์Šด๋‹ค ~~ ๐Ÿ‘

import sys

def input(): return sys.stdin.readline().rstrip()

N, M = map(int, input().split())

relations = [list(map(int, input().split())) for _ in range(M)]

graph = [[int(1e9)] * (N+1) for _ in range(N+1)]
sum = [0] * (N+1)
result = 0 # ์ตœ์ข… ๋‹ต
tmp = 0

# ์ž๊ธฐ ์ž์‹ ์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
for i in range(1, N+1):
    for j in range(1, N+1):
        if i == j:
            graph[i][j] = 0

# ์นœ๊ตฌ ์ •๋ณด
for f1, f2 in relations:
    graph[f1][f2] = 1
    graph[f2][f1] = 1

# ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ DP
for i in range(1,N+1):
    for j in range(1, N+1):
        for k in range(1, N+1):
            graph[j][k] = min(graph[j][k], graph[j][i] + graph[i][k])

for i in range(1, N + 1):
    for j in range(1, N + 1):
        sum[i] += graph[i][j]

tmp = int(1e9)

for i in range(1, N+1):
    if sum[i] < tmp:
        tmp = sum[i]
        result = i

print(result)

@alstjr7437
Copy link
Member

๋‹ค๋“ค ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์œผ๋กœ๋งŒ ํ’€์—ˆ๋„ค์š”!!
์•„๊นŒ ๊ฐ•์˜์‹œ๊ฐ„์— ๋งํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ ์ €๋Š” BFS๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!
min์„ ๋ฝ‘์•„๋‚ด๊ณ  ์ธ๋ฑ์Šค๋กœ ํ•˜์—ฌ์„œ ์ œ์ผ ์ ์€ ๋ฒˆํ˜ธ์ธ ์‚ฌ๋žŒ์„ ์ถœ๋ ฅํ•˜๋„๋ก!!

์•„์ง ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์ž˜ ๋ชฐ๋ผ์„œ ๊ทธ๋Ÿฐ๊ฑฐ ์•„๋‹˜

import sys
from collections import deque

input = sys.stdin.readline

n, m = map(int, input().split())
graph = [[] for _ in range(n+1)]

for i in range(m):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

def bfs(start):
    num = [0] * (n+1)
    visited = [start]
    queue = deque()
    queue.append(start)

    while queue:
        now = queue.popleft()
        for i in graph[now]:
            if i not in visited:
                num[i] = num[now] + 1
                visited.append(i)
                queue.append(i)
    return sum(num)

result = []
for i in range(1, n+1):
    result.append(bfs(i))

print(result.index(min(result))+1)

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.

4 participants