Subway
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.
Input
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
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
Sample Input
0 0 10000 1000
0 200 5000 200 7000 200 -1 -1
2000 600 5000 600 10000 600 -1 -1
Sample Output
21
这个题就是说有若干地铁线,这个小孩去上学,步行是10km/h,坐地铁是40km/h,不过两站地铁在同一条线路上时才能通过地铁到达,但是在同一条线上也能走路到达,如果一个地铁线是类似于一个圆形的,那么这个地铁线的起点和终点之间走路就比坐地铁快了,这是一个需要注意的点,还有就是读入的方式,这个比较难想,思路就是同一地铁线上相邻的两个点建立40km/h的,所有点之间再建立一条步行的,那么咱们就能求Dijkstra求最短路了,下面具体看代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
using namespace std;
const int N = 500;
typedef pair<double , double> PII;
PII z[N];
double w[N][N];
int e[N];
bool st[N];
double dis(int i,int j) {
return sqrt((z[j].first-z[i].first)*(z[j].first-z[i].first)+(z[j].second-z[i].second)*(z[j].second-z[i].second));
}
int main() {
double sx,sy,ex,ey;
double v1 = 10000.0/60,v2 = 40000.0/60;
cin>>sx>>sy>>ex>>ey;
memset(st,0,sizeof st);
z[1] = make_pair(sx,sy);
z[2] = make_pair(ex,ey);
w[1][2] = w[2][1] = dis(1,2)/v1;
e[1] = e[2] = 0;
int idx = 3,l = 1;
int x,y;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if(i == j) w[i][j] = 0;
else w[i][j] = 1e19;
while(~scanf("%d%d",&x,&y)) {
if(x == -1 && y == -1) {
l++;
continue;
}
z[idx] = make_pair(1.0*x,1.0*y);
e[idx] = l;
if(e[idx] == e[idx-1]) w[idx][idx-1] = w[idx-1][idx] = dis(idx,idx-1)/v2;
idx++;
}
for(int i=3; i<idx; i++) {
w[1][i] = w[i][1] = dis(1,i)/v1;
w[2][i] = w[i][2] = dis(2,i)/v1;
for(int j=i+1; j<idx; j++) {
w[j][i] = w[i][j] = min(w[i][j],dis(i,j)/v1);
}
}
double dist[N];
for(int i=0; i<N; i++) dist[i] = 1e19;
dist[1] = 0;
for(int i=0; i<idx; i++) w[i][i] = 0;
for(int k=0; k<idx; k++) {
int re = -1;
for(int i=1; i<idx; i++) if(!st[i] && (re == -1 || dist[re] > dist[i])) re = i;
if(re == -1) break;
st[re] = true;
for(int i=0; i<idx; i++) if(!st[i] && dist[i] > dist[re] + w[re][i]) dist[i] = dist[re] + w[re][i];
}
printf("%.0lf\n",dist[2]);
return 0;
}