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
题目大意:首先给出两个点的坐标,代表家和学校,有多条地铁线路,分别给出站点的坐标,设地铁都是走直线切地铁以40km/h运行,同时还可以在任意站间步行,步行速度为10km/h
求从家到学校的最短时间花费
解题思路:首先题目并没有给出有多少条地铁线路,所以应用while循环输入,在windows里,文件结束的标识符是ctrl+z
其次难点就是建图了,由于地铁只能一站一站的跑,所以设输入的点为点n,只要计算并存入图中n-1到n的时间即可,建图详见代码
还有一个问题是memset,不能用memset给double类型的数组直接赋值
用spfa计算最短路
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
double x,y;
} p[305];
struct edge
{
int from,to,next;
double w;
} e[90000];
double v1=10000.0/60.0;
double v2=40000.0/60.0;
int head[305],n;
double dis[305];
bool book[305][305],vis[305];
double dist(node a,node b)
{
return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int T;
void addedge(int u,int v,double w)
{
e[T].from=u;
e[T].to=v;
e[T].w=w;
e[T].next=head[u];
head[u]=T++;
}
void spfa()
{
int i;
memset(vis,0,sizeof(vis));
for(i=1; i<=n; i++)
{
dis[i]=inf;
}
stack<int> q;
q.push(1);
vis[1]=1;
dis[1]=0;
int u;
while(!q.empty())
{
u=q.top();
q.pop();
vis[u]=0;
for(i=head[u]; i!=-1; i=e[i].next)
{
if(dis[e[i].to]>dis[e[i].from]+e[i].w)
{
dis[e[i].to]=dis[e[i].from]+e[i].w;
if(vis[e[i].to]==0)
{
q.push(e[i].to);
vis[e[i].to]=1;
}
}
}
}
}
int main()
{
scanf("%lf %lf %lf %lf",&p[1].x,&p[1].y,&p[2].x,&p[2].y);
int flag=1;
n=2;
T=0;
double a,b;
memset(head,-1,sizeof(head));
memset(book,0,sizeof(book));
while(scanf("%lf %lf",&a,&b)!=EOF)
{
if(a==-1&&b==-1)
{
flag=1;
continue ;
}
n++;
p[n].x=a;
p[n].y=b;
if(flag) flag=0;
else
addedge(n,n-1,dist(p[n],p[n-1])/v2),addedge(n-1,n,dist(p[n],p[n-1])/v2),book[n][n-1]=book[n-1][n]=1;
}
int i,j;
for(i=1; i<=n; i++)
{
for(j=i+1; j<=n; j++)
{
if(book[i][j]==0)
{
addedge(i,j,dist(p[i],p[j])/v1);
addedge(j,i,dist(p[i],p[j])/v1);
}
}
}
spfa();
long long int d=dis[2]+0.5;
printf("%lld\n",d);
return 0;
}