File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main {
5+
6+ public static final int MAX = 10_000_001 ;
7+
8+ public static void main (String [] args ) throws IOException {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10+ int n = Integer .parseInt (br .readLine ());
11+ int m = Integer .parseInt (br .readLine ());
12+ int [][] graph = new int [n +1 ][n +1 ];
13+ for (int i =1 ; i <=n ; i ++) {
14+ for (int j =1 ; j <=n ; j ++) {
15+ if (i == j ) {
16+ graph [i ][j ] = 0 ;
17+ }else {
18+ graph [i ][j ] = MAX ;
19+ }
20+ }
21+ }
22+ for (int i =0 ; i <m ; i ++) {
23+ StringTokenizer st = new StringTokenizer (br .readLine ());
24+ int a = Integer .parseInt (st .nextToken ());
25+ int b = Integer .parseInt (st .nextToken ());
26+ int c = Integer .parseInt (st .nextToken ());
27+ graph [a ][b ] = Math .min (graph [a ][b ], c );
28+ }
29+ for (int k =1 ; k <=n ; k ++) {
30+ for (int i =1 ; i <=n ; i ++) {
31+ for (int j =1 ; j <=n ; j ++) {
32+ graph [i ][j ] = Math .min (graph [i ][j ], graph [i ][k ] + graph [k ][j ]);
33+ }
34+ }
35+ }
36+ StringBuffer sb = new StringBuffer ();
37+ for (int i =1 ; i <=n ; i ++) {
38+ for (int j =1 ; j <=n ; j ++) {
39+ sb .append ((graph [i ][j ] == MAX ) ? 0 : graph [i ][j ]).append (' ' );
40+ }
41+ sb .append ('\n' );
42+ }
43+ System .out .print (sb );
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments