forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd Warshall
53 lines (52 loc) · 1 KB
/
floyd Warshall
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
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> APSP_floyd(vector<vector<int>>weight,int n)
{
vector<vector<int>>dis(n+1,vector<int>(n+1,0));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((weight[i][j]==0) && i!=j)
{
dis[i][j]=INT_MAX/2;
}
else
{
dis[i][j]=weight[i][j];
}
}
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
return dis;
}
int main()
{
int v,e;
cin>>v>>e;
vector<vector<int>>weight(v+1,vector<int>(v+1,0));
for(int i=1;i<=e;i++)
{
int a,b,w;
cin>>a>>b>>w;
weight[a][b]=w;
}
vector<vector<int>>ans=APSP_floyd(weight,v);
for(int i=1;i<=v;i++)
{
for(int j=1;j<=v;j++)
{
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
}