CH6_6.C
上传用户:lgb298
上传日期:2013-03-22
资源大小:1025k
文件大小:1k
- #include <stdio.h>
- #define M 10
- #define MAX 100
- void shortpath_FLOYD(int cost[][M],int path[][M],int length[][M],int n)
- { int i,j,k,wm;
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- { length[i][j]=cost[i][j];
- if(i==j) path[i][j]=0;
- else if(length[i][j]<MAX)
- path[i][j]=i+1;
- else path[i][j]=0;
- }
- for(k=0;k<n;k++)
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- if(length[i][k]+length[k][j]<length[i][j])
- { length[i][j]=length[i][k]+length[k][j];
- path[i][j]=path[k][j];
- }
- }
- void main()
- {
- int i,j,n=3;
- static int cost[][M]={{0,4 ,11},
- {6,0 ,2},
- {3,MAX,0}};
- static int path[M][M],length[M][M];
- shortpath_FLOYD(cost,path,length,n);
- for(i=0;i<n;i++)
- { for(j=0;j<n;j++)
- printf("%-6d",length[i][j]);
- printf("n");
- }
- printf("n");
- for(i=0;i<n;i++)
- { for(j=0;j<n;j++)
- printf("%-6d",path[i][j]);
- printf("n");
- }
- }