题目链接:http://poj.org/problem?id=2502

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

已知家和学校的坐标,并给出若干条地铁线,以及步行速度和地铁速度,求从家到学校最少需要多少时间
题目的难点在于输入和建图,由于输入的点最多200个,我们可以直接读入点的坐标,生成一个邻接矩阵,而同一地铁线上的各个站点,就可以在输入是直接存入邻接矩阵了,为了便于松弛,邻接矩阵中我们存入路径的用时,注意速度的单位是km/h,而路程的单位是m
建好图之后,就是一个简单的最短路问题了
 #include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstdio>
#include<queue>
#include<stack> using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = ;
const double wsp = * / ;
const double ssp = * / ; struct Node{
double x, y;
}node[MAXN]; struct ff{
int x, d;
ff(){}
ff( int a, double b ){ x = a; d = b; }
bool operator <( const ff & a )const{
return d > a.d;
}
}; int cnt;
double cost[MAXN][MAXN];
double dis[MAXN]; double gdis( int pre, int pos ){
double dx = node[pre].x - node[pos].x;
double dy = node[pre].y - node[pos].y;
return sqrt( dx * dx + dy * dy );
} void dij(){
for( int i = ; i < MAXN; i++ )
dis[i] = INF;
dis[] = ; priority_queue<ff> Q;
Q.push( ff( , dis[]) ); while( !Q.empty() ){
ff temp = Q.top(); Q.pop();
int x = temp.x;
if( temp.d > dis[x] ) continue;
for( int i = ; i < cnt; i++ ){
if( dis[i] > dis[x] + cost[x][i] ){
dis[i] = dis[x] + cost[x][i];
Q.push( ff( i, dis[i] ) );
}
}
}
} int main(){
ios::sync_with_stdio( false ); for( int i = ; i < MAXN; i++ )
for( int j = ; j < MAXN; j++ )
cost[i][j] = INF; cin >> node[].x >> node[].y >> node[].x >> node[].y;
cnt = ; while( cin >> node[cnt].x >> node[cnt].y ){
cnt++;
while( cin >> node[cnt].x >> node[cnt].y, !( node[cnt].x == - && node[cnt].y == - ) ){
cost[cnt][cnt - ] = cost[cnt - ][cnt] = gdis( cnt - , cnt ) / ssp;
cnt++;
}
} for( int i = ; i < cnt - ; i++ ){
cost[i][i] = ;
for( int j = i + ; j < cnt; j++ ){
cost[i][j] = cost[j][i] = min( cost[i][j], gdis( i, j ) / wsp );
}
} dij(); cout << int( dis[] + 0.5 );
}

最新文章

  1. MySQL 使用方法简单教程
  2. php微信支付(仅pc端扫码支付模式二)详细步骤.----仅适合第一次做微信开发的程序员
  3. update多表陷阱
  4. 配置并学习微信JS-SDK(2)—图片接口
  5. 今天在发布IIS站点的时候遇到了一些问题
  6. DirectFB 之 简介
  7. BZOJ_3210_花神的浇花集会_切比雪夫距离
  8. Codeforce Round #555 Div.3 D - N Problems During K Days
  9. Vue2开发大全
  10. (九)Delete an Index
  11. 那些年,很多人没看懂的Python内置函数
  12. react 组件列表
  13. docker中i的作用
  14. oracle 替换字符串中指定位置内容
  15. 前端-CSS-11-Z-index
  16. flex“深拷贝”
  17. 配置nginx为FastDFS的storage server提供http访问接口
  18. 在(Raspberry Pi)树莓派上安装NodeJS
  19. 使用dockerfile 构建springboot 的docker镜像
  20. FastReport.Net使用:[30]对话框使用

热门文章

  1. 100天搞定机器学习|Day13-14 SVM的实现
  2. MemCached的工具类。获取cached中的所有key
  3. java高并发系列 - 第21天:java中的CAS操作,java并发的基石
  4. Java——win10配置环境变量
  5. 喜大普奔 | 微信小程序支持PC端打开了
  6. mac 下 docker 镜像加速器
  7. java多线程基础(二)--sleep(),wait,()yield()和join()方法
  8. .netcore持续集成测试篇之搭建内存服务器进行集成测试一
  9. spring架构解析--入门一
  10. Multiple dex files define Lokhttp3/internal/wsWebSocketProtocol