#include <stdio.h>
#define N 5
#define I 9999.9
#define true 1
#define false 0
struct three{
float S;
float time;
float money;}allcity[N][N];
final[N];
float test[N];
main()
{
char ch,zh;
int score,v,w,i,k,p,min;
float city[N][N],a1,a2,a3; FILE *fp;
fp=fopen("DATA","wb");
printf("input:");
for (i=0;i<N;i++)
for (k=0;k<N;k++)
{printf("allcity[%d][%d]",i,k);
scanf("%f %f %f",&a1,&a2,&a3);
allcity[i][k].S=a1; allcity[i][k].time=a2; allcity[i][k].money=a3;}
i=fwrite(allcity,sizeof(allcity),1,fp); printf("_%d_%d_",ferror(fp),i);
fclose(fp);
printf("byebye!");
if (fp=fopen("DATA","rb")==NULL)
{printf("can't open the file"); exit(0); }
i=fread(allcity,sizeof(allcity),1,fp); printf("_%d_",ferror(fp));
printf("please input beginning city:\n");
scanf("%c",&ch);
score=ch-'A';
printf("please input searching type:\n");
printf("1.distance\n");
printf("2.time\n");
printf("3.money\n");
scanf("%d",&p);
switch (p)
{
case 1: {for (i=0;i<N;i++) {for(k=0;k<N;k++) city[i][k]=allcity[i][k].S;}
break;}
case 2: {for (i=0;i<N;i++) {for(k=0;k<N;k++) city[i][k]=allcity[i][k].time;}break;}
case 3: {for (i=0;i<N;i++) {for(k=0;k<N;k++) city[i][k]=allcity[i][k].money;}break;}
}
fclose(fp);
for(i=0;i<N;i++)
{test[i]=city[score][i]; final[i]=false;}
test[score]=0;
final[score]=true;
for (v=0;v<N-1;v++)
{min=I;
for(w=0;w<N;w++)
if (test[w]<min&&(!final[w]))
{min=city[score][w];i=w;}
final[i]=true;
for(w=0;w<N;w++)
if(test[i]+city[i][w]<test[w]&&(!final[w]))
test[w]=test[i]+city[i][w];
}
getchar();
printf("please input ending city:");
scanf("%c",&zh);
switch (p)
{
case 1:{printf("the shortest distance:%f",test[zh-'A']);break;}
case 2:{printf("the shortest time:%f",test[zh-'A']);break;}
case 3:{printf("the least money:%f",test[zh-'A']);break;}
}
}