Subway

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14634   Accepted: 4718

题目链接: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

题意:

在二维平面上,给出起点和终点的坐标。另外还有多条地铁线路,给出每条线路有哪些站。

如果坐地铁的速度是40km/h,而走路的速度为10km/h,现在问从起点到终点最少花费多少时间。

题解:

直接模拟建边就行了,注意要换算一下单位,因为最后问的是分钟。

有个需要注意的地方就是地铁站之间也可以走路。另外结果四舍五入!!

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int x,y,X,Y,cnt,num,s,t;
map <ll,map<ll,int> > p;
double d[N];
int vis[N],head[N];
struct node{
int x,y;
};
vector <node> vec[N];
double dis(int X1,int X2,int Y1,int Y2){
return sqrt((double)(X1-X2)*(X1-X2)+(double)(Y1-Y2)*(Y1-Y2));
}
double T(double d,int op){
if(op==) return *d/1000.0;
else return *d/4000.0;
}
struct Edge{
int u,v,next ;
double w;
}e[N*N<<];
int tot;
struct Node{
double d;
int u;
bool operator < (const Node &A)const{
return d>A.d;
}
};
void adde(int u,int v,double w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
void Dijkstra(int s){
priority_queue <Node> q;
for(int i=;i<=t;i++) d[i]=INF;
memset(vis,,sizeof(vis));d[s]=;
Node now;
now.d=;now.u=s;
q.push(now);
while(!q.empty()){
Node cur = q.top();q.pop();
int u=cur.u;
if(vis[u]) continue ;
vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
now.d=d[v];now.u=v;
q.push(now);
}
}
}
}
int main(){
cin>>x>>y>>X>>Y;
int xx,yy;
memset(head,-,sizeof(head));
while(scanf("%d",&xx)!=EOF){
scanf("%d",&yy);
cnt++;
node cur;
cur.x=xx;cur.y=yy;
vec[cnt].push_back(cur);
while(){
scanf("%d%d",&xx,&yy);
if(xx==- && yy==-) break ;
cur.x=xx;cur.y=yy;
vec[cnt].push_back(cur);
}
}
s=;t=;
for(int i=;i<=cnt;i++){
for(int j=;j<vec[i].size();j++){
node cur = vec[i][j];
if(p[cur.x][cur.y]==) p[cur.x][cur.y]=++num;
adde(s,p[cur.x][cur.y],T(dis(,cur.x,,cur.y),));
adde(p[cur.x][cur.y],t,T(dis(cur.x,X,cur.y,Y),));
}
}
adde(s,t,T(dis(x,X,y,Y),));
for(int i1=;i1<=cnt;i1++){
for(int j1=;j1<vec[i1].size();j1++){
int len1 = vec[i1].size();
node cur1 = vec[i1][j1];
if(j1>){
node cur2 = vec[i1][j1-];
adde(p[cur1.x][cur1.y],p[cur2.x][cur2.y],T(dis(cur1.x,cur2.x,cur1.y,cur2.y),));
}
if(j1<len1-){
node cur2 = vec[i1][j1+];
adde(p[cur1.x][cur1.y],p[cur2.x][cur2.y],T(dis(cur1.x,cur2.x,cur1.y,cur2.y),));
}
for(int i2=;i2<=cnt;i2++){
for(int j2=;j2<vec[i2].size();j2++){
node cur2 = vec[i2][j2];
adde(p[cur1.x][cur1.y],p[cur2.x][cur2.y],T(dis(cur1.x,cur2.x,cur1.y,cur2.y),));
}
}
}
}
Dijkstra(s);
cout<<(int)(d[t]+0.5);
return ;
}

POJ2502:Subway(最短路)的更多相关文章

  1. POJ-2502 Subway( 最短路 )

    题目链接:http://poj.org/problem?id=2502 Description You have just moved from a quiet Waterloo neighbourh ...

  2. L - Subway(最短路spfa)

    L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...

  3. (poj 2502) Subway 最短路

    题目链接: 题意:在一个城市里有许多地铁,现在你知道每条地铁的起点  终点与停站点的坐标,知道我们的起始坐标与终点坐标,问加上走路最快到达终点的时间是多少? 方法:求出任意两点的车速时间与步行时间,再 ...

  4. POJ 2502 Subway ( 最短路 && 最短路建图 )

    题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...

  5. poj2502 Subway

    思路: 需要注意的地方:一条地铁线路并不一定和样例描述的那样是直的:同一条线路上的两个站点步行可能更快. 实现: #include <iostream> #include <cstd ...

  6. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  7. POJ 2502 Subway (最短路)

    Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...

  8. POJ-2502(Dijikstra应用+最短路)

    Subway POJ-2502 这里除了直接相连的地铁站,其他图上所有的点都要连线,这里是走路的速度. 记住最后的结果需要四舍五入,否则出错. #include<iostream> #in ...

  9. poj2502 最短路

    //Accepted 504 KB 16 ms //spfa最短路 //把n个地铁站作为n个顶点,边权为从一个站到另一个站的时间 //注意:地铁在相邻的两站之间是直线行驶,但其他的就不是了 #incl ...

随机推荐

  1. 学会了 python 的pip方法安装第三方库

    超级开心啊!!!!!!!!!!!!! win10 打开cmd Installing with get-pip.py To install pip, securely download get-pip. ...

  2. UVA10474 Where is the Marble?【排序】

    参考:https://blog.csdn.net/q547550831/article/details/51326321 #include <iostream> #include < ...

  3. shell重温---基础篇(流程控制&if判断&for&while&循环操作)

        和Java.PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法): <?php if (isset($_GET["q"])) { search( ...

  4. vs2015-Cordova开发安卓应用环境搭建

    之前看到过用html5+css+js就可以开发跨平台的应用,然后发现vs2015里就有个Cordova项目所以就想试试,但并不是这么顺利.刚开始对安卓环境一点也不了解,就到处百度搜索.终于成功了. 首 ...

  5. 从浏览器或者Webview 中唤醒APP

    本文来自网易云社区 作者:刘新奇 移动互联时代,很多互联网服务都会同时具备网站以及移动客户端,很多人认为APP的能帮助建立更稳固的用户关系,于是经常会接到各种从浏览器.webview中唤醒APP的需求 ...

  6. 【jQuery】 选择器

    [jQuery] 选择器 资料: w3school  http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp 1. 标签选择器 : $(& ...

  7. (原)一段看似美丽的for循环,背后又隐藏着什么

    之前很长一段时间,潜心修炼汇编,专门装了一个dos7,慢慢玩到win32汇编,再到linux的AT&A汇编,尝试写mbr的时候期间好几次把centos弄的开不了机,又莫名其妙的修好了,如今最大 ...

  8. Clean Code 《代码整洁之道》前四章读书笔记

    第一章: 整洁的代码只做好一件事   减少重复代码   提高表达力   提早构建简单抽象   让营地比你来时更干净   第二章:有意义的命名 名副其实:如果名称需要注释来补充,就不算是名副其实.   ...

  9. Linux-OpenSUSE折腾-1(Qt安装,Chrome安装)

    先上图,大蜥蜴还是不错的,偶然看到了大蜥蜴这个系统,我就觉得又可以折腾几天了,先上图 OpenSUSE有一个入门介绍的网站写的相当不错,感兴趣的可以连接过去:https://lug.ustc.edu. ...

  10. zabbix 一些问题随记

    1. zabbix运行不了,显示被锁,去检查日志中的报错 2. 配置界面,连接不到数据库,检查server配置文件,mysql授权命令要准确,重启 3. 显示没有php文件,下载即可,或者修改网页访问 ...