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。(当然,你在两个点之间走,肯定是勾股距离最短)。再给出N条地铁线路,每条给出不小于2个的坐标,表示这些坐标构成一条线路。在地铁轨道中,速度是40km/h。我们认为无论何时你到某个站,都恰好有一辆车来。求起点到终点的最小时间。
难得是构图,把最时间当成最短路来求,套Dijkstra模板即可.
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxx=501;
const int inf=0x3f3f3f3f;
double map[maxx][maxx],dis[maxx];
int book[maxx];
struct node
{
int x;
int y;
} q[maxx];
void init()
{
for(int i=1; i<maxx; i++)
for(int j=1; j<maxx; j++)
if(i==j)map[i][j]=0;
else map[i][j]=inf;
}
double Dis(int i,int j)
{
return sqrt(1.0*(q[i].x-q[j].x)*(q[i].x-q[j].x)+(q[i].y-q[j].y)*(q[i].y-q[j].y));
}
double Distra(int st,int n)
{
for(int i=1; i<=n; i++)
dis[i]=map[st][i];
memset(book,0,sizeof(book));
for(int i=1; i<n; i++)
{
double mini=inf;
int k=-1;
for(int j=1; j<=n; j++)
if(!book[j]&&dis[j]<mini)
{
mini=dis[j];
k=j;
}
if(k==-1)
break;
book[k]=1;
for(int j=1; j<=n; j++)
if(!book[j]&&dis[j]>dis[k]+map[k][j])
dis[j]=dis[k]+map[k][j];
}
return dis[2];
}
int main()
{
double v1=40000.0/60.0;
double v2=10000.0/60.0;
init();
scanf("%d%d%d%d",&q[1].x,&q[1].y,&q[2].x,&q[2].y);
int n=3,last=3;
while(scanf("%d%d",&q[n].x,&q[n].y)!=EOF)
{
if(q[n].x==-1&&q[n].y==-1)
{
last=n;
continue;
}
if(last!=n)
map[n-1][n]=map[n][n-1]=Dis(n-1,n)/v1;//车的路径 ,
n++;
}
n--;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
map[i][j]=min(map[i][j],Dis(i,j)/v2);//人的路径
printf("%.0f\n",Distra(1,n));
return 0;
}