- Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAllPairShortestPath.java
149 lines (124 loc) · 3.05 KB
/
AllPairShortestPath.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
importjava.util.*;
importjava.lang.*;
importjava.io.*;
importjava.math.*;
importutils.io.FastReader;
/*
* Author : joney_000[developer.jaswant@gmail.com]
* Algorithm : All Pair Shortest Path
* Platform : Codeforces
* Ref : Time Complexity: O(N^3), Space Complexity: O(N^2)
*/
classA{
privateInputStreaminputStream ;
privateOutputStreamoutputStream ;
privateFastReaderin ;
privatePrintWriterout ;
privatefinalintBUFFER = 100005;
privatefinallongmod = 1000000000+7;
privatefinalintINF = Integer.MAX_VALUE;
privatefinallongINF_L = Long.MAX_VALUE / 10;
publicA(){}
publicA(booleanstdIO)throwsFileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
outputStream = System.out;
}else{
inputStream = newFileInputStream("input.txt");
outputStream = newFileOutputStream("output.txt");
}
in = newFastReader(inputStream);
out = newPrintWriter(outputStream);
}
finalintMAX_N = 100;
longcost[][] = newlong[MAX_N + 1][MAX_N + 1];
longweight[][] = newlong[MAX_N + 1][MAX_N + 1];
voidrun()throwsException{
intn = i();
intans = 0;
initialize();
out.write(""+ans+"\n");
}
voidinitialize(){
for(inti = 1; i <= MAX_N; i++){
for(intj = 1; j <= MAX_N; j++){
weight[i][j] = INF_L;
if(i==j)weight[i][j] = 0L;
}
}
}
voidallPairShortestPath(intn){
for (inti = 1; i <= n; i++){
for (intj = 1; j <= n; j++){
cost[i][j] = weight[i][j];
}
}
// order matters: k->i->j
for(intk = 1; k <= n; k++){
for (inti = 1; i <= n; i++){
for (intj = 1; j <= n; j++){
if(cost[i][k] + cost[k][j] < cost[i][j]){
cost[i][j] = cost[i][k] + cost[k][j];
}
}
}
}
}
inti()throwsException{
returnin.nextInt();
}
longl()throwsException{
returnin.nextLong();
}
doubled()throwsException{
returnin.nextDouble();
}
charc()throwsException{
returnin.nextCharacter();
}
Strings()throwsException{
returnin.nextLine();
}
BigIntegerbi()throwsException{
returnin.nextBigInteger();
}
privatevoidcloseResources(){
out.flush();
out.close();
return;
}
// IMP: roundoff upto 2 digits
// double roundOff = Math.round(a * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
// print upto 2 digits after decimal
// val = ((long)(val * 100.0))/100.0;
publicstaticvoidmain(String[] args) throwsjava.lang.Exception{
Adriver = newA(true);
driver.run();
driver.closeResources();
}
}
classPairimplementsComparable<Pair>{
publicinta;
publicintb;
publicPair(){
this.a = 0;
this.b = 0;
}
publicPair(inta,intb){
this.a = a;
this.b = b;
}
publicintcompareTo(Pairp){
if(this.a == p.a){
returnthis.b - p.b;
}
returnthis.a - p.a;
}
@Override
publicStringtoString(){
return"a = " + this.a + " b = " + this.b;
}
}