You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
0 0 10000 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
21
我们还是要记录点,然后判断时间,只不过我们需要特殊处理一下,subway的问题,还有了这个点的问题,导致一直WA,所以还是说快读题,快做题;
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
int cnt;
const int maxn=250;
const double v1=10*1000.0/60.0,v2=40*1000.0/60.0;
struct point{
int x,y;
}pos[maxn];
double graph[maxn][maxn];
double dis[maxn];
int vis[maxn];
void dijistra(){
for(int i=1;i<cnt;i++)
dis[i]=1e18;
memset(vis,0,sizeof(vis));
dis[1]=0;
for(int i=1;i<cnt;i++){
double min_dis=1e18;
int min_id;
for(int j=1;j<cnt;j++){
if(!vis[j]&&dis[j]<min_dis){
min_dis=dis[j];
min_id=j;
}
}
vis[min_id]=1;
for(int j=1;j<cnt;j++){
if(!vis[j]&&dis[j]>dis[min_id]+graph[min_id][j])
dis[j]=dis[min_id]+graph[min_id][j];
}
}
printf("%d\n",(int)(dis[2]+0.5));
}
double dis_be(int a,int b){
ll temp=(pos[a].x-pos[b].x)*(pos[a].x-pos[b].x)+(pos[a].y-pos[b].y)*(pos[a].y-pos[b].y);
return sqrt(1.0*temp);
}
int main()
{
for(int i=1;i<=maxn;i++)
for(int j=1;j<=maxn;j++)
graph[i][j]=1e18;
scanf("%d %d %d %d",&pos[1].x,&pos[1].y,&pos[2].x,&pos[2].y);
cnt=3;
while(~scanf("%d %d",&pos[cnt].x,&pos[cnt].y)){
cnt++;
while(~scanf("%d %d",&pos[cnt].x,&pos[cnt].y)){
if(pos[cnt].x==-1&&pos[cnt].y==-1)
break;
graph[cnt][cnt-1]=graph[cnt-1][cnt]=dis_be(cnt-1,cnt)/v2;
cnt++;
}
}
for(int i=1;i<cnt;i++){
for(int j=i;j<cnt;j++){
double dis=dis_be(i,j);
graph[i][j]=graph[j][i]=min(graph[i][j],dis/v1);
}
}
dijistra();
return 0;
}