Skip to content

Commit ac3c4f1

Browse files
Merge pull request matthewsamuel95#87 from naruto31/patch-3
floydWarshall.cpp
2 parents 211f24f + e900069 commit ac3c4f1

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Graph/floydWarshall.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define maxn 1009
5+
#define inf 10000009
6+
7+
int graph[maxn][maxn];
8+
int dist[maxn][maxn];
9+
10+
void floydWarshall(int v){
11+
for(int k=0;k<v;k++){
12+
for(int i=0;i<v;i++){
13+
for(int j=0;j<v;j++){
14+
dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);
15+
}
16+
}
17+
}
18+
}
19+
20+
int main(){
21+
22+
int numV, numE;
23+
cin>>numV>>numE;
24+
25+
for(int i=0;i<numV;i++){
26+
for(int j=0;j<numV;j++){
27+
dist[i][j]=inf;
28+
if(i==j)dist[i][j]=0;
29+
}
30+
}
31+
32+
for(int i=0;i<numE;i++){
33+
int u,v,w;
34+
// Assuming 0-based indexing
35+
cin>>u>>v>>w;
36+
graph[u][v]=w;
37+
dist[u][v]=w;
38+
// undirected graph
39+
graph[v][u]=w;
40+
dist[v][u]=w;
41+
}
42+
43+
44+
floydWarshall(numV);
45+
46+
for(int i=0;i<numV;i++){
47+
for(int j=0;j<numV;j++){
48+
if(dist[i][j]==inf){
49+
cout<<"inf\t";continue;
50+
}
51+
cout<<dist[i][j]<<"\t";
52+
}cout<<endl;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)