【迪杰斯特拉算法】【求助】
#include <stdio.h>#include <conio.h>
#define n 5
int dim1[12];
/*int c[6][6]={{0,0,10,32767,30,100},{0,32767,0,50,32767,32767},{0,32767,32767,0,32767,10},{0,32767,32767,20,0,60},{0,32767,32767,32767,32767,0}};*/
int c[6][6]={{0,0,7,2,8,32767},{0,7,0,3,32767,4},{0,2,3,0,4,8},{0,8,32767,4,0,32767},{0,32767,4,8,32767,0}};
int prev[6];
int s[n];
int dist[n];
int i,v,j;
int Dijkstra(int v)
{
int u,temp,newdist;
for(i=1;i<=n;i++)
{dist[i]=c[v][i];
s[i]=0;
prev[i]=v;
}
dist[v]=0;
s[v+1]=1;
for(i=1;i<=n;i++)
printf("%d ",dist[i]);
printf("\n ");
getch();
for(i=1;i<=n;i++)
{temp=32767;
u=v;
for(j=1;j<=n;j++)
{if((s[j]==0)&&(dist[j]<temp))
{u=j;
temp=dist[j];
}
}
s[u]=1;
printf("%d :",u);
for(j=1;j<=n;j++)
{if((s[j]==0)&&(c[u][j]<32767))
{
if(dist[u]+c[u][j]<dist[j])
{dist[j]=dist[u]+c[u][j];
prev[j]=u;
}
}
}
for(i=1;i<=n;i++)
printf("%d ",dist[i]);
printf("\n ");getch();
}
}
int main()
{
v=0;
Dijkstra(v);
for(i=1;i<=n;i++)
printf("%d ",dist[i]);
printf("i=");
/*while(i!=v)
{printf("%d <---",i=prev[i]);
}*/
getch();}