POJ 2502 Subway (最短路)
Subway
题目链接:
http://acm.hust.edu.cn/vjudge/contest/122685#problem/L
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
Hint
##题意:
已知地铁的速度是40km/h, 步行速度是10km/h.
给出若干条地铁线路.
问从起点到终点的最快路径.
##题解:
很显然是建边然后求最短路.
需要注意的是:
1. 地铁线不一定是直线,所以同一地铁线上不相邻的点之间不能直接建"地铁边".
2. 还是由于不是直线,所以同一地铁线上不相邻的点之间步行可能要比地铁快.(因为步行是直线,地铁可能是曲线).
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 350
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
double value[maxn][maxn];
double dis[maxn];
bool vis[maxn];
int pre[maxn];
void dijkstra(int s) {
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
for(int i=0; i<=n; i++) dis[i] = inf;
dis[s] = 0;
for(int i=0; i<n; i++) {
int p;
double mindis = inf;
for(int j=0; j<n; j++) {
if(!vis[j] && dis[j]<mindis)
mindis = dis[p=j];
}
vis[p] = 1;
for(int j=0; j<n; j++) {
if(dis[j] > dis[p]+value[p][j]) {
dis[j] = dis[p] + value[p][j];
pre[j] = p;
}
}
}
}
typedef pair<int,int> pii;
vector stop;
int first[maxn], last[maxn], lines;
int cnt;
int main(int argc, char const *argv[])
{
//IN;
for(int i=0; i<maxn; i++) for(int j=0; j<maxn; j++) value[i][j] = inf;
stop.clear(); lines = 1;
int a,b; cnt = 2; first[0]=0; last[0]=1;
cin >> a >> b; stop.push_back(make_pair(a,b));
cin >> a >> b; stop.push_back(make_pair(a,b));
value[0][1] = value[1][0] = 4.0*sqrt((double)((stop[0].first-stop[1].first)*(stop[0].first-stop[1].first) + (stop[0].second-stop[1].second)*(stop[0].second-stop[1].second)));
first[lines] = cnt;
while(scanf("%d %d",&a,&b) != EOF) {
if(a==-1 && b==-1) {
last[lines] = cnt-1;
lines++;
first[lines] = cnt;
continue;
}
stop.push_back(make_pair(a,b));
cnt++;
}
for(int l=1; l<lines; l++) {
for(int i=first[l]; i<last[l]; i++) {
int j = i + 1;
double tmp = sqrt(double((stop[i].first-stop[j].first)*(stop[i].first-stop[j].first) + (stop[i].second-stop[j].second)*(stop[i].second-stop[j].second)));
if(tmp < value[i][j]) value[i][j] = tmp;
if(tmp < value[j][i]) value[j][i] = tmp;
}
for(int i=first[l]; i<=last[l]; i++) {
for(int j=0; j<i; j++) {
double tmp = sqrt(double((stop[i].first-stop[j].first)*(stop[i].first-stop[j].first) + (stop[i].second-stop[j].second)*(stop[i].second-stop[j].second)));
tmp *= 4.0;
if(tmp < value[i][j]) value[i][j] = tmp;
if(tmp < value[j][i]) value[j][i] = tmp;
}
}
}
n = cnt;
dijkstra(0);
double ans = (dis[1]*3.0/2000.0);
printf("%.0f\n", ans);
return 0;
}
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 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 ...
随机推荐
- Hibernate4.2.2使用Annotation配置
1.在hibernate官网下载hibernate-release-4.2.2.Final.zip并解压 2.新建一个java project工程(20130619_Hibernate4.2.2_An ...
- Android 开机动画启动过程详解
Android 开机会出现3个画面: 1. Linux 系统启动,出现Linux小企鹅画面(reboot)(Android 1.5及以上版本已经取消加载图片): 2. Android平台启动初始化,出 ...
- 安卓学习之--如何关闭所有的activity
根据Activity的声明周期 方法1 我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在A窗口打开B窗口时在Intent中直接加入标志 Intent ...
- BZOJ_1028_[JSOI2007]_麻将_(模拟+贪心)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1028 同一种花色的牌,序数为\(1,2,...,n\).定义"和了"为手上 ...
- asp.net读excle的数据类型不统一取出空值问题
如果表格里某列全是数字或是字符没问题,但如果混合了全是数字和部分字符就会有部分读取为空连接EXCEL方式如下 string strConn = "Provider=Microsoft.Jet ...
- [转] Jquery滚动加载
原文地址:http://hi.baidu.com/vipxiaofan/item/9eb927b795671f77254b0985 另外一个asp.net的例子:http://blog.csdn.ne ...
- LT1619EMS8 锂电池 升压电路分析
LT1619EMS8 锂电池 升压电路分析 本文主要是分析LT1619EMSB锂电池升压芯片电路,知道其大致是怎么工作的,其中的一些电阻该如何配置. 2016-1-23 深圳 南山平山村 曾剑锋 一. ...
- android 应用页面与数据申请逻辑剥离;
1.页面与数据申请剥离,数据申请框架可以灵活更换,解耦合: 2.对应页面的数据申请类中,将返回数据解析剥离,灵活更换数据返回及对应解析: 二.模块划分: 1.一些通用的工具类,可以考虑迁移到com.c ...
- 用ioctl获取无线网络信息 /usr//include/linux/wireless.h
1.UNIX Network Programming环境搭建 Unix NetWork Programming――环境搭建(解决unp.h等源码编译问题) http://blog.csdn.net/a ...
- windows主线程等待子线程退出卡死问题
在windows下调用_beginthread创建子线程并获得子线程id(函数返回值),如果子线程很快退出,在主线程中调用WaitForSingleObject等待该线程id退出,会导致主线程卡死.需 ...