思路:

建完了图就是模板水题了 …..

但是建图很坑。

首先要把出发点向地铁站&终点 连一条边

地铁站之间要连无向边

地铁站向终点连一条边

以上的边权要*0.006

两个地铁站之间要连无向边 边权*0.0015

//By SiriusRen
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 40500
int n,ex,ey,cnt=1,tot=0;
int xx,yy,lastx,lasty;
struct node{int now;double weight;}jy,top;
double w[N],dis[333];
int v[N],next[N],first[333],px[N],py[N];
bool vis[333];
void add(int x,int y,double ww){
w[tot]=ww;v[tot]=y;
next[tot]=first[x];first[x]=tot++;
}
priority_queue<node>pq;
bool operator <(node a,node b){
return a.weight>b.weight;
}
void Dijkstra(){
jy.now=jy.weight=0;
pq.push(jy);
while(!pq.empty()){
top=pq.top();pq.pop();
if(vis[top.now])continue;
vis[top.now]=1;
for(int i=first[top.now];~i;i=next[i])
if(!vis[v[i]]&&dis[v[i]]>dis[top.now]+w[i]){
dis[v[i]]=dis[top.now]+w[i];
jy.now=v[i];
jy.weight=top.weight+w[i];
pq.push(jy);
}
}
}
queue<int>q;
void spfa()
{
q.push(0);
while(!q.empty())
{
int t=q.front();q.pop();
vis[t]=0;
for(int i=first[t];~i;i=next[i])
{
if(dis[v[i]]>dis[t]+w[i])
{
dis[v[i]]=dis[t]+w[i];
if(!vis[v[i]])q.push(v[i]),vis[v[i]]=1;
}
}
}
}
int main()
{
for(int i=1;i<=222;i++)dis[i]=0x3fffff;
memset(first,-1,sizeof(first));
scanf("%d%d%d%d",&px[0],&py[0],&ex,&ey);
scanf("%d%d",&lastx,&lasty);
px[cnt]=lastx;py[cnt]=lasty;
while(scanf("%d%d",&xx,&yy)){
if(xx==-1&&yy==-1){
if(scanf("%d%d",&lastx,&lasty)==EOF)break;
cnt++;
px[cnt]=lastx;py[cnt]=lasty;
continue;
}
add(cnt,cnt+1,sqrt((lastx-xx)*(lastx-xx)+(lasty-yy)*(lasty-yy))*0.0015);
add(cnt+1,cnt,sqrt((lastx-xx)*(lastx-xx)+(lasty-yy)*(lasty-yy))*0.0015);
lastx=xx,lasty=yy;
cnt++;
px[cnt]=lastx;py[cnt]=lasty;
}
px[++cnt]=ex;py[cnt]=ey;
for(int i=0;i<=cnt;i++)
for(int j=i+1;j<=cnt;j++){
add(i,j,sqrt((px[i]-px[j])*((px[i]-px[j]))+(py[i]-py[j])*(py[i]-py[j]))*0.006);
add(j,i,sqrt((px[i]-px[j])*((px[i]-px[j]))+(py[i]-py[j])*(py[i]-py[j]))*0.006);
}
spfa();
printf("%d\n",(int)(dis[cnt]+0.5));
}

POJ 2502 Dijkstra OR spfa的更多相关文章

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

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

  2. POJ 1502 MPI Maelstrom( Spfa, Floyd, Dijkstra)

    题目大意: 给你 1到n ,  n个计算机进行数据传输, 问从1为起点传输到所有点的最短时间是多少, 其实就是算 1 到所有点的时间中最长的那个点. 然后是数据 给你一个n 代表有n个点, 然后给你一 ...

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

  4. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  5. hdu1874 畅通project续 最短路 floyd或dijkstra或spfa

    Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多路.只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择.而某些方案 ...

  6. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  7. POJ 2502 Subway (Dijkstra 最短+建设规划)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6689   Accepted: 2176 Descriptio ...

  8. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  9. POJ 3268 Dijkstra+priority_queue或SPFA

    思路:正向建边,一遍Dijkstra,反向建边,再一遍Dijkstra.ans加在一起输出最大值. (SPFA也行--) // by SiriusRen #include <queue> ...

随机推荐

  1. 给centos重新安装yum的base-repo源

    转自:https://blog.csdn.net/lovemysea/article/details/79552952 如果自己的centos的系统yum源出现问题了,如何才能修复? 方式一:使用国内 ...

  2. Extjs 可重用组件开始写 2014 8 23日

    今天开始自己去写组件. 这次写组件重点在于参考cfWeb来写出自己的组件. 首先先把结构做出来. 对于这次的自定义组件,现在所做的事情关键在于上面四个文件.于是将上面四个文件贴出来. MyApp.js ...

  3. BZOJ 4004 高斯消元

    思路: 排个序 消元 完事~ 但是! 坑爹精度毁我人生 我hhhh他一脸 红红火火恍恍惚惚 //By SiriusRen #include <cmath> #include <cst ...

  4. ML学习笔记- 神经网络

    神经网络 有的模型可以有多种算法.而有的算法可能可用于多种模型.在神经网络中,对外部环境提供的模式样本进行学习训练,并能存储这种模式,则称为感知器;对外部环境有适应能力,能自动提取外部环境变化特征,则 ...

  5. 当安装了ubuntu操作系统怎么也调用不出中文输入法时,可以用以下方式尝试解决。

    卸载 fcitx sudo apt-get remove fcitx 重启 sudo reboot 重新安装 fcitxsudo apt-get isntall fcitx 安装拼音输入法sudo a ...

  6. .NET MVC Dropzone 上传图片

    在nuget控制台输入:Install-Package dropzone @{ Layout = null; } <!DOCTYPE html> <html> <head ...

  7. 手把手教你写带登录的NodeJS爬虫+数据展示

    其实在早之前,就做过立马理财的销售额统计,只不过是用前端js写的,需要在首页的console调试面板里粘贴一段代码执行,点击这里.主要是通过定时爬取https://www.lmlc.com/s/web ...

  8. java 读写分离

    源码地址:http://git.oschina.net/xiaochangwei 先回答下 1.为啥要读写分离? 大家都知道最初开始,一个项目对应一个数据库,基本是一对一的,但是由于后来用户及数据还有 ...

  9. 在应用层通过spring特性解决数据库读写分离

    如何配置mysql数据库的主从? 单机配置mysql主从:http://my.oschina.net/god/blog/496 常见的解决数据库读写分离有两种方案 1.应用层 http://neore ...

  10. MySQL监控SQL及命中率脚本

    [root@hank-yoon scrips]# vi mysqlstat.sh​​​/export/servers/mysql/bin/mysqladmin -P3306 -uyoon -pyoon ...