POJ-2502(Dijikstra应用+最短路)
Subway
POJ-2502
- 这里除了直接相连的地铁站,其他图上所有的点都要连线,这里是走路的速度。
- 记住最后的结果需要四舍五入,否则出错。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef pair<int,int> p;
const int INF=0x3f3f3f3f;
int sx,sy,ex,ey;
struct edge{
int to;
double cost;
int next;
};
struct node{
double dis;
int to;
node(){}
node(int a,int b):dis(a),to(b){}
bool operator<(const node& t)const{
return dis>t.dis;
}
};
edge ma[1500005];
int head[220];
int top;//指向头结点
double d[220];
int tn;//结点个数
int n=220;//最大结点数
map<pair<int,int>,int> mmap;
map<int,pair<int,int> >mmap1;
void addedge(int a,int b,double c){
ma[top].to=b;
ma[top].cost=c;
ma[top].next=head[a];
head[a]=top;
top++;
}
void dijikstra(int s){
priority_queue<node> que;
for(int i=0;i<tn;i++){
d[i]=INF;
}
d[s]=0;
que.push(node(0,s));
while(!que.empty()){
node temp=que.top();
que.pop();
int v=temp.to;
if(d[v]<temp.dis)
continue;
for(int h=head[v];h!=-1;h=ma[h].next){
edge e=ma[h];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(node(d[e.to],e.to));
}
}
}
}
bool isnode(int x,int y){
if(!mmap.count(p(x,y))){
mmap1[tn]=p(x,y);
mmap[p(x,y)]=tn++;
return true;
}else return false;
}
double G[220][220];
int main(){
memset(head,-1,sizeof(head));
top=0;
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
mmap1[tn]=p(sx,sy);
mmap[p(sx,sy)]=tn++;//0
mmap1[tn]=p(ex,ey);
mmap[p(ex,ey)]=tn++;//1
int x,y;
while(scanf("%d%d",&x,&y)!=EOF){
int nx,ny;
if(!mmap.count(p(x,y))){
mmap1[tn]=p(x,y);
mmap[p(x,y)]=tn++;
}
while(scanf("%d%d",&nx,&ny)!=EOF&&(nx!=-1||ny!=-1)){
if(!mmap.count(p(nx,ny))){
mmap1[tn]=p(nx,ny);
mmap[p(nx,ny)]=tn++;
}
double distance=sqrt((nx-x)*(nx-x)+(ny-y)*(ny-y));
distance/=40000.0;
int ttn=mmap[p(nx,ny)];
int ttnp=mmap[p(x,y)];
addedge(ttn,ttnp,distance);
addedge(ttnp,ttn,distance);
G[ttn][ttnp]=distance;
G[ttnp][ttn]=distance;
x=nx,y=ny;
}
}
for(int i=0;i<tn;i++){
for(int j=0;j<tn;j++){
if(G[i][j]==0&&i!=j){
p p1=mmap1[i];
sx=p1.first,sy=p1.second;
p p2=mmap1[j];
ex=p2.first,ey=p2.second;
double distance1=sqrt((ex-sx)*(ex-sx)+(ey-sy)*(ey-sy));
distance1/=10000.0;
addedge(i,j,distance1);
G[i][j]=distance1;
}
}
}
dijikstra(0);
int final=d[1]*60.0+0.5;
cout<<final<<endl;
//system("pause");
return 0;
}
POJ-2502(Dijikstra应用+最短路)的更多相关文章
- POJ 2502 Subway (最短路)
Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...
- (poj 2502) Subway 最短路
题目链接: 题意:在一个城市里有许多地铁,现在你知道每条地铁的起点 终点与停站点的坐标,知道我们的起始坐标与终点坐标,问加上走路最快到达终点的时间是多少? 方法:求出任意两点的车速时间与步行时间,再 ...
- POJ 2502 - Subway Dijkstra堆优化试水
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...
- 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 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 2502 最短路
http://poj.org/problem?id=2502 同一条地铁线上的站点相邻点间按照v2建边,然后所有点之间按照v1更新建边,所有的边都是双向边,both directions. 然后直接跑 ...
- POJ 2502 Subway-经过预处理的最短路
Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...
- POJ 2502 Subway ( 最短路 && 最短路建图 )
题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...
- Subway POJ - 2502 最短路
题意:给出地铁线 起点和 终点 坐地铁速度为v2 走路为v1 求起点到终点的最短距离 (答案需要四舍五入这里坑了好久) 拿给出的地铁站点 和起点终点建边即可 然后跑个迪杰斯特拉 #inclu ...
- POJ 2502 【思维是朴素的最短路 卡输入和建图】
题意: 给出两个坐标,分别是小明家和小明学校的坐标. 给出多条地铁线,给出每站的坐标,已知地铁是双向的,每条线以-1 -1结尾. 给出地铁速度,步行速度. 地铁线可看成是顺次连接的线段. 求小明从家到 ...
随机推荐
- 关于贪心算法的经典问题(算法效率 or 动态规划)
如题,贪心算法隶属于提高算法效率的方法,也常与动态规划的思路相挂钩或一同出现.下面介绍几个经典贪心问题.(参考自刘汝佳著<算法竞赛入门经典>).P.S.下文皆是我一个字一个字敲出来的,绝对 ...
- hdu 6814 Tetrahedron 规律+排列组合逆元
题意: 给你一个n,你需要从1到n(闭区间)中选出来三个数a,b,c(可以a=b=c),用它们构成一个直角四面体的三条棱(可看图),问你从D点到下面的三角形做一条垂线h,问你1/h2的期望 题解: 那 ...
- UVA - 12295 最短路(迪杰斯特拉)——求按对称路线最短路条数
题意: 给你一个n,然后给你一个n*n的正方形w[i][j],你需要找到一个从(1,1)点走到(n,n)点的最短路径数量.而且这个路径必须按照y=x对称 题解: 我们把左上角的点当作(0,0)点,右下 ...
- 利用windows api共享内存通讯
主要涉及CreateFile,CreateFileMapping,GetLastError,MapViewOfFile,sprintf,OpenFileMapping,CreateProcess Cr ...
- Ubuntu第一次使用注意点
第一次装完Ubuntu登录,打开命令行,登录的不是root权限,切换root不成功: 这个问题产生的原因是由于Ubuntu系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...
- 计蒜客第五场 UCloud 的安全秘钥(中等) (尺取游标法
每个 UCloud 用户会构造一个由数字序列组成的秘钥,用于对服务器进行各种操作.作为一家安全可信的云计算平台,秘钥的安全性至关重要.因此,UCloud 每年会对用户的秘钥进行安全性评估,具体的评估方 ...
- 翻译:《实用的Python编程》01_02_Hello_world
目录 | 上一节 (1.1 Python) | 下一节 (1.3 数字) 1.2 第一个程序 本节讨论有关如何创建一个程序.运行解释器和调试的基础知识. 运行 Python Python 程序始终在解 ...
- MOOC学习成果认证及对高等教育变革路径的影响
MOOC是网络开放教育创新发展的产物,也是备受人们欢迎的网络学习途径.当前制约MOOC能否可持续深入发展的问题聚焦于MOOC学习成果能否得到合理的认证.MOOC学习成果认证分为非学分认证和学分认证.M ...
- CURL (CommandLine Uniform Resource Locator) 简易教程!
1 http://curl.haxx.se/ http://curl.haxx.se/docs/httpscripting.html curl is an open source command li ...
- TypeScript Errors All In One
TypeScript Errors All In One 1. Property 'name' has no initializer and is not definitely assigned in ...