POJ-2502 Subway( 最短路 )
题目链接:http://poj.org/problem?id=2502
Description
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
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 );
}
POJ-2502 Subway( 最短路 )的更多相关文章
- POJ 2502 Subway ( 最短路 && 最短路建图 )
题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...
- POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)
POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离) Description You have just moved from a ...
- POJ 2502 - Subway Dijkstra堆优化试水
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...
- POJ 2502 Subway (最短路)
Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...
- POJ 2502 Subway(迪杰斯特拉)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6692 Accepted: 2177 Descriptio ...
- POJ 2502 Subway (Dijkstra 最短+建设规划)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6689 Accepted: 2176 Descriptio ...
- POJ 2502 Subway
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4928 Accepted: 1602 Descriptio ...
- (简单) POJ 2502 Subway,Dijkstra。
Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...
- Dijkstra+计算几何 POJ 2502 Subway
题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...
- POJ 2502 Subway dij
这个题的输入输出注意一下就好 #include<cstdio> #include<cstring> #include<queue> #include<cstd ...
随机推荐
- ld: warning: directory not found for option ''
iOS开发中经常遇到这样的警告,如图所示: 原因是存在未用到的目录. 解决方法:选择Build Settings,找到Search Paths中的Library Search Paths,如下图 删除 ...
- Java悲观锁Pessimistic-Lock常用实现场景
1:商品库存秒杀采用悲观锁Pessimistic-Lock主要好处是安全,充分利用了数据库的性能来做的一种锁机制. 悲观锁的实现: (1)环境:mysql + jdbctemplate (2)商品表g ...
- MySQL操作命令梳理(2)
一.表操作 在mysql运维操作中会经常使用到alter这个修改表的命令,alter tables允许修改一个现有表的结构,比如增加或删除列.创造或消去索引.改变现有列的类型.或重新命名列或表本身,也 ...
- 【错误】【vscode】"'#' not expected here"
今天使用vscode发现完整的代码报错了,但依然可以运行
- Go中的日志及第三方日志包logrus
有别的语言使用基础的同学工作中都会接触到日志的使用,Go中自然也有log相关的实现.Go log模块主要提供了3类接口,分别是 "Print .Panic .Fatal ",对每一 ...
- 做梦也没有想到:Windows 上的 .NET Core 表现更糟糕
昨天晚上 18:15 左右我们发布了跑在 Windows 上 .NET Core 博客系统,本想与 .NET Framework 版进行同“窗”的较量,结果刚发布上线就发现 CPU 占用异常高,发布不 ...
- 链表:如何实现LRU缓存淘汰算法?
缓存淘汰策略: FIFO:先入先出策略 LFU:最少使用策略 LRU:最近最少使用策略 链表的数据结构: 可以看到,数组需要连续的内存空间,当内存空间充足但不连续时,也会申请失败触发GC,链表则可 ...
- openldap介绍和使用
openldap介绍和使用 为什么会有本文? 早期,公司是没有统一认证这个东西的,所以各自玩各自的.于是, confluence一个用户体系,gitlab一个用户体系,Jenkins一个用户体系等等, ...
- 7.18 collection time random os sys 序列化 subprocess 等模块
collection模块 namedtuple 具名元组(重要) 应用场景1 # 具名元组 # 想表示坐标点x为1 y为2 z为5的坐标 from collections import namedtu ...
- gcd 和 同余方程(Exgcd)
求关于x的同余方程 ax≡1(mod b) 的最小正整数解. 对于 100%的数据,2≤a,b≤2*109. NOIP 2012 提高组 第二天 第一题 (只看Exgcd的自行跳过这段文字) 先撇开扩 ...