다익스트라 질문이요 ㅠ
들햇님
다익스트라 프로그램짜고있는데요...
경로는 최단거리로 나오는데최단거리 합이 -1로 나와요 ㅠ어디서 잘못된거죠 ㅠㅠㅠㅠㅠㅠㅠㅠㅠ
#includestdio.h
void dijkstra(int n, const int W[][100], int m){ int length[100];
int touch[100];
int Y[100];
int st = 1; int i,j,min,vnear; for(i=2;i=n;i++){
touch[i]=st,touch[st]=0;
length[i]=W[1][i];
Y[i] = 0;
} for(i=2;i=n;i++){
min=9999;
for(j=2;j=n;j++){
if(0 = length[j] && length[j]min){
min=length[j];
vnear=j;
}
} length[vnear] = -1;
if(min== 999)
printf(노답\n);
for(j=2;j=n;j++){
if(length[vnear] + W[vnear][j] length[j])
{
length[j] = length[vnear] + W[vnear][j];
touch[j] = vnear;
}
}
} for(i=2;i=n;i++){
printf(%d : %d,length[i],i); --- length[i] 이부분이 전부 -1로 나오네요..
vnear = i;
while(touch[vnear] != 0){
printf(-- %d,touch[vnear]);
vnear = touch[vnear];
}
printf(\n);
}
}void main(){ int n,m; //n은 n*n행렬할때 n, m 은 간선을 의미
int W[100][100]; int i,j,x,y,z;
FILE *fp=fopen(dijkstra.txt,r);
fscanf(fp,%d %d,&n,&m);
for(i=1;i=n;i++){
for(j=1;j=n;j++){
if(i == j)
W[i][j] = 0;
else
W[i][j]=9999;
}
}
for(i=0;im;i++){
fscanf(fp,%d %d %d,&x,&y,&z);
W[x][y]=z;
} fclose(fp);
//배열 출력
for(i=1;i=n;i++){
for(j=1;j=n;j++){
printf(%5d ,W[i][j]);
}printf(\n);
}
printf(\n); dijkstra(n, W, m);
}