-
Notifications
You must be signed in to change notification settings - Fork 2
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
26-SeongHoonC #200
Conversation
There was a problem hiding this 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)
์ฌ๋ฌ๊ฐ์ง ์๊ณ ๋ฆฌ์ฆ์ด ๋์ค๋๊น ์ฌ๋ฐ๋ค์ ํํํ
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] | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
๋ก ์ธ์ฐ๋๊น ํธํ๋๋ผ๊ณ ์ฉ ํคํผ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mid, start, end ๋ก ์ธ์์ผ๊ฒ ๋ค์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์คํธ? min start end.. ์ค์ค
There was a problem hiding this 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)
๋ค๋ค ํ๋ก์ด๋ ์์
์ผ๋ก๋ง ํ์๋ค์!!
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) |
๐ ๋ฌธ์ ๋งํฌ
์ผ๋น ๋ฒ ์ด์ปจ์ 6๋จ๊ณ ๋ฒ์น
โ๏ธ ์์๋ ์๊ฐ
40๋ถ
โจ ์๋ ์ฝ๋
๋ชจ๋ ๊ณณ์์ ๋ชจ๋ ๊ณณ์ผ๋ก ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํด์ผํ๋ค๋๊ฒ ๋ณด์ฌ์
๋ฌธ์ ์์ฒด๋ ๊ทธ๋ฅ ํ๋ก์ด๋ ์์ ๋ก ํ์์ต๋๋ค...
๋ค๋ง ์์๋ฅผ ์๋ชป ์๊ฐํด์ ์กฐ๊ธ ์ค๋ ๊ฑธ๋ ธ๋ค์.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํ๋ก์ด๋ ์์ฌ์ k, i, j ์์๋ค ๋ช ์ฌ..!