forked from zapellass123/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloydWarshall.cpp
More file actions
52 lines (47 loc) · 1.04 KB
/
floydWarshall.cpp
File metadata and controls
52 lines (47 loc) · 1.04 KB
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
void Graph::propogateNegativeCycles(vector<vector<ll> > &dp, vector<vector<int> >&next){
int V = numOfNodes;
for(int k=1;k<=V;k++){
for(int i=1;i<=V;i++){
for(int j=1;j<=V;j++){
if(dp[i][k]+dp[k][j]<dp[i][j]){
dp[i][j] = -INF;
next[i][j] = -1;
}
}
}
}
}
// O(V*V*V)
vector<vector<ll> > Graph::floydWarshall(){
int V=numOfNodes;
vector<vector<ll> > dp(V+1,vector<ll>(V+1,INF));
vector<vector<int> > next(V+1,vector<int>(V+1,-1));
for(int i=1;i<=V;i++)
dp[i][i]=0LL;
for(auto edge:edgeW){
int from=edge.first.first, to=edge.first.second;
ll wt = edge.second;
dp[from][to] = wt;
next[from][to] = to;
}
for(int k=1;k<=V;k++){
for(int i=1;i<=V;i++){
for(int j=1;j<=V;j++){
if(dp[i][k]+dp[k][j]<dp[i][j]){
dp[i][j] = dp[i][k]+dp[k][j];
next[i][j] = next[i][k];
}
}
}
}
// propogateNegativeCycles(dp,next);
for(int i=1;i<=V;i++){
for(int j=1;j<=V;j++){
if(dp[i][j]<=-INF) cout<<"-INF ";
else if(dp[i][j]>=INF) cout<<"NP ";
else cout<<dp[i][j]<<" ";
}
cout<<"\n";
}
return dp;
}