一段程序的分析
//帮忙做一段程序的分析//程序如下:
#define MAXNUMBER 10
#define MAXLENGTH 5000
int weight[MAXNUMBER][MAXNUMBER];
int findshortestpath(int *includearray,int i,int includenumber)
{
int totalweight,value;
int t,j,k=2,point=2,minlength=MAXLENGTH;
int select[MAXNUMBER][MAXNUMBER];
if(includenumber==1)
return(weight[1][*(includearray+i)]);
value=*(includearray+i);
for(j=2;j<=includenumber+1;j++)
{
k=2;
for(t=2;t<=includenumber+1;t++)
if(t!=i)
select[j][k++]=*(includearray+t);
if(j<i)
{
totalweight=findshortestpath(select[j],j,includenumber-1);
totalweight=totalweight+weight[select[j][j]][value];
}
else if(j>i)
{
totalweight=findshortestpath(select[j],j-1,includenumber-1);
totalweight=totalweight+weight[select[j][j-1]][value];
}
else if(j==i)
continue;
if(totalweight<minlength)
{
point=j;
minlength=totalweight;
}
}
for(j=2;j<includenumber+1;j++)
*(includearray+j)=select[point][j];
*(includearray+j)=value;
return(minlength);
}
main()
{
int number,minlenght=MAXLENGTH;
int i,j,point=1,totalweight;
int vallagename[MAXNUMBER][MAXNUMBER];
printf("\nplease input the number of the vallage:");
scanf("%d",&number);
if(number<=0)
{
printf("input error!");
return;
}
for(i=1;i<=number;i++)
for(j=1;j<=number;j++)
{
printf("the weight[%d][%d]:",i,j);
if(i!=j)
scanf("%d",&weight[i][j]);
else
printf("0\n");
vallagename[i][j]=j;
}
for(i=1;i<=number;i++)
for(j=1;j<=number;j++)
{
printf("%d ",weight[i][j]);
if(j==number)
printf("\n");
}
for(i=1;i<=number;i++)
for(j=1;j<=number;j++)
{
if(j==1)
printf("\n");
printf("%d ",vallagename[i][j]);
}
for(i=2;i<=number;i++)
{
totalweight=findshortestpath(vallagename[i],i,number-1);
totalweight+=weight[i][1];
if(totalweight<minlenght)
{
point=i;
minlenght=totalweight;
}
printf("\nthe shortest path is:1");
for(i=2;i<=number;i++)
printf("--->%d",vallagename[point][i]);
printf("--->1");
printf("\n the weight is %d",minlenght);
getch();
}
}
//结果可以发送到我的邮箱里:shengxp119@21cn.com
//谢谢啊