题意,求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. 4.4 使用STM32控制MC20进行GPS帧数据解析

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  2. Django基础(三)_分页器、COOKIE与SESSION、FORM表单

    分页器(paginator) 分页器的使用 >>> from django.core.paginator import Paginator >>> objects ...

  3. QT应用程序设置图标

    一.纯Qt 1.下载图标:app.ico 2.新建记事本,输入:IDI_ICON1 ICON DISCARDABLE"app.ico":改变名字为jude.rc 3.将两个文件放在 ...

  4. 在Xcode中使用pch文件

    Xcode后面的版本不建议在开发时使用pch文件,但如果仍然需要使用pch文件可以这样配置: 1.用快捷键  [CMD] + N 新建文件,选 iOS下的 other->PCH File 2.p ...

  5. tcp三次握手过程

    TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确 ...

  6. Elasticsearch6.4.3安装

    Linux内存一定要1g以上! 首先要有jdk环境 要求1.8版本以上   elasticsearch是Java写的 将上传的 elasticSearch安装包解压 cd /home/elastics ...

  7. 通过join方法顺序执行多个线程

    方法一:直接用多线程之间的通讯去解决 package com.toov5.test; import javax.imageio.ImageTypeSpecifier; class Res1{ char ...

  8. jQuery计时器插件

    /** * 定义一个jQuery计时插件,实现记录计时开始时间.结束时间,总共耗时的功能 * @param $ * * @author Ivan 2862099249@qq.com * @date 2 ...

  9. 解决可以Ping通ip地址,但Ping不通域名的思路

    在正常的网络故障处理中,ping命令是大家经常用到的,出现ping通ip地址,但ping域名是出现超时情况,一般是由于TCP/IP协议中的“DNS设置”不正确,请检查其中的配置,或者更换其他可用的DN ...

  10. Android 报错Android - Performing stop of activity that is not resumed

    [原文] FROM STACKOVERFLOW: Just giving my 50 cents on the issue. Catching the exception is indeed one ...