174 lines
3.8 KiB
C
174 lines
3.8 KiB
C
|
/* file name: shortestPath.c */
|
|||
|
/* <20>Q<EFBFBD><51> Dijkstra <20>t<EFBFBD><74><EFBFBD>k<EFBFBD>D<EFBFBD>̵u<CCB5><75><EFBFBD>| */
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
|
|||
|
#define MAX_V 100 /*<2A>̤j<CCA4>`<60>I<EFBFBD><49>*/
|
|||
|
#define VISITED 1
|
|||
|
#define NOTVISITED 0
|
|||
|
#define Infinite 1073741823
|
|||
|
|
|||
|
/* A[1..N][1..N] <20><><EFBFBD>ϧΪ<CFA7><CEAA>۾F<DBBE>x<EFBFBD>} */
|
|||
|
/* D[i] i=1..N <20>Ψ<EFBFBD><CEA8>x<EFBFBD>s<EFBFBD>Y<EFBFBD>_<EFBFBD>l<EFBFBD><6C><EFBFBD>I<EFBFBD><49>i<EFBFBD>`<60>I<EFBFBD><49><EFBFBD>̵u<CCB5>Z<EFBFBD><5A> */
|
|||
|
/* S[1..N] <20>ΨӰO<D3B0><4F><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD>O<EFBFBD>_<EFBFBD>w<EFBFBD>g<EFBFBD><67><EFBFBD>X<EFBFBD>L */
|
|||
|
/* P[1..N] <20>ΨӰO<D3B0><4F><EFBFBD>̪<EFBFBD><CCAA>g<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I */
|
|||
|
|
|||
|
long int A[MAX_V+1][MAX_V+1];
|
|||
|
long int D[MAX_V+1];
|
|||
|
long int S[MAX_V+1], P[MAX_V+1];
|
|||
|
int source , sink , N;
|
|||
|
int step = 1;
|
|||
|
|
|||
|
int top = -1; /*<2A><><EFBFBD>|<7C><><EFBFBD><EFBFBD>*/
|
|||
|
int Stack[MAX_V+1]; /*<2A><><EFBFBD>|<7C>Ŷ<EFBFBD>*/
|
|||
|
|
|||
|
void init(void);
|
|||
|
int minD(void);
|
|||
|
void output_step(void);
|
|||
|
void output_path(void);
|
|||
|
void push(int);
|
|||
|
int pop(void);
|
|||
|
|
|||
|
int main(){
|
|||
|
int t,I;
|
|||
|
|
|||
|
init();
|
|||
|
output_step();
|
|||
|
printf("\n");
|
|||
|
for (step =2;step <=N; step++) {
|
|||
|
/* minD <20>Ǧ^<5E>@<40><>t<EFBFBD>ϱoD[t] <20><><EFBFBD>̤p */
|
|||
|
t = minD();
|
|||
|
S[t] = VISITED;
|
|||
|
/* <20><><EFBFBD>X<EFBFBD>g<EFBFBD>Lt<4C>I<EFBFBD>|<7C>ϸ<EFBFBD><CFB8>|<7C>Y<EFBFBD>u<EFBFBD><75><EFBFBD>`<60>I*/
|
|||
|
for (I=1; I <= N; I++)
|
|||
|
if ((S[I] == NOTVISITED) && (D[t]+A[t][I] <= D[I])) {
|
|||
|
D[I] = D[t] + A[t][I];
|
|||
|
P[I] = t;
|
|||
|
}
|
|||
|
output_step();
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
output_path();
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
void init(){
|
|||
|
FILE *fptr;
|
|||
|
int i,j;
|
|||
|
int weight;
|
|||
|
|
|||
|
fptr = fopen("shortestPath1.dat","r");
|
|||
|
if (fptr == NULL) {
|
|||
|
perror("shortestPath.dat");
|
|||
|
exit(1);
|
|||
|
}
|
|||
|
|
|||
|
fscanf(fptr, "%d", &N); /*Ū<><C5AA><EFBFBD>ϧθ`<60>I<EFBFBD><49>*/
|
|||
|
for (i=1; i<=N; i++ )
|
|||
|
for (j=1; j<=N; j++)
|
|||
|
A[i][j] = Infinite; /*<2A>_<EFBFBD>lA[1..N][1..N]<5D>۾F<DBBE>x<EFBFBD>}*/
|
|||
|
|
|||
|
while (fscanf(fptr,"%d %d %d", &i, &j, &weight) != EOF)
|
|||
|
A[i][j] = weight; /*Ū<><C5AA>i<EFBFBD>`<60>I<EFBFBD><49>j<EFBFBD>`<60>I<EFBFBD><49><EFBFBD>vweight */
|
|||
|
fclose(fptr);
|
|||
|
|
|||
|
printf("Enter source node : ");
|
|||
|
scanf("%d", &source);
|
|||
|
printf("Enter sink node : ");
|
|||
|
scanf("%d", &sink);
|
|||
|
|
|||
|
/* <20>_<EFBFBD>l<EFBFBD>U<EFBFBD>}<7D>C<EFBFBD><43><EFBFBD><EFBFBD>*/
|
|||
|
for (i = 1; i <= N; i++) {
|
|||
|
S[i] = NOTVISITED; /*<2A>U<EFBFBD><55><EFBFBD>I<EFBFBD>]<5D><><EFBFBD>|<7C><><EFBFBD><EFBFBD><EFBFBD>X*/
|
|||
|
D[i] = A[source][i]; /*<2A>O<EFBFBD><4F><EFBFBD>_<EFBFBD>l<EFBFBD><6C><EFBFBD>I<EFBFBD>ܦU<DCA6><55><EFBFBD>I<EFBFBD>̵u<CCB5>Z<EFBFBD><5A>*/
|
|||
|
P[i] = source;
|
|||
|
}
|
|||
|
S[source] = VISITED; /*<2A>l<EFBFBD>_<EFBFBD>`<60>I<EFBFBD>]<5D><><EFBFBD>w<EFBFBD>g<EFBFBD><67><EFBFBD>X*/
|
|||
|
D[source] = 0;
|
|||
|
}
|
|||
|
|
|||
|
int minD(){
|
|||
|
int i,t;
|
|||
|
long int minimum = Infinite;
|
|||
|
|
|||
|
for (i=1; i<=N; i++)
|
|||
|
if ((S[i] == NOTVISITED) && D[i] < minimum) {
|
|||
|
minimum = D[i];
|
|||
|
t = i;
|
|||
|
}
|
|||
|
|
|||
|
return t;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ܥثe<D8AB><65>D<EFBFBD>}<7D>C<EFBFBD>PP<50>}<7D>C<EFBFBD><43><EFBFBD>p */
|
|||
|
void output_step(){
|
|||
|
int i;
|
|||
|
|
|||
|
printf("\n Step #%d",step);
|
|||
|
printf("\n================================================\n");
|
|||
|
for (i=1; i<=N; i++)
|
|||
|
printf(" D[%d]", i);
|
|||
|
printf("\n");
|
|||
|
for (i=1; i<=N; i++)
|
|||
|
if (D[i] == Infinite)
|
|||
|
printf(" ----");
|
|||
|
else
|
|||
|
printf("%6ld",D[i]);
|
|||
|
printf("\n================================================\n");
|
|||
|
for (i=1; i<=N; i++)
|
|||
|
printf(" P[%d]", i);
|
|||
|
printf("\n");
|
|||
|
for (i=1; i<=N;i++)
|
|||
|
printf("%6ld", P[i]);
|
|||
|
}
|
|||
|
|
|||
|
/*<2A><><EFBFBD>̵ܳu<CCB5><75><EFBFBD>|*/
|
|||
|
void output_path(){
|
|||
|
int node = sink;
|
|||
|
|
|||
|
/*<2A>P<EFBFBD>_<EFBFBD>O<EFBFBD>_<EFBFBD>_<EFBFBD>l<EFBFBD><6C><EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD>εL<CEB5><4C><EFBFBD>|<7C>ܲ<EFBFBD><DCB2>I*/
|
|||
|
if ((sink == source) || (D[sink] == Infinite)) {
|
|||
|
printf("\nNode %d has no Path to Node %d", source, sink);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
printf("\n");
|
|||
|
printf(" The shortest Path from V%d to V%d :", source, sink);
|
|||
|
printf("\n------------------------------------------\n");
|
|||
|
|
|||
|
/*<2A>Ѳ<EFBFBD><D1B2>I<EFBFBD>}<7D>l<EFBFBD>N<EFBFBD>W<EFBFBD>@<40><><EFBFBD>g<EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>`<60>I<EFBFBD><49><EFBFBD>J<EFBFBD><4A><EFBFBD>|<7C>ܨ<EFBFBD><DCA8>_<EFBFBD>l<EFBFBD>`<60>I*/
|
|||
|
printf(" V%d", source);
|
|||
|
while (node != source) {
|
|||
|
push(node);
|
|||
|
node = P[node];
|
|||
|
}
|
|||
|
while(node != sink) {
|
|||
|
node = pop();
|
|||
|
printf(" --%ld-->",A[P[node]][node]);
|
|||
|
printf("V%d", node);
|
|||
|
}
|
|||
|
|
|||
|
printf("\n Total length : %ld\n", D[sink]);
|
|||
|
}
|
|||
|
|
|||
|
void push(int value){
|
|||
|
if (top >= MAX_V) {
|
|||
|
printf("Stack overflow!\n");
|
|||
|
exit(1);
|
|||
|
}
|
|||
|
else
|
|||
|
Stack[++top] = value;
|
|||
|
}
|
|||
|
|
|||
|
int pop(){
|
|||
|
if (top < 0) {
|
|||
|
printf("Stack empty!\n");
|
|||
|
exit(1);
|
|||
|
}
|
|||
|
|
|||
|
return Stack[top--];
|
|||
|
}
|
|||
|
|