|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class boj11404 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + static StringTokenizer st; |
| 9 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 10 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 11 | + static void end() throws Exception { |
| 12 | + bw.flush(); |
| 13 | + bw.close(); |
| 14 | + br.close(); |
| 15 | + } |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + nextLine(); |
| 18 | + int N = nextInt(); //도시 개수 |
| 19 | + nextLine(); |
| 20 | + int M = nextInt(); //버스 개수 |
| 21 | + int[][] answer = new int[N+1][N+1]; |
| 22 | + for (int i = 1; i < N+1; i++) { |
| 23 | + Arrays.fill(answer[i], Integer.MAX_VALUE); |
| 24 | + } |
| 25 | + |
| 26 | + for (int i = 0; i < M; i++) { |
| 27 | + nextLine(); |
| 28 | + int a = nextInt(); |
| 29 | + int b = nextInt(); |
| 30 | + int c = nextInt(); |
| 31 | + answer[a][b] = Math.min(answer[a][b], c); |
| 32 | + } |
| 33 | + |
| 34 | + for (int k = 1; k < N+1; k++) { |
| 35 | + for (int i = 1; i < N+1; i++) { |
| 36 | + for (int j = 1; j < N+1; j++) { |
| 37 | + if (i == j) { |
| 38 | + answer[i][j] = 0; |
| 39 | + continue; |
| 40 | + } |
| 41 | + if (answer[i][k] == Integer.MAX_VALUE || answer[k][j] == Integer.MAX_VALUE) continue; |
| 42 | + answer[i][j] = Math.min(answer[i][j], answer[i][k]+answer[k][j]); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + for (int i = 1; i < N+1; i++) { |
| 47 | + for (int j = 1; j < N+1; j++) { |
| 48 | + if (answer[i][j] == Integer.MAX_VALUE) { |
| 49 | + bw.write("0 "); |
| 50 | + continue; |
| 51 | + } |
| 52 | + bw.write(answer[i][j]+" "); |
| 53 | + } |
| 54 | + bw.write("\n"); |
| 55 | + bw.flush(); |
| 56 | + } |
| 57 | + end(); |
| 58 | + } |
| 59 | + |
| 60 | + static class Node { |
| 61 | + int a; |
| 62 | + int b; |
| 63 | + int c; |
| 64 | + public Node(int a, int b, int c) { |
| 65 | + this.a = a; |
| 66 | + this.b = b; |
| 67 | + this.c = c; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +``` |
0 commit comments