-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloydWarshall.cpp
More file actions
65 lines (55 loc) · 1.2 KB
/
floydWarshall.cpp
File metadata and controls
65 lines (55 loc) · 1.2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
#define MAX 100
#define INF 3<<20
using namespace std;
floydWarshall(int nodes,int G[][MAX])
{
for(int k=1;k<=nodes;k++)
{
for(int i=1;i<=nodes;i++)
{
for(int j=1;j<=nodes;j++)
{
G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
}
}
}
}
int main()
{
int nodes,edges;
printf("Enter the number of nodes and edges:");
scanf("%d%d",&nodes,&edges);
int G[MAX][MAX];
for(int i=1;i<=nodes;i++)
{
for(int j=1;j<=nodes;j++)
{
if(i==j) G[i][j]=0;
else G[i][j]=INF;
}
}
printf("Enter edges with their costs:\n");
for(int i=1;i<=edges;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
G[u][v]=c;
}
floydWarshall(nodes,G);
printf("All pair shortest paths:\n");
for(int i=1;i<=nodes;i++)
{
for(int j=1;j<=nodes;j++)
{
if(G[i][j]==INF)
{
printf("INF\t");
continue;
}
printf("%d\t",G[i][j]);
}
printf("\n");
}
return 0;
}