-
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
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import utils.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)
*/
class A{
private InputStream inputStream ;
private OutputStream outputStream ;
private FastReader in ;
private PrintWriter out ;
private final int BUFFER = 100005;
private final long mod = 1000000000+7;
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
public A(){}
public A(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
outputStream = System.out;
}else{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
in = new FastReader(inputStream);
out = new PrintWriter(outputStream);
}
final int MAX_N = 100;
long cost[][] = new long[MAX_N + 1][MAX_N + 1];
long weight[][] = new long[MAX_N + 1][MAX_N + 1];
void run()throws Exception{
int n = i();
int ans = 0;
initialize();
out.write(""+ans+"\n");
}
void initialize(){
for(int i = 1; i <= MAX_N; i++){
for(int j = 1; j <= MAX_N; j++){
weight[i][j] = INF_L;
if(i==j)weight[i][j] = 0L;
}
}
}
void allPairShortestPath(int n){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cost[i][j] = weight[i][j];
}
}
// order matters: k->i->j
for(int k = 1; k <= n; k++){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if(cost[i][k] + cost[k][j] < cost[i][j]){
cost[i][j] = cost[i][k] + cost[k][j];
}
}
}
}
}
int i()throws Exception{
return in.nextInt();
}
long l()throws Exception{
return in.nextLong();
}
double d()throws Exception{
return in.nextDouble();
}
char c()throws Exception{
return in.nextCharacter();
}
String s()throws Exception{
return in.nextLine();
}
BigInteger bi()throws Exception{
return in.nextBigInteger();
}
private void closeResources(){
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;
public static void main(String[] args) throws java.lang.Exception{
A driver = new A(true);
driver.run();
driver.closeResources();
}
}
class Pair implements Comparable<Pair>{
public int a;
public int b;
public Pair(){
this.a = 0;
this.b = 0;
}
public Pair(int a,int b){
this.a = a;
this.b = b;
}
public int compareTo(Pair p){
if(this.a == p.a){
return this.b - p.b;
}
return this.a - p.a;
}
@Override
public String toString(){
return "a = " + this.a + " b = " + this.b;
}
}