132 lines
2.7 KiB
C++
132 lines
2.7 KiB
C++
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
|
|||
|
#define MAX_V 100 /*<2A>̤j<CCA4>`<60>I<EFBFBD><49>*/
|
|||
|
#define Infinite 1073741823
|
|||
|
|
|||
|
long int dist[MAX_V+1][MAX_V+1];
|
|||
|
int N; // <20>`<60>I<EFBFBD>ƶq
|
|||
|
int source, sink; // <20>_<EFBFBD>I<EFBFBD>M<EFBFBD><4D><EFBFBD>I
|
|||
|
|
|||
|
void init();
|
|||
|
int floydWarshall();
|
|||
|
void output_path();
|
|||
|
void output_step();
|
|||
|
|
|||
|
int main() {
|
|||
|
|
|||
|
init();
|
|||
|
|
|||
|
// <20>ˬd<CBAC>O<EFBFBD>_<EFBFBD>s<EFBFBD>b<EFBFBD>t<EFBFBD>v<EFBFBD>j<EFBFBD><6A>
|
|||
|
if (floydWarshall()) {
|
|||
|
output_path();
|
|||
|
printf("\n<EFBFBD>ϧΤ<EFBFBD><EFBFBD>s<EFBFBD>b<EFBFBD>t<EFBFBD>v<EFBFBD>j<EFBFBD><EFBFBD><EFBFBD>A<EFBFBD>L<EFBFBD>k<EFBFBD>p<EFBFBD><EFBFBD><EFBFBD>̵u<EFBFBD><EFBFBD><EFBFBD>|\n");
|
|||
|
} else {
|
|||
|
output_path();
|
|||
|
}
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
void init(){
|
|||
|
FILE *fptr;
|
|||
|
int i, j, weight;
|
|||
|
|
|||
|
fptr = fopen("shortestPath.dat", "r");
|
|||
|
if (fptr == NULL) {
|
|||
|
perror("shortestPath.dat");
|
|||
|
exit(1);
|
|||
|
}
|
|||
|
|
|||
|
fscanf(fptr, "%d", &N); // Ū<><C5AA><EFBFBD>ϧθ`<60>I<EFBFBD><49>
|
|||
|
|
|||
|
// <20><><EFBFBD>l<EFBFBD>ƶZ<C6B6><5A><EFBFBD>x<EFBFBD>}
|
|||
|
for (i = 1; i <= N; i++) {
|
|||
|
for (j = 1; j <= N; j++) {
|
|||
|
if (i == j)
|
|||
|
dist[i][j] = 0; // <20>ۨ<EFBFBD><DBA8><EFBFBD><EFBFBD>ۨ<EFBFBD><DBA8><EFBFBD><EFBFBD>Z<EFBFBD><5A><EFBFBD><EFBFBD>0
|
|||
|
else
|
|||
|
dist[i][j] = Infinite; // <20><><EFBFBD>L<EFBFBD>w<EFBFBD>]<5D><><EFBFBD>L<EFBFBD>a<EFBFBD>j
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Ū<><C5AA><EFBFBD>䪺<EFBFBD>v<EFBFBD><76>
|
|||
|
while (fscanf(fptr, "%d %d %d", &i, &j, &weight) != EOF)
|
|||
|
dist[i][j] = weight; // <20>]<5D>m<EFBFBD><6D><EFBFBD><EFBFBD><EFBFBD>۳s<DBB3>䪺<EFBFBD>v<EFBFBD><76>
|
|||
|
|
|||
|
fclose(fptr);
|
|||
|
printf("<EFBFBD>ɮפ<EFBFBD><EFBFBD>v<EFBFBD><EFBFBD><EFBFBD>p<EFBFBD>U\n");
|
|||
|
output_step();
|
|||
|
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>_<EFBFBD>l<EFBFBD>`<60>I (1~%d): ", N);
|
|||
|
scanf("%d", &source);
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>ؼи`<60>I (1~%d): ", N);
|
|||
|
scanf("%d", &sink);
|
|||
|
}
|
|||
|
|
|||
|
int floydWarshall(){
|
|||
|
int k, i, j;
|
|||
|
|
|||
|
for (k = 1; k <= N; k++) {
|
|||
|
for (i = 1; i <= N; i++) {
|
|||
|
for (j = 1; j <= N; j++) {
|
|||
|
// <20>p<EFBFBD>G<EFBFBD>g<EFBFBD>L k <20>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>|<7C><EFBFBD><F1AABDB1><EFBFBD><EFBFBD>|<7C>u<EFBFBD>A<EFBFBD><41><EFBFBD>s<EFBFBD>Z<EFBFBD><5A>
|
|||
|
if (dist[i][k] != Infinite &&
|
|||
|
dist[k][j] != Infinite &&
|
|||
|
dist[i][k] + dist[k][j] < dist[i][j]) {
|
|||
|
dist[i][j] = dist[i][k] + dist[k][j];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>ˬd<CBAC>O<EFBFBD>_<EFBFBD>s<EFBFBD>b<EFBFBD>t<EFBFBD>v<EFBFBD>j<EFBFBD><6A>
|
|||
|
for (i = 1; i <= N; i++) {
|
|||
|
if (dist[i][i] < 0) {
|
|||
|
return 1; // <20>s<EFBFBD>b<EFBFBD>t<EFBFBD>v<EFBFBD>j<EFBFBD><6A>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return 0; // <20><><EFBFBD>s<EFBFBD>b<EFBFBD>t<EFBFBD>v<EFBFBD>j<EFBFBD><6A>
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void output_step(){
|
|||
|
int i, j;
|
|||
|
|
|||
|
printf("\t");
|
|||
|
for (j = 1; j <= N; j++) {
|
|||
|
printf("V%d\t", j);
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
|
|||
|
// <20><><EFBFBD>X<EFBFBD>Z<EFBFBD><5A><EFBFBD>x<EFBFBD>}
|
|||
|
for (i = 1; i <= N; i++) {
|
|||
|
printf("V%d\t", i);
|
|||
|
for (j = 1; j <= N; j++) {
|
|||
|
if (dist[i][j] == Infinite)
|
|||
|
printf("<EFBFBD><EFBFBD>\t");
|
|||
|
else
|
|||
|
printf("%2ld\t", dist[i][j]);
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void output_path(){
|
|||
|
int i, j;
|
|||
|
|
|||
|
// <20><><EFBFBD>X<EFBFBD><58><EFBFBD>D<EFBFBD>C
|
|||
|
printf("\n<EFBFBD>`<60>I<EFBFBD><49><EFBFBD>̵u<CCB5>Z<EFBFBD><5A><EFBFBD>x<EFBFBD>}<7D>G\n");
|
|||
|
|
|||
|
output_step();
|
|||
|
|
|||
|
// <20>ˬd<CBAC>_<EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>|
|
|||
|
if (dist[source][sink] == Infinite) {
|
|||
|
printf("\n<EFBFBD>`<60>I V%d <20><><EFBFBD>`<60>I V%d <20>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>|\n", source, sink);
|
|||
|
} else {
|
|||
|
printf("\n<EFBFBD>q<EFBFBD>`<60>I V%d <20><><EFBFBD>`<60>I V%d <20><><EFBFBD>̵u<CCB5>Z<EFBFBD><5A><EFBFBD><EFBFBD><EFBFBD>G%ld\n",
|
|||
|
source, sink, dist[source][sink]);
|
|||
|
}
|
|||
|
}
|