Home BOJ. Floyd(2) (11780)
Post
Cancel

BOJ. Floyd(2) (11780)

[Link] https://www.acmicpc.net/problem/11780

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*Can do more optimization with
	-- instead of storing middle idx(pass), store next idx
  -- instead of checking with visit[], init val of cost[][] with INF and
     just update when cost[from][to] > cost[from][mid] + cost[mid][to]
*/
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static int max = 0, maxIdx = 0;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] line;
		int n = toi(br.readLine()), m = toi(br.readLine());
		long[][] cost = new long[n][n];
		boolean[][] visit = new boolean[n][n];
		int[][] pass = new int[n][n];
		for(long[] ar: cost) Arrays.fill(ar, Long.MAX_VALUE);
		for(int[] ar: pass) Arrays.fill(ar, -1);
		for(int i = 0; i < n; i++) {
			visit[i][i] = true;
			cost[i][i] = 0;
		}
		for(int i = 0; i < m; i++) {
			line = getLine();
			int a = toi(line[0]) - 1, b = toi(line[1]) - 1, c = toi(line[2]);
			cost[a][b] = Math.min(cost[a][b], c);
			visit[a][b] = true;
		}

		for(int mid = 0; mid < n; mid++) {
			for(int from = 0; from < n; from++) {
				for(int to = 0; to < n; to++) {
					if(visit[from][mid] && visit[mid][to]) {
						if(visit[from][to]) {
							if(cost[from][to] > cost[from][mid] + cost[mid][to]) {
								cost[from][to] = cost[from][mid] + cost[mid][to];
								pass[from][to] = mid;
							}
						}
						else {
							cost[from][to] = cost[from][mid] + cost[mid][to];
							visit[from][to] = true;
							pass[from][to] = mid;
						}
					}
				}
			}
		}

		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				sb.append(visit[i][j] ? cost[i][j] : 0).append(" ");
			}
			sb.append("\n");
		}

		ArrayList<Integer> al = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(!visit[i][j] || i == j) {
					sb.append("0\n");
					continue;
				}
				al.clear();
				al.add(i + 1);
				getMiddlePath(pass, al, i, j);
				al.add(j + 1);
				sb.append(al.size()).append(" ");
				for(int e: al) sb.append(e).append(" ");
				sb.append("\n");
			}
		}
		print(sb);

	}

	static void getMiddlePath(int[][] arr, ArrayList<Integer> al, int start, int end) {
		if(arr[start][end] == -1) return;
		int middle = arr[start][end];
		getMiddlePath(arr, al, start, middle);
		al.add(middle + 1);
		getMiddlePath(arr, al, middle, end);
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.