题意,求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. Shortcut Keys in Eclipse

    @1: Here are some shortcut keys in Eclipse that I use a lot.Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开 ...

  2. Django——Session源码分析

    首先我们导入django.contrib.sessions.middleware这个中间件,查看里面的Session源码 from django.contrib.sessions.middleware ...

  3. 学会Retrofit+OkHttp+RxAndroid三剑客的使用,让自己紧跟Android潮流的步伐

    http://blog.csdn.net/iamzgx/article/details/51607387 概括 在上一篇博客android网络框架OkHttp之get请求(源码初识) 讲解了OkHtt ...

  4. Python编程-数据类型方法

    一.进制简介 进制也就是进位制,是人们规定的一种进位方法.对于任何一种进制---X进制,就表示某一位置上的数运算时是逢X进一位.十进制是逢十进一,十六进制是逢十六进一,二进制就是逢二进一,以此类推,x ...

  5. Hadoop单机搭建

    单机Hadoop搭建 1.下载hadoop-2.7.3.tar.gz http://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-2.7.3/h ...

  6. python配置文件操作

    步骤: 1.导入模块  import configparser 2.创建实例 cf = configparser.ConfigParser() 3.读取配置文件,若配置文件中有中文,则需设置编码格式  ...

  7. HttpUtils工具类

    HttpUtils工具类 package net.hs.itn.teng.common.util; import java.io.IOException; import java.io.Unsuppo ...

  8. ARC管理内存(一)

    相关概念 栈 当程序执行某个方法(或函数)时,会从内存中名为栈(stack)的区域分配一块内存空间,这块内存空间称为帧(frame).帧负责保存程序在方法内声明的变量的值.在方法内声明的变量称为局部变 ...

  9. 获取蓝牙mac地址

    http://macpu.github.io/2015/11/12/iOS%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E8%93%9D%E7%89%99Mac%E5%9C ...

  10. HTML5 上传图片 到ASP.NET MVC

    @{ ViewBag.Title = "Home Page"; } <!DOCTYPE HTML PUBLIC> <html> <head> & ...