1722 Optimal Ride
1997 NOI National Competition
H City is a tourist attraction, and thousands of people come to visit it every year. To facilitate tourists, bus companies have set up bus stops and opened some one-way bus routes in various tourist attractions, hotels and other places. Each one-way bus route starts from a bus stop and passes through several bus stops in turn, finally arriving at the terminal bus stop.
A traveler recently visited H City. He would like to visit S Park. But if there is no bus from his hotel to reach S Park directly, he may have to take one bus for several stops and then come down to another bus on the same platform, so he can get to S Park several times later.
Now use integers 1, 2, and ____________. N. Number all bus stops in H City and agree that the passenger's hotel bus stop number is 1. The number of S Park Bus Station is N.
Write a program to help the passenger find an optimal ride plan, so that he can change at least the number of times during the ride from the hotel to S Park.
The input file is INPUT.TXT. The first line of the document has two digits M and N (1<=M<=100.1<N<=500), indicating that M one-way bus routes have been opened, with a total of N stations. Information on bus routes from line 1 to M is given in turn from line 2 to issue M. Line i+1 gives the information of the bus route i. From left to right, it gives the information of all station numbers on the bus line which are separated by a space between two adjacent station numbers.
The output file is OUTPUT.TXT, with only one line. If you can't take a bus from the hotel to S Park, then output "N0", otherwise output the minimum number of changes found by your program. The number of changes is 0, which means that you can arrive without changing trains.
3 7
6 7
4 7 3 6
2 1 3 5
2
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int a[510][510]; int map[510][510]; int cd[510]; int inf=0x7fffffff; int main() { for(int i=0;i<=501;i++) for(int j=0;j<=501;j++) map[i][j]=inf; //memset(map,0x7f,sizeof(map)); //for(int i=1;i<=) int n,m; //scanf("%d%d",&m,&n); cin>>m>>n; char p; for(int i=1;i<=m;i++) { int now=1; char p; while(1) { int bc=0;//Storage results p=getchar(); while(p>='0'&&p<='9') { bc=bc*10+p-'0'; p=getchar(); } if(bc!=0) { a[i][now]=bc; now++; } if(p==' ') continue; if(p=='\n') { if(a[i][now-1]==0) continue; cd[i]=now; now=1; break; } } cd[i]--; } for(int k=1;k<=m;k++) { for(int i=1;i<=cd[k];i++) { for(int j=i+1;j<=cd[k];j++) { if(i==j) map[a[k][i]][a[k][j]]=0; else map[a[k][i]][a[k][j]]=1; } } } for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(map[i][k]!=inf&&map[k][j]!=inf&&((map[i][k]+map[k][j]!=0))) if(map[i][j]>map[i][k]+map[k][j]) { map[i][j]=map[i][k]+map[k][j]; } } } } if(map[1][n]==inf) { cout<<"NO"; } else { cout<<map[1][n]-1; } return 0; }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int a[510][510]; int map[510][510]; int cd[510]; int inf=0x7fffffff; int main() { for(int i=0;i<=501;i++) for(int j=0;j<=501;j++) map[i][j]=inf; //memset(map,0x7f,sizeof(map)); //for(int i=1;i<=) int n,m; //scanf("%d%d",&m,&n); cin>>m>>n; char p; for(int i=1;i<=m;i++) { int now=1; char p; while(1) { int bc=0;//Storage results p=getchar(); while(p>='0'&&p<='9') { bc=bc*10+p-'0'; p=getchar(); } if(bc!=0) { a[i][now]=bc; now++; } if(p==' ') continue; if(p=='\n') { if(a[i][now-1]==0) continue; cd[i]=now; now=1; break; } } cd[i]--; } for(int k=1;k<=m;k++) { for(int i=1;i<=cd[k];i++) { for(int j=i+1;j<=cd[k];j++) { if(i==j) map[a[k][i]][a[k][j]]=0; else map[a[k][i]][a[k][j]]=1; } } } for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(map[i][k]!=inf&&map[k][j]!=inf&&((map[i][k]+map[k][j]!=0))) if(map[i][j]>map[i][k]+map[k][j]) { map[i][j]=map[i][k]+map[k][j]; } } } } if(map[1][n]==inf) { cout<<"NO"; } else { cout<<map[1][n]-1; } return 0; }