-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFloyd_Warshall.cpp
More file actions
54 lines (46 loc) · 847 Bytes
/
Floyd_Warshall.cpp
File metadata and controls
54 lines (46 loc) · 847 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#define N 100
int dis[N][N];
#define inf 1e9+10
int main()
{
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(i==j) dis[i][j]=0;
else dis[i][j]=inf;
}
}
int v,e;
cin>>v>>e;
for(int i=0;i<e;i++)
{
int u,v,w;
cin>>u>>v>>w;
dis[u][v]=w;
}
for(int k=1;k<=v;k++){
for(int i=1;i<=v;i++){
for(int j=1;j<=v;j++){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
for(int i=1;i<=v;i++){
for(int j=1;j<=v;j++){
if(dis[i][j]==inf) cout<<"i ";
else cout<<dis[i][j]<<" ";
}
cout<<endl;
}
}
/*
--6x6 adjacent matrix
(1,6) distance=4
0 1 3 3 2 4
i 0 2 2 1 3
i i 0 i 2 4
i i i 0 3 1
i i i i 0 2
i i i i i 0
*/