传送门-----------------------------------subway
Subway
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11732 | Accepted: 3835 |
Description
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
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
Source
思路:spfa模板题练手。
需要注意的有几点:第一点是同一条地铁线上只有相邻的两站才可以加边如1.2、2.3
第二点是最后结果的四舍五入
第三点式步行加边的时候不能把地铁加边的边覆盖
CODE:
#include<iostream>
#include<math.h>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const double INF=1<<30;
const double v1=40000.0/60,v2=10000.0/60;
int cnt;
bool vis[205];
double d[205],g[205][205];
struct point{
int x,y;
}p[210];
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void spfa()
{
int f,i;
queue<int>q;
for(i = 0;i<=cnt;i++) d[i]=INF,vis[i]=0;
q.push(1);
vis[1]=1;
d[1]=0;
while(!q.empty())
{
f=q.front();
q.pop();
vis[f]=0;
for(i =1;i<cnt;i++)
{
if(g[f][i]>0&&d[i]>d[f]+g[f][i])
{
d[i]=d[f]+g[f][i];
if(!vis[i])
vis[i]=1,q.push(i);
}
}
}
}
int main()
{
int i,j,x,y,last;
for(i = 1;i<=205;i++)
for(j = 1;j<=205;j++)
g[i][j]=0;
scanf("%d%d%d%d",&p[1].x,&p[1].y,&p[2].x,&p[2].y);
last=cnt=3;
while(~scanf("%d%d",&x,&y))
{
if(x==-1&&y==-1)
{
last=cnt;
continue;
}
else
{
p[cnt].x=x;
p[cnt].y=y;
if(cnt!=last)
g[cnt][cnt-1]=g[cnt-1][cnt]=dis(p[cnt],p[cnt-1])/v1;
cnt++;
}
}
for(i = 1;i<cnt;i++)
for(j = 1;j<cnt;j++)
if(g[i][j]==0)g[i][j]=g[j][i]=dis(p[i],p[j])/v2;
spfa();
printf("%.d\n",int(d[2]+0.5));
return 0;
}