标签:
源代码:
#include<cstdio>
int i[1001][1001],n,k;
bool f[1001]={0};
const int maxint=10000000;
int main()
{
scanf("%d%d",&n,&k);
for (int a=1;a<=n;a++)
for (int b=1;b<=n;b++)
{
scanf("%d",&i[a][b]);
if (i[a][b]==-1)
i[a][b]=maxint;
}
f[k]=true;
for (int a=2;a<=n;a++)
{
int t=maxint,s;
for (int b=1;b<=n;b++)
if (!f[b]&&(i[k][b]<t))
{
t=i[k][b];
s=b;
}
f[s]=true;
for (int b=1;b<=n;b++)
if (!f[b]&&(i[k][s]+i[s][b]<i[k][b]))
i[k][b]=i[k][s]+i[s][b];
}
for (int a=1;a<=n;a++)
printf("%d --> %d = %d\n",k,a,i[k][a]);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/koruko/p/5115628.html