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应用+最短路)的更多相关文章

  1. POJ 2502 Subway (最短路)

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

  2. (poj 2502) Subway 最短路

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

  3. POJ 2502 - Subway Dijkstra堆优化试水

    做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...

  4. 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 ...

  5. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  6. POJ 2502 最短路

    http://poj.org/problem?id=2502 同一条地铁线上的站点相邻点间按照v2建边,然后所有点之间按照v1更新建边,所有的边都是双向边,both directions. 然后直接跑 ...

  7. POJ 2502 Subway-经过预处理的最短路

    Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...

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

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

  9. Subway POJ - 2502 最短路

    题意:给出地铁线  起点和 终点  坐地铁速度为v2  走路为v1 求起点到终点的最短距离  (答案需要四舍五入这里坑了好久) 拿给出的地铁站点 和起点终点建边即可  然后跑个迪杰斯特拉 #inclu ...

  10. POJ 2502 【思维是朴素的最短路 卡输入和建图】

    题意: 给出两个坐标,分别是小明家和小明学校的坐标. 给出多条地铁线,给出每站的坐标,已知地铁是双向的,每条线以-1 -1结尾. 给出地铁速度,步行速度. 地铁线可看成是顺次连接的线段. 求小明从家到 ...

随机推荐

  1. 【noi 2.6_9289】Ant Counting 数蚂蚁{Usaco2005 Nov}(DP)

    题意:有M个家族的蚂蚁,各Ni只(互相相同).问选出 l~r 只的不同方案数. 解法:很基础的一种DP,不要被"排列组合"所迷惑了啊~我之前接触过这个类型,可惜又忘了,一定要记住! ...

  2. fzu2204 7

    Problem Description n个有标号的球围成一个圈.每个球有两种颜色可以选择黑或白染色.问有多少种方案使得没有出现连续白球7个或连续黑球7个.  Input 第一行有多组数据.第一行T表 ...

  3. 571A Lengthening Sticks

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  4. 国产网络损伤仪SandStorm -- 为什么数据流还是走Bypass链路?

    如果你在使用网络损伤仪SandStorm测试移动互联网的应用程序或者在仿真所谓"弱网测试"的时候,发现所有的数据流还是在走Bypass链路,并没有预期地走自己创建的仿真链路,那么你 ...

  5. VScode 相关

    1.F5运行py文件,打开terminal终端的时候总是弹出Powershell窗口,只能在powershell窗口中用命令行运行程序,实在很不方便. 解法:右键Powershell属性,取消使用旧版 ...

  6. 鸟哥的linux私房菜——第十三章学习(Linux 帐号管理与 ACLL 权限设置)

    第十三章.Linux 帐号管理与 ACLL 权限设置 1.0).使用者识别码: UID 与 GID UID :User ID GID :group ID [root@study ~]# ll -d / ...

  7. 基于OpenCV全景拼接(Python)SIFT/SURF

    一.实验内容: 利用sift算法,实现全景拼接算法,将给定的两幅图片拼接为一幅. 二.实验环境: 主机配置: CPU :intel core i5-7300 2.50GHZ RAM :8.0GB 运行 ...

  8. codeforces 1C (非原创)

    C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...

  9. spring-cloud-netflix-eureka-client

    服务注册中心eureka-server已经搭好,我们开始编写一个eureka-client,并提供一个hello服务 一.新建module,选择对应的springcloud模块,pom.xml如下: ...

  10. 手工数据结构系列-C语言模拟队列和栈 hdu1702

    #include <stdio.h> #include <stdlib.h> //================= DATA STRUCTURE ============== ...