Skip to content

Commit

Permalink
Complete q37_floyd(플로이드)
Browse files Browse the repository at this point in the history
  • Loading branch information
bong6981 committed Nov 5, 2021
1 parent fa1dab5 commit c5b3477
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
54 changes: 54 additions & 0 deletions bongf/java/src/ch17_shortest_path/Q37_Floyd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ch17_shortest_path;

import java.util.Arrays;
import java.util.Scanner;

// https://www.acmicpc.net/problem/11404
public class Q37_Floyd {
public static int INF = (int) 1e9;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
Arrays.fill(graph[i], INF);
graph[i][i] = 0;
}

for (int i = 0; i < m; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
int cost = sc.nextInt();
/*
dongbin :
if(cost < graph[from][to]) graph[from][to] = cost;
*/
if (graph[from][to] != INF) {
graph[from][to] = Math.min(cost, graph[from][to]);
continue;
}
graph[from][to] = cost;
}

for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
}
}
}

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (graph[i][j] == INF) {
System.out.print(0 + " ");
continue;
}
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
}
31 changes: 31 additions & 0 deletions bongf/python/ch17_shortest_path/q37_floyd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## 백준 pypy로 제출 - 통과
## https://www.acmicpc.net/problem/11404
def solution():
## 처음에는 최대값을 100001로 해서 틀렸다.
INF = int(1e9)
n = int(input())
m = int(input())
board = [[INF] * (n+1) for _ in range(n+1)]
for i in range(1, n+1):
board[i][i] = 0

for _ in range(m):
s, e, c = map(int, input().split(" "))
board[s][e] = min(board[s][e], c)


for i in range(1, n+1):
for j in range(1, n+1):
for k in range(1, n+1):
board[j][k] = min(board[j][k], board[j][i] + board[i][k])

for i in range(1, n+1):
for j in range(1, n+1):
if(board[i][j] < 100001):
print(board[i][j], end= ' ')
else:
print(0, end= ' ')
print()

solution()

0 comments on commit c5b3477

Please sign in to comment.