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

34-alstjr7437 #241

Merged
merged 2 commits into from
Sep 27, 2024
Merged

34-alstjr7437 #241

merged 2 commits into from
Sep 27, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Aug 28, 2024

πŸ”— 문제 링크

νŒŒν‹°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„

μ˜€λž˜λ§Œμ— λ‹€μ΅μŠ€νŠΈλΌ μ—°μŠ΅κ²Έ κΈ°μ–΅ 볡귀λ₯Ό μœ„ν•΄μ„œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€!!

그런데 λ‹€ν’€κ³  μ‚΄νŽ΄λ³΄λ‹ˆκΉŒ νƒœκ·œλ‹˜μ˜ λ¬Έμ œμ— μžˆλ”κ΅°μš” γ„·γ„·γ„·...

문제점

λ”°λ‘œ μ²˜μŒμ—λŠ” λ¬Έμ œκ°€ 이해가 가지 μ•Šμ•˜μŠ΅λ‹ˆλ‹€

λ§ˆμ§€λ§‰ κΈ€λ§Œ 읽고 λŒμ•„μ„œ 가더라도 μ΅œλŒ€λ‘œ κ°€λŠ” 방법을 κ΅¬ν•˜μž!!

인쀄 μ•Œμ•˜λŠ”λ° 결과값을 λ³΄λ‹ˆ 10이 λ‚˜μ˜€λ”λΌκ΅¬μš”!!

μžμ„Ένžˆ μ½μ–΄λ³΄λ‹ˆμ΅œλ‹¨ 거리둜 κ°€μ§€λ§Œ κ·Έ 쀑에 제일 λ¨Ό μ• λ₯Ό 좜λ ₯μ΄λ”λΌκ΅¬μš”

μ €λŠ” κ·Έλƒ₯ μ–΄λ–»κ²Œ λŒμ•„μ„œ 가도 2λ²ˆμ— κ°€λŠ” μ΅œλŒ€ 거리λ₯Ό κ΅¬ν•˜λŠ” 쀄 μ•Œμ•˜λ„€μš” γ… 

✨ μˆ˜λ„ μ½”λ“œ

  1. κ·Έλž˜ν”„ λ§Œλ“€κΈ°
  2. 반볡문 돌기
    • νŒŒν‹° μž₯μ†ŒκΉŒμ§€ κ°€λŠ” 거리
    • νŒŒν‹° μž₯μ†Œμ—μ„œ λŒμ•„μ˜€λŠ” 거리
    • λ‘κ°œ λ”ν•΄μ„œ 거리 μ•ŒκΈ°
  3. μœ„ λ‚΄μš©μ„ λ‹€μ΅μŠ€νŠΈλΌλ‘œ κ΅¬ν•˜κΈ°
    • 거리λ₯Ό 담아두기
    • κ°€λŠ” 곳의 거리가 ν˜„μž¬ κ°€λ €λŠ” 거리보닀 κ°€κΉŒμš΄μ§€ 확인(예λ₯Ό λ“€μ–΄ 1 -> 4, 1 -> 3 -> 4 μ΄λ ‡κ²Œ 되면 1,3,4κ°€ κ°€κΉŒμš΄κ²Œ 확인됨)
    • κ°€κΉŒμš°λ©΄ ν•΄λ‹Ή λΆ€λΆ„μœΌλ‘œ 거리λ₯Ό μ΅œμ‹ ν™”ν•˜κ³  큐에 λ„£κΈ°
    • 가깝지 μ•ŠμœΌλ©΄ 큐에 넣지 μ•Šκ³  끝

으둜 κ΅¬ν˜„μ„ ν–ˆμŠ΅λ‹ˆλ‹€!

μ½”λ“œ

from collections import deque 
import sys

input = sys.stdin.readline
INF = int(1e9)

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

graph = [[] for _ in range(N+1)]

for _ in range(M):
    start, target, dist = map(int, input().split())
    graph[start].append((target, dist))

def dijkstra(root):
    queue = deque()
    distance = [INF] * (N + 1)

    distance[root] = 0
    queue.append((root, 0))
    while queue:
        now, dist = queue.popleft()
        
        for target, cost in graph[now]:
            target_cost = dist + cost

            if distance[target] > target_cost:
                distance[target] = target_cost
                queue.append((target, target_cost))
    return distance

