티스토리 뷰
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package BACKJOON; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
public class back_11404 { | |
public static int n; | |
public static int[][] fares; | |
public static final int INF = 1000000000; | |
public static void main(String[] args) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
n = Integer.parseInt(br.readLine()); | |
int x = Integer.parseInt(br.readLine()); | |
fares = new int[n+1][x+1]; | |
for(int i=1; i <= n; i++) { | |
for(int j=1; j <= n; j++) { | |
if(i == j) continue; | |
fares[i][j] = INF; | |
} | |
} | |
while(x-- > 0){ | |
StringTokenizer st = new StringTokenizer(br.readLine()); | |
int start = Integer.parseInt(st.nextToken()); | |
int end = Integer.parseInt(st.nextToken()); | |
int fare = Integer.parseInt(st.nextToken()); | |
fares[start][end] = Math.min(fares[start][end], fare); | |
} | |
floyd(); | |
print(); | |
} | |
public static void floyd(){ | |
for(int k = 1; k <= n; k++) { | |
for(int i=1; i <= n; i++) { | |
for(int j=1; j <= n; j++) { | |
fares[i][j] = Math.min(fares[i][k] + fares[k][j], fares[i][j]); | |
} | |
} | |
} | |
} | |
public static void print() { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= n; j++) { | |
if (fares[i][j] >= INF) sb.append("0 "); | |
else sb.append(fares[i][j] + " "); | |
} | |
sb.append("\n"); | |
} | |
System.out.println(sb.toString()); | |
} | |
} |
반응형
'알고리즘 > 백준' 카테고리의 다른 글
1260번 백준 (0) | 2021.04.20 |
---|---|
백준 9251 (0) | 2021.04.20 |
[1197]최소 스패닝 트리(MST) / 스패닝 트리(STP) (0) | 2021.02.14 |
[백준] 1330번 두 수 비교하기 (0) | 2021.02.09 |
[level 1] 두 개 뽑아서 더하기 (0) | 2021.02.09 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- springboot
- JPA
- Spring
- nginx
- 자바
- interview
- 수학
- 자격증
- 매트랩
- CS
- kakao
- 디자인패턴
- spring-cloud
- security
- C언어
- 그래프
- ajax
- 스프링부트
- Algorithm
- 스프링
- Matlab
- OOP
- 알고리즘
- Solid
- docker
- 백준
- 프로그래머스
- 릿코드
- java
- 면접
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함