POJ 2502 Subway

Subway
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4928   Accepted: 1602

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.

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

 
 
 
 
人走路的速度是10km/h,地铁的速度是40km/h
题目给出一个起点,一个终点,
以及几条地铁线路运行的站点。
 
题目给的点的做坐标单位是m
把速度统一为m/min
 
答案输出从起点到终点的时间,到最近的分钟数。
 
10km/h= 10000/60 m/min
40km/h= 40000/60 m/min
 
所有的点直接以步行的速度建边。
地铁线路两站相邻的以地铁速度建边
//============================================================================
// Name : POJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN=;
struct Node
{
double x,y;
}node[MAXN];
double dis(Node a,Node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
const double INF=1e30;
bool vis[MAXN];
double dist[MAXN];
double cost[MAXN][MAXN];
void Dijkstra(int n,int start)
{
for(int i=;i<=n;i++)
{
dist[i]=INF;
vis[i]=false;
}
dist[start]=;
for(int j=;j<n;j++)
{
int k=-;
double Min=INF;
for(int i=;i<=n;i++)
if(!vis[i]&&dist[i]<Min)
{
Min=dist[i];
k=i;
}
if(k==-)break;
vis[k]=true;
for(int i=;i<=n;i++)
if(!vis[i]&&dist[k]+cost[k][i]<dist[i])
dist[i]=dist[k]+cost[k][i];
}
} int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
double v1=10000.0/;
double v2=40000.0/;
while(scanf("%lf%lf%lf%lf",&node[].x,&node[].y,&node[].x,&node[].y)==)
{
int n=;
int cnt1=;
int x,y;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
if(i==j)cost[i][j]=;
else cost[i][j]=INF;
}
while(scanf("%d%d",&x,&y)==)
{
if(x==-&&y==-)
{
cnt1=n+;
continue;
}
n++;
node[n].x=x;
node[n].y=y;
if(n!=cnt1)cost[n][n-]=cost[n-][n]=min(cost[n][n-],dis(node[n],node[n-])/v2);
//只有相邻的站点能到
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cost[i][j]=min(cost[i][j],dis(node[i],node[j])/v1);
Dijkstra(n,);
// int ans=(int)round(dist[2]);
// cout<<ans<<endl;
printf("%.0lf\n",(dist[]));
}
return ;
}
 
 
 
 
 
 
上一篇:OpenStack swift安装


下一篇:hdu2653之BFS