-
Notifications
You must be signed in to change notification settings - Fork 0
/
CargoAgency.old.java
99 lines (75 loc) · 2.43 KB
/
CargoAgency.old.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
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
93
94
95
96
97
98
99
import java.util.stream.*;
import java.util.*;
import java.io.*;
//import java.math.BigInteger;
//import java.math.RoundingMode;
//import java.text.*;
public class CargoAgency
{
public static int table[][];
public static int n;
public static int[][] matrixMultiply(int m1[][], int m2[][]) {
int result[][] = new int[n][n];
for (int i = 0; i < n ; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = Integer.MAX_VALUE;
for (int k = 0; k < n; k++) {
if (m1[i][k] != Integer.MAX_VALUE && m2[k][j] != Integer.MAX_VALUE) {
result[i][j] = Math.min( result[i][j], m1[i][k]+m2[k][j]);
}
}
}
}
return result;
}
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
n = in.nextInt();
table = new int[n][n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++) {
if (i == j) {
table[i][j]=0;
}
else {
table[i][j]=Integer.MAX_VALUE;
}
}
}
int nc = n;
while (nc-- > 1) {
int x = in.nextInt() - 1;
int y = in.nextInt() - 1;
int cost = in.nextInt();
table[x][y] = cost;
table[y][x] = table[x][y];
}
printMatrix(table);
int[][] res = matrixMultiply(table, table);
table = matrixMultiply(res,table);
res = matrixMultiply(table, table);
printMatrix(table);
double sum = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++) {
if (i != j)
sum += table[i][j];
}
}
//System.out.println(""+sum);
/* DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println(df.format( sum/(n*(n-1))) );
*/
System.out.printf("%.4f\n", sum/(n*(n-1)));
}
static void printMatrix(int[][] grid) {
for(int r=0; r<grid.length; r++) {
for(int c=0; c<grid[r].length; c++)
System.out.print((grid[r][c] == Integer.MAX_VALUE?"∞":grid[r][c]) + " ");
System.out.println();
}
System.out.println();
}
}