File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+
7+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ static StringTokenizer st;
9+ static int V ,E ;
10+ static int [][] matrix;
11+ static int INF = 1000000007 ;
12+ static int ans = INF ;
13+
14+ public static void main (String [] args ) throws Exception {
15+ st = new StringTokenizer (br. readLine());
16+ V = Integer . parseInt(st. nextToken());
17+ E = Integer . parseInt(st. nextToken());
18+
19+ matrix = new int [V + 1 ][V + 1 ];
20+ for (int i = 0 ; i <= V ; i++ ) {
21+ for (int j = 0 ; j <= V ; j++ ) {
22+ if (i != j) {
23+ matrix[i][j] = INF ;
24+ }
25+ }
26+ }
27+
28+ for (int i = 0 ; i < E ; i++ ) {
29+ st = new StringTokenizer (br. readLine());
30+ int from = Integer . parseInt(st. nextToken());
31+ int to = Integer . parseInt(st. nextToken());
32+ int cost = Integer . parseInt(st. nextToken());
33+ matrix[from][to] = cost;
34+ }
35+
36+ for (int k = 1 ; k <= V ; k++ ) for (int i = 1 ; i <= V ; i++ ) for (int j = 1 ; j <= V ; j++ ) {
37+ matrix[i][j] = Math . min(matrix[i][j], matrix[i][k] + matrix[k][j]);
38+ }
39+
40+ for (int i = 1 ; i <= V ; i++ ) for (int j = 1 ; j <= V ; j++ ) {
41+ if (i != j) ans = Math . min(ans, matrix[i][j] + matrix[j][i]);
42+ }
43+
44+ System . out. println(ans >= INF ? - 1 : ans);
45+ }
46+ }
47+ ```
You can’t perform that action at this time.
0 commit comments