注意记录后缀的方法。。
#include <stdio.h>
#include <string.h>
int a[50][50];
int c[50][50];
int b[50];
int find(int beg,int end)
{
while(c[beg][end]!=end)
{
printf("-->%d",c[beg][end]);
beg=c[beg][end];
}
printf("-->%d\n",end);
return 0;
}
int main ()
{
int i,j,k,n,beg,end;
while (scanf("%d",&n)!=EOF)
{
if (n==0) return 0;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
c[i][j]=j;
}
}
for (i=1;i<=n;i++)
scanf("%d",&b[i]);
for (k=1;k<=n;k++)
{
for (i=1;i<=n;i++)
{
if (i==k || a[k][i]==-1) continue;
for (j=1;j<=n;j++)
{
if (a[k][j]==-1 || k==j || i==j) continue;
if (a[i][j]==-1 || a[i][j]>a[i][k]+a[k][j]+b[k])
{
a[i][j]=a[i][k]+a[k][j]+b[k];
c[i][j]=c[i][k];
}
if (a[i][j]==a[i][k]+a[k][j]+b[k] && c[i][k]<c[i][j])
c[i][j]=c[i][k];
}
}
}
while (scanf("%d %d",&beg,&end)!=EOF)
{
if (beg==-1 && end==-1) break;
printf ("From %d to %d :\n",beg,end);
if(beg==end)
printf("Path: %d\n",beg);
else
{
printf ("Path: %d",beg);
find(beg,end);
}
printf ("Total cost : %d\n\n",a[beg][end]);
}
}
return 0;
}