-
Notifications
You must be signed in to change notification settings - Fork 0
/
excursie.cpp
88 lines (69 loc) · 1.84 KB
/
excursie.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
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
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
//#include <unordered_map>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (vector<ii>::iterator it = c.begin(); it != c.end(); it++)
int N;
int distances[200][200];
int numberCities[200][200];
void floydWarshall() {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
numberCities[i][j] = 1;
}
}
for(int i = 0; i < N; i++) {
numberCities[i][i] = 0;
}
for(int k = 0; k < N; k++) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(distances[i][k] + distances[k][j] < distances[i][j]) {
distances[i][j] = distances[i][k] + distances[k][j];
numberCities[i][j] = numberCities[i][k] + numberCities[k][j];
} else if(distances[i][k] + distances[k][j] == distances[i][j] &&
k != i && k != j) {
numberCities[i][j] = max(numberCities[i][j], numberCities[i][k] + numberCities[k][j]);
}
}
}
}
}
void printSolution() {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
cout << distances[i][j] << " ";
}
cout << endl;
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
cout << numberCities[i][j] << " ";
}
cout << endl;
}
}
int main() {
cin >> N;
memset(distances, -1, sizeof(distances));
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
cin >> distances[i][j];
}
}
floydWarshall();
printSolution();
return 0;
}