问题描述:

给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。

输入:

第一行包含一个正整数n,表示点数。

接下来n行,每行包含两个整数x[i],y[i](0<=x[i],y[i]<=10^9),依次表示每个点的坐标。

输出:

一个整数,即最小费用。

输入输出样例:

path.in

path.out

5
2 2
1 1
4 5
7 1
6 7

2

数据范围:

对于30%的数据,1<=n<=100;

对于60%的数据,1<=n<=1000;

对于全部的数据,1<=n<=200000;

思路:

1、60分直接最短路

2、对于坐标系中连续的三个点,(x1,y1),(x2,y2),(x3,y3),总有min(x2-x1,y2-y1) + min(x3-x2,y3-y2) ≤ min(x3 -  x1,y3 - y1),所以在建图的时候,可以按x,y分别排一遍序,然后,相邻的点建边,这样边数为2*n,再用堆dij就可以了

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define maxn 200005
#define inf ~0U>>1
using namespace std;
struct point{
int x;
int y;
int pos;
};
struct orz{
int p;
int d;
friend bool operator < (orz a,orz b){
return a.d > b.d;
}
};
struct edge{
int v;
int w;
};
priority_queue< orz > ss;
vector<edge> g[maxn];
point pt[maxn];
int flag = ,v[maxn],d[maxn],n;
bool cmpa(point a,point b){
return a.x < b.x;
}
bool cmpb(point a,point b){
return a.y < b.y;
}
void init(){
cin>>n;
int tx,ty;
for(int i = ;i <= n;i++){
scanf("%d%d",&tx,&ty);
pt[i].x = tx;
pt[i].y = ty;
pt[i].pos = i;
}
for(int i = ;i <= n;i++) d[i] = inf;
sort(pt+,pt++n,cmpa);
edge tmp;
for(int i = ;i < n;i++){
tmp.w = pt[i+].x - pt[i].x;
tmp.v = pt[i + ].pos;
g[pt[i].pos].push_back(tmp);
tmp.v = pt[i].pos;
g[pt[i+].pos].push_back(tmp);
}
sort(pt+,pt++n,cmpb);
for(int i = ;i < n;i++){
tmp.w = pt[i+].y - pt[i].y;
tmp.v = pt[i + ].pos;
g[pt[i].pos].push_back(tmp);
tmp.v = pt[i].pos;
g[pt[i+].pos].push_back(tmp);
}
}
void dij(){
orz tmp;
d[] = ;
tmp.p = ;
tmp.d = ;
ss.push(tmp);
flag++;
int to,wei,x,dd;
edge j;
while(!ss.empty()){
tmp = ss.top();
ss.pop();
x = tmp.p;
dd = tmp.d;
if(v[x] == flag) continue;
v[x] = flag;
for(int i = ;i < g[x].size();i++){
j = g[x][i];
to = j.v;
wei = j.w;
if(d[to] > dd + wei){
d[to] = dd + wei;
tmp.d = dd + wei;
tmp.p = to;
ss.push(tmp);
}
}
}
cout<<d[n];
}
int main(){ init();
dij();
return ;
}

2015山东信息学夏令营 Day5T3 路径的更多相关文章

  1. 2015山东信息学夏令营 Day4T3 生产

    2015山东信息学夏令营 Day4T3 生产 [题目描述] 工厂为了生产一种复杂的产品,给各个生产部门制定了详细的生产计划.那么,就经常会有生产部门要把产品送到另一个生产部门作为原料.这是一个注重产品 ...

  2. Net中获取程序集路径

      从内存中加载的程序集,无路径   IIS中路径 protected void Page_Load(object sender, EventArgs e) { Response.Write(&quo ...

  3. [Xilinx]Modelsim独立仿真Vivado生成的PLL核

    EDA Tools: 1.Vivado 2015.1(64-bit) 2.Modelsim SE-64 10.1c Time: 2016.05.26 ------------------------- ...

  4. POJ2286 The Rotation Game

    Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...

  5. [GRYZ]寒假模拟赛

    写在前面 这是首次广饶一中的OIERS自编自导,自出自做(zuo)的模拟赛. 鉴于水平气压比较低,机(wei)智(suo)的WMY/XYD/HYXZC就上网FQ下海找了不少水(fei)题,经过他们优( ...

  6. 小爬虫。爬取网站多页的通知标题并存取在txt文档里。

    爬取网页中通知标题的内容展示: this is  1  page!<精算学综合>科目考试参考大纲2016年上半年研究生开题报告评议审核结果公示[答辩]2016下半年研究生论文答辩及学位评定 ...

  7. Linux安装mysql mysql5.5.40 <NIOT>

    一.    操作系统与软件 操作系统及版本 Centos 6.4 依赖包 gcc.gcc-c++.cmake.ncurses-devel 下载目录 /opt Mysql安装目录 /usr/local/ ...

  8. Android开发学习之路--React-Native之初体验

      近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下rea ...

  9. iOS应用软件沙盒sandbox相关知识(整理)

    1.iOS沙盒机制原理 iOS应用程序只能在该程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...

随机推荐

  1. C# PropertyInfo(官网)

    using System; using System.Reflection; class Module1 { public static void Main() { // This variable ...

  2. 1354 选数字 DP背包 + 数学剪枝

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1354&judgeId=187448 其实这题和在若干个数字中,选 ...

  3. net start iisadmin报错:系统找不到指定的文件

    IIS Admin Service不能启动 ,直接启动或命令(net start iisadmin)都不成功.导致IIS站点访问异常. 最终参考网上解决方案: 这是大多是由于windows\syste ...

  4. MySQL学习随笔--视图

    视图概念 数据库中的视图指的是一个虚拟表,其内容由查询定义.同真实的表一样,视图也是由行与列构成的.视图的数据来源由SQL语句查询得到,不存储数据 视图创建方法 格式 : create view 视图 ...

  5. 飞思卡尔开发板-迅为IMX6开兼容单核 双核 四核Plus开发板

    飞思卡尔开发硬件接口介绍: 核心板参数 尺寸:51mm*61mm CPU:Freescale Cortex-A9 四核 i.MX6Q,主频 1.2 GHz 内存:2GB DDR3 存储:16GB EM ...

  6. jQuery PC端图片预览,鼠标移上去查看大图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. scrapy 请求传参

    class MovieSpider(scrapy.Spider): name = 'movie' allowed_domains = ['www.id97.com'] start_urls = ['h ...

  8. 03XML Schema Definition

    1. XML Schema Definition 1. XML Schema Definition XML Schema(XML Schema Definition,XSD)用于描述 XML 文档的结 ...

  9. js【table】合并行,合并列

    eg: function hbh() { var tab = document.getElementById("tableID"); var maxCol = 2, val, co ...

  10. base64记载

    一丶 js /** * * Base64 encode / decode * * @author haitao.tu * @date 2010-04-26 * @email tuhaitao@foxm ...