题意,求1到n的最短路。不难想到单源最短路,难点在于数量级太大,因此如何建图是关键;

因为cost =  min{|Xi-Xj|, |Yi-Yj|};所以,点i的移动只有两种情况,. x距离最近的点,. y距离最近的点 

如此一来,每个点i的最多只有四条边(为什么是四条?),这样复杂度就降下来了,单源最短路的复杂度为n*m(点*边数) 

我用spfa。 

解题思路: 

x轴排序,建图 

y轴排序,建图 

求最短路 

用spfa注意队列que的大小至少是n*m,可以使用循环队列 

#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ; struct node
{
int v;
int i;
bool operator < (const node &t)const
{
return v < t.v;
}
}x[N],y[N]; vector<int>g[N];
int p[N][]; int ABS(int x)
{
return x >= ? x : -x;
} long long get_value(int i,int j)
{
return min(ABS(p[i][]-p[j][]),ABS(p[i][]-p[j][]));
} bool flag[N];
long long dist[N];
int que[N]; long long spfa(int s,int n)
{ memset(flag,false,sizeof(flag));
memset(dist,0x7f,sizeof(dist));
int head = ,tail = ;
dist[s] = ;
flag[s] = true;
que[tail++] = s;
while(head != tail)
{
int tep = que[head++];
if(head >= N) head = ;//循环队列 flag[tep] = false;
for(int i = ; i < g[tep].size(); i++)
{
int v = g[tep][i];
if(dist[v] > dist[tep] + get_value(v,tep))
{
dist[v] = dist[tep] + get_value(v,tep);
if(!flag[v])
{
que[tail++] = v;
if(tail >= N) tail = ;//循环队列 flag[v] = true;
}
}
}
}
return dist[n-]; } int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i = ; i < N; i++) g[i].clear();
for(int i = ; i < n; i++)
{ scanf("%d %d",&p[i][],&p[i][]);
x[i].v = p[i][]; x[i].i = i;
y[i].v = p[i][]; y[i].i = i;
}
sort(x,x+n);
sort(y,y+n);
for(int i = ; i < n; i++)
{
int u = x[i-].i;
int v = x[i].i;
//这里可以去下重
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = ; i < n; i++)
{
int u = y[i-].i;
int v = y[i].i;
//这里可以去下重
g[u].push_back(v);
g[v].push_back(u);
}
printf("%lld\n",spfa(,n));
}
return ;
}

hihocoder #1138 : Islands Travel的更多相关文章

  1. hihocoder 1138 Islands Travel dijkstra+heap 难度:2

    http://hihocoder.com/problemset/problem/1138 很久不用最短路,几乎连基本性质也忘了,结果这道题就是某些最短路算法空间复杂度是o(n) 这里总结四种算法 算法 ...

  2. hihocoder Counting Islands II(并查集)

    Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...

  3. 微软2016校园招聘在线笔试 [Recruitment]

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A company plans to recruit some new employees. There are N ca ...

  4. hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1578 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for ...

  5. USACO环绕岛屿Surround the Islands 并查集 枚举暴力

    题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...

  6. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  7. [LeetCode] Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  9. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

随机推荐

  1. mysql数据库补充知识4 数据备份和pymysql模块

    一 介绍 #注意: 批量加注释:ctrl+?键 批量去注释:ctrl+shift+?键 二 MySQL数据备份 #1. 物理备份: 直接复制数据库文件,适用于大型数据库环境.但不能恢复到异构系统中如W ...

  2. Javascript 广告浮动效果在浏览器中间N秒后移动到右下角

    Javascript 广告浮动效果在浏览器中间N秒后移动到右下角 闲着无聊做了一个,本人原创...就是这个页面的广告效果....怎么样???? 刚刚学习的javascript

  3. 蓝图、基于DBUtils实现数据库连接池、上下文管理等

    基于DBUtils实现数据库连接池 小知识: 1.子类继承父类的三种方式 class Dog(Animal): #子类 派生类 def __init__(self,name,breed, life_v ...

  4. Eclipse部署项目的时候抛异常【Multiple Contexts have a path of "/cdcpm".】

    Eclipse部署项目的时候抛异常[Multiple Contexts have a path of "/cdcpm".]重新clean .删除server都不好使.查看一下tom ...

  5. Linux Shell基础 Shell基本知识

    概述 在 Linux 的脚本中,只要是基于 Bash语法写的Shell脚本第一行必须是"#!/bin/bash",用来声明此文件是一个脚本. 运行方式 Shell 脚本的运行主要有 ...

  6. python中编写无参数decorator

    Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法,这样可以避免手动编写 f = de ...

  7. 跨平台移动开发 App-Framework DEMO 演示

    穿越到2015 回到->MarkFan的程序员客栈 App-Framework   DEMO 演示 点击APK包下载 点击Demo代码下载 官方网站 :http://app-framework- ...

  8. 主攻ASP.NET MVC4.0之重生:MVC Controller修改Controller.tt模版,自动添加版本注释信息

    第一步找到MVC 4.0 CodeTemplates 一般路径在:C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Ite ...

  9. CentOS 6.5下Redmine的安装配置

    首先引用百度介绍下redmine: Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统,据说是源于Basecamp的ror版而来,支持多种数据库,有不 ...

  10. C/C++ 字符串操作函数 思维导图梳理

    这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