result = []
for i in range(1, N+1):
    go = dijkstra(i)[X]
    back = dijkstra(X)[i]
    result.append(go+back)

print(max(result))

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

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.

from heapq import *
from collections import defaultdict

total_node_count, total_edge_count, target_node = map(int,input().split(" "))

visited = [False for _ in range(total_node_count+1)]
go_distance_table = [int(1e9) for _ in range(total_node_count+1)]
link_table = defaultdict(lambda: defaultdict(lambda : int(1e9)))


for _ in range(total_edge_count):
    node1, node2, weight = map(int, input().split(" "))
    link_table[node1][node2] = weight

#μ§‘μ—μ„œ X둜 κ°€λŠ” κΈΈ
for idx in range(1,total_node_count+1):
    if idx == target_node:
        go_distance_table[target_node] = 0
        continue

    heap = [[0,idx]]
    temp_visited = [False for _ in range(total_node_count+1)]
    temp_distance_table = [int(1e9) for _ in range(total_node_count+1)]
    temp_distance_table[idx] = 0
    
    while heap:
        now_distance, now_node = heappop(heap)
        temp_visited[now_node] = True

        for link_node in link_table[now_node]:
            if temp_visited[link_node] == True:
                continue

            if temp_distance_table[link_node] > now_distance + link_table[now_node][link_node] :
                temp_distance_table[link_node] = now_distance + link_table[now_node][link_node]

                heappush(heap,[temp_distance_table[link_node],link_node])
    
    go_distance_table[idx] = temp_distance_table[target_node]

#Xμ—μ„œ μ§‘μœΌλ‘œ λŒμ•„κ°€λŠ” κΈΈ
come_distance_table = [int(1e9) for _ in range(total_node_count+1)]
heap = [[0,target_node]]

while heap:
    now_distance, now_node = heappop(heap)
    visited[now_node] = True

    for link_node in link_table[now_node]:
        if visited[link_node] == True:
            continue

        if come_distance_table[link_node] > now_distance + link_table[now_node][link_node] :
            come_distance_table[link_node] = now_distance +link_table[now_node][link_node]

            heappush(heap,[come_distance_table[link_node],link_node])

go_distance_table[target_node] = -1
come_distance_table[target_node] = -1
print(max([x+y for x,y in zip(go_distance_table[1:], come_distance_table[1:])]))

ν—ˆν—ˆ. μ΄λ ‡κ²Œ λ§›μžˆλŠ” 문제λ₯Ό λ‚΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€ ,,,, γ…Žγ…Ž

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.

λ‹€μ΅μŠ€νŠΈλΌ n->x, x->n 을 두 번 λŒλ¦¬λ‹ˆκΉŒ λ°”λ‘œ λ‹΅ λ‚˜μ˜€λ„€μš©

λ‹€μ΅μŠ€νŠΈλΌ 볡슡 잘 ν–ˆμŠ΅λ‹ˆλ‹€μ˜Ή ~! ~! ~🫑

import sys
import heapq

INF = 1e8

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

N, M, X = map(int, input().split())
T = [[] for _ in range(N+1)]
distance = [INF] * (N+1)
cmp = [0] * (N+1)

for _ in range(M):
    u, v, w = map(int, input().split())
    T[u].append((v, w))

def dijkstra(start):
  q = []
  heapq.heappush(q, (0, start))
  distance[start] = 0

  while q:
    dist, now = heapq.heappop(q)

    if distance[now] < dist:
      continue

    for i in T[now]:
      if dist+i[1] < distance[i[0]]:
        distance[i[0]] = dist+i[1]
        heapq.heappush(q, (distance[i[0]], i[0]))

for i in range(N):
  dijkstra(i+1)
  cmp[i+1] = distance[X]
  distance = [INF] * (N+1)

dijkstra(X)
for i in range(N):
  cmp[i+1] += distance[i+1]

print(max(cmp))

@alstjr7437 alstjr7437 merged commit 8e74983 into main Sep 27, 2024
7 checks passed
@alstjr7437 alstjr7437 deleted the 34-alstjr7437 branch September 27, 2024 09:22
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.

3 participants