📄 3.c
字号:
#include <stdio.h>
#include<stdlib.h>
#define STOPNUM 3957
#define MAXTO 1400
FILE *fp1, *fp2;
struct stop{
int reach[MAXTO][4]; //车号,站点,站数,价格
int num;
int flag;
};
void find(int start, int end, struct stop *busstop[]){
int i, j, tmp;
int number=1;
int fstop[STOPNUM][5]; //站次,车号,上站,站数,价格
for(i=0;i<STOPNUM;i++){
busstop[i]->flag=0;
for(j=0;j<3;j++)
fstop[i][j]=0;
for(j=3;j<5;j++)
fstop[i][j]=999;
}
fstop[start-1][0]=1;
fstop[start-1][3]=0;
fstop[start-1][4]=0;
while(fstop[end-1][0]==0){
for(i=0;i<STOPNUM;i++){
if(fstop[i][0]==number){
for(j=0;j<(busstop[i]->num);j++){
tmp=busstop[i]->reach[j][1];
if(fstop[tmp-1][0]==0)
fstop[tmp-1][0]=number+1;
else ;
if((busstop[i]->reach[j][2]+fstop[i][3])<fstop[tmp-1][3]){
fstop[tmp-1][3]=busstop[i]->reach[j][2]+fstop[i][3];
fstop[tmp-1][4]=busstop[i]->reach[j][3]+fstop[i][4];
fstop[tmp-1][1]=busstop[i]->reach[j][0];
fstop[tmp-1][2]=i+1;
}
else if((busstop[i]->reach[j][2]+fstop[i][3])==fstop[tmp-1][3]){
if((busstop[i]->reach[j][3]+fstop[i][4])<=fstop[tmp-1][4]){
fstop[tmp-1][3]=busstop[i]->reach[j][2]+fstop[i][3];
fstop[tmp-1][4]=busstop[i]->reach[j][3]+fstop[i][4];
fstop[tmp-1][1]=busstop[i]->reach[j][0];
fstop[tmp-1][2]=i+1;
}
else ;
}
else ;
}
}
}
number++;
}
fp2=fopen("path.txt","w");
for(i=0;i<STOPNUM;i++){
for(j=0;j<5;j++)
fprintf(fp2,"%d\t", fstop[i][j]);
fprintf(fp2,"\n");
}
}
int main(){
int t1[1043][100];
int bus[1040][100];
int i, j, k, temp;
fp1=fopen("bus.txt","r");
for(i=0;i<1043;i++)
for(j=0;j<100;j++)
t1[i][j]=0;
for(i=-1;i<1040;i++){
for(j=0;j<100;j++){
temp=0;
fscanf(fp1,"%d", &temp);
if(temp>9000){
i++;
j=0;
if(i%2==0){
t1[i][j]=temp;
t1[i+1][j]=temp;
}
else if(t1[i-1][2]==6000){
t1[i][2]=t1[i-1][2];
for(k=3;k<100;k++)
t1[i][k]=t1[i-1][102-k];
i++;
j=0;
t1[i][j]=temp;
t1[i+1][j]=temp;
}
else {
t1[i][2]=t1[i-1][2];
for(k=3;k<100;k++)
t1[i][k]=t1[i-1][k];
i++;
j=0;
t1[i][j]=temp;
t1[i+1][j]=temp;
}
}
else if(temp>=7000 && temp<8000){
t1[i][j]=temp;
t1[i+1][j]=temp;
}
else if(temp>=6000 && temp<7000){
if(j==2) t1[i][j]=temp;
else {
i++;
j=2;
t1[i][j]=temp;
t1[i+1][j]=temp;
}
}
else t1[i][j]=temp;
}
}
for(i=0;i<1040;i++){
for(j=0;j<100;j++)
bus[i][j]=t1[i][j];
}
fp2=fopen("Output.txt","w");
for(i=0;i<1040;i++){
for(j=0;j<100;j++)
fprintf(fp2,"%d\t", bus[i][j]);
fprintf(fp2,"\n");
}
struct stop *busstop[STOPNUM];
for(i=0; i<STOPNUM; i++)
busstop[i]=malloc(sizeof(struct stop));
for(i=0;i<STOPNUM;i++){
for(j=0;j<MAXTO;j++){
for(k=0;k<4;k++)
busstop[i]->reach[j][k]=0;
}
busstop[i]->num=0;
busstop[i]->flag=0;
}
for(i=0; i<1040; i++){
for(j=3; j<100; j++){
temp = bus[i][j];
if(temp){
k=j;
while(k++<100 && bus[i][k]!=0){
busstop[temp-1]->reach[busstop[temp-1]->num][0]=i/2+1;
busstop[temp-1]->reach[busstop[temp-1]->num][1]=bus[i][k];
busstop[temp-1]->reach[busstop[temp-1]->num][2]=k-j;
busstop[temp-1]->reach[busstop[temp-1]->num][3]
=(bus[i][1]==7000)?1:(((k-j)<=20)?1:(((k-j)<=40)?2:3));
(busstop[temp-1]->num)++;
}
}
else;
}
}
temp=0;
for(i=0;i<STOPNUM;i++)
temp=(busstop[i]->num > temp)?busstop[i]->num:temp;
printf("%d",temp);
fp2=fopen("node.txt","w");
for(i=0;i<STOPNUM;i++){
fprintf(fp2,"%d\t%d\n", i+1, busstop[i]->num);
for(j=0;j<MAXTO;j++){
if(busstop[i]->reach[j][1]==0)
break;
for(k=0;k<4;k++)
fprintf(fp2,"%d\t", busstop[i]->reach[j][k]);
fprintf(fp2,"\n");
}
fprintf(fp2,"\n");
}
find(3359, 1828, &busstop[0]);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -