-
Notifications
You must be signed in to change notification settings - Fork 4
/
royfloyd.cpp
49 lines (41 loc) · 957 Bytes
/
royfloyd.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
vector<vector<long long>> roadMat;
void InitMats(size_t n) {
roadMat.resize(n);
for(size_t i = 0; i < n; ++i) {
roadMat[i].resize(n,0);
}
}
void ReadPondMat(size_t n) {
for(size_t i = 0; i < n; ++i)
for(size_t j = 0; j < n; ++j)
in >> roadMat[i][j];
}
void GenRoadMat(size_t n) {
for(size_t k = 0; k < n; ++k)
for(size_t i = 0; i < n; ++i)
for(size_t j = 0; j < n; ++j)
if(roadMat[i][k] && roadMat[k][j] &&
(!roadMat[i][j] || roadMat[i][j] > roadMat[i][k] + roadMat[k][j]) && i != j)
roadMat[i][j] = roadMat[i][k]+roadMat[k][j];
}
void PrintRoadMat(size_t n) {
for(size_t i = 0; i < n; ++i) {
for(size_t j = 0; j < n; ++j)
out << roadMat[i][j] << ' ';
out << '\n';
}
}
int main() {
size_t n;
in >> n;
InitMats(n);
ReadPondMat(n);
GenRoadMat(n);
PrintRoadMat(n);
}