-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloyd_warshall.cpp
More file actions
56 lines (46 loc) · 1023 Bytes
/
floyd_warshall.cpp
File metadata and controls
56 lines (46 loc) · 1023 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
55
56
/************** Floyd Warshall ***********/
#include<stdio.h>
#include<math.h>
#define SZ 105
#define INF 2147483647
int nV, nC, wght[SZ][SZ];
int min(int a, int b){
return (a<b)?a:b;
}
int max(int a, int b){
return (a>b)?a:b;
}
void init(){
int i, j;
for(i=1; i<=nV; i++)
for(j=1; j<=nV; j++)
wght[i][j] = -INF;
}
void floyd_warshall(){
int i, j, k;
for(k=1; k<=nV; k++)
for(i=1; i<=nV; i++)
for(j=1; j<=nV; j++)
wght[i][j] = min(wght[i][j], wght[i][k]+wght[k][j]);
}
int main(){
int u, v, w, i, cs=1;
scanf("%d%d", &nV, &nC);
init();
for(i=0; i<nC; i++){
scanf("%d%d%d", &u, &v, &w);
wght[u][v] = wght[v][u] = w;
}
floyd_warshall();
scanf("%d%d%d", &u, &v);
printf("%d\n", wght[u][v]);
return 0;
}
/***************************
to find the max value
wght[i][j] = max(wght[i][j], min(wght[i][k], wght[k][j]));
***************************/
/***************************
to find the max value
wght[i][j] = min(wght[i][j], max(wght[i][k], wght[k][j]));
***************************/