-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloywarshall.cpp
111 lines (95 loc) · 2 KB
/
floywarshall.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* https://www.spoj.com/problems/TRVCOST/
*/
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
using namespace std;
#define max 10000
#define INF 1e9
#define M 500
const int inf = 1e9; //10^9
vector<vector<int>> graph;
vector<vector<int>> dist;
vector<vector<int>> path;
int n;
void printPath(int s, int t){
int b[max];
int m = 0;
while(s!=t){
b[m++] = t;
t = path[s][t];
}
b[m++] = s;
for (int i = m-1; i >= 0; i--){
cout << b[i] << "";
}
}
bool floydWarshall(vector<vector<int>>& graph, vector<vector<int>>& dist, int& h)
{
int i,j,k;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
dist[i][j] = graph[i][j];
if (graph[i][j] != inf && i != j)
{
path[i][j] = i;
}
else
{
path[i][j] = -1;
}
}
}
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
dist[i][j] = graph[i][j];
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = path[k][j];
}
}
}
}
}
int main()
{
int T, b;
string a = "";
cin >> T;
graph = vector<vector<int>> (M, vector<int>(M));
dist = vector<vector<int>> (M, vector<int>(M));
path = vector<vector<int>> (M, vector<int>(M));
for(int i = 0; i < T; i++){
for(int j = 0; j < T; j++){
cin >> a;
if(a == "Y"){
graph[i][j] = 1;
}
else if(a == "N"){
graph[i][j] = 0;
}
}
}
for(int i = 0; i < T; i++){
for(int j = 0; j < T; j++){
cout << graph[i][j];
}
}
int h = 0;
floydWarshall(graph, dist, h);
system("pause");
//int result = dist[][];
getchar();
return 0;
}