题意,求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. myql命令

    ALTER TABLE 表名 DROP COLUMN 列名#删除某一列

  2. LeetCode:搜索旋转排序数组【33】

    LeetCode:搜索旋转排序数组[33] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2]  ...

  3. CentOS 6.4下OpenSSH升级到6.7操作

    一.升级前准备 1.下载openssh-6.7p1.tar.gz: cd /usr/local/src/wget http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/ ...

  4. vue 项目结构说明

    eslink:规范es6的代码风格检测工具. npm install node-sass -g :全局安装,即使安装之后可以全局使用dode-sass,不用进到工具目录. .babel:把es6转换成 ...

  5. ag-grid

    使用: import { AgGridVue } from "ag-grid-vue"; <ag-grid-vue style="width:100%;height ...

  6. Python编程-网络编程进阶(IO复用、Socketserver)

    一.认证客户端的链接合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方式来实现. 服务端 from socket import * imp ...

  7. Apache的order、allow、deny

    Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是:Or ...

  8. create_workqueue和create_singlethread_workqueue【转】

    本文转载自:http://bgutech.blog.163.com/blog/static/18261124320116181119889/ 1. 什么是workqueueLinux中的Workque ...

  9. linux 虚拟机在线添加新磁盘

    在线添加磁盘,扩展LVM卷案例   一.添加硬盘,在线扫描出来 首先到虚拟机那里添加一块硬盘,注意必须是SCSI类型的硬盘. 扫描硬盘,不用重启操作系统的. echo "- - -" ...

  10. 吴恩达深度学习笔记(五) —— 优化算法:Mini-Batch GD、Momentum、RMSprop、Adam、学习率衰减

    主要内容: 一.Mini-Batch Gradient descent 二.Momentum 四.RMSprop 五.Adam 六.优化算法性能比较 七.学习率衰减 一.Mini-Batch Grad ...