题目描述:
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 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
题目大意:
一个人从家要到学校去,途中有许多车站,所以有步行和做地铁两种方式,其速度分别是10km/h 和40km/h。输入的规则是第一行输入的是x1,y1,x2,y2,分别代表家的坐标和学校的坐标。以后输入的是车站的坐标,数目不超过200,相邻的两个站点可以坐地铁,其他的需要步行。问到达学校的最短时间是多少?
思路:
其实只是一个最短路径问题,只是建图比较麻烦,当然,很可能是题目没有看懂
还是想吐槽一下机翻
code:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=450;
const double inf=999999.99;
int cnt;
struct node{
int x,y;
}feng[maxn];
double a[maxn][maxn];
double d[maxn];
int visit[maxn];
double juli(int a,int b,int x,int y)
{
return sqrt((a-x)*(a-x)+(b-y)*(b-y));
}
void dijkstra(int start)
{
fill(d,d+cnt,inf);
memset(visit,0,sizeof(visit));
d[start]=0.0;
while(1)
{
int v=-1;
for(int i=0;i<cnt;i++)
{
if(visit[i]==0&&(v==-1||d[i]<d[v]))
v=i;
}
if(v==-1)break;
visit[v]=1;
for(int i=0;i<cnt;i++)
{
d[i]=min(d[i],d[v]+a[v][i]);
}
}
printf("%.0f\n",d[1]*60.0);
}
int main()
{
scanf("%d%d%d%d",&feng[0].x,&feng[0].y,&feng[1].x,&feng[1].y);
int x,y;
cnt=2;
int tra=2;
while(scanf("%d%d",&x,&y)!=EOF)
{
if(x==-1&&y==-1)
{
for(int i=tra;i<cnt-1;i++)
{
a[i][i+1]=a[i+1][i]=juli(feng[i].x,feng[i].y,feng[i+1].x,feng[i+1].y)/40000.0;
}
tra=cnt;//下一班的列车
continue;
}
else
{
feng[cnt].x=x;
feng[cnt++].y=y;
}
}
for(int i=0;i<cnt;i++)
{
for(int j=i+1;j<cnt;j++)
{
if(a[i][j]==0)
{
a[i][j]=a[j][i]=juli(feng[i].x,feng[i].y,feng[j].x,feng[j].y)/10000.0;
}
}
}
dijkstra(0);
return 0;
}