-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloyd-Warshall.cpp
59 lines (54 loc) · 1.08 KB
/
Floyd-Warshall.cpp
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
#include<bits/stdc++.h>
using namespace std;
vector<int>path;
int matrix[100][100],next[100][100];
void findpath(int i, int j)
{
path.clear();
path.push_back(i);
while(i!=j)
{
i=next[i][j];
path.push_back(i);
}
for(i=path.size()-1;i>=0;i--)
{
printf("%d",path[i]);
if(i)
printf(" ");
}
printf("\n");
}
int main()
{
int i,j,k,node,edge;
memset(matrix,0,sizeof(matrix));
scanf("%d %d",&node,&edge);
for(i=0;i<edge;i++)
{
scanf("%d %d",&j,&k);
matrix[j][k]=1;
}
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
next[i][j]=j;
}
}
for(k=0;k<node;k++)
{
for(i=0;i<node;i++)
{
for(j=0;j<node;j++)
{
if(matrix[i][k]+matrix[k][j]<matrix[i][j])
{
matrix[i][j]=matrix[i][k]+matrix[k][j];
next[i][j]=next[i][k];
}
}
}
}
return 0;
}