hihocoder 1138 Islands Travel dijkstra+heap 难度:2
http://hihocoder.com/problemset/problem/1138
很久不用最短路,几乎连基本性质也忘了,结果这道题就是某些最短路算法空间复杂度是o(n)
这里总结四种算法
算法名称 时间复杂度 空间复杂度
dijkstra+heap O(elog(e+n)) O(n)
bellman-ford O(ne) O(n)
spfa O(ke) O(n)
floyd-warshall O(n^3) O(n^2)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue> using namespace std;
const int maxn = 1e5 + 5;
const int maxm = 1e6 + 6; int first[maxn],n;
struct edge
{
int t,c,nxt;
} e[maxm]; void addedge(int f,int t,int c,int ind)
{
e[ind].nxt = first[f];
e[ind].t = t;
e[ind].c = c;
first[f] = ind;
} struct pnt
{
int x,y,id;
pnt()
{
x = y = id = 0;
}
pnt(int _x,int _y,int _id)
{
x = _x;
y = _y;
id = _id;
}
};
bool cmpx(pnt p1,pnt p2)
{
if(p1.x!= p2.x)return p1.x < p2.x;
return p1.y < p2.y;
}
bool cmpy(pnt p1,pnt p2)
{
if(p1.y!= p2.y)return p1.y < p2.y;
return p1.x < p2.x;
}
pnt a[maxn];
long long dis[maxn];
bool vis[maxn];
typedef pair<long long ,int> P;
priority_queue<P, vector <P>, greater<P> > que;
long long dijkstra()
{
for(int i = 0; i < n; i++)dis[i] = 2e18;
memset(vis,false,sizeof vis);
while(!que.empty())que.pop(); dis[0] = 0;
vis[0] = true;
for(int p = first[0]; p != -1; p = e[p].nxt)
{
int t = e[p].t;
dis[t] = e[p].c;
que.push(P(dis[t],t));
} while(!que.empty())
{
int f = que.top().second;
que.pop();
if(f == n-1)break;
if(vis[f])continue;
vis[f] = true; for(int p = first[f]; p != -1; p = e[p].nxt)
{
int t = e[p].t;
if(dis[t] > dis[f] + e[p].c){
dis[t] = dis[f] + e[p].c;
que.push(P(dis[t],t));
}
}
} return dis[n - 1];
}
int main()
{
while(scanf("%d",&n)==1)
{
memset(first, -1, sizeof first);
for(int i = 0; i < n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].id = i;
} sort(a,a + n,cmpx);
for(int i = 0; i < n - 1; i++)
{
addedge(a[i].id,a[i + 1].id,
min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i);
addedge(a[i + 1].id,a[i].id,
min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i + 1);
}
sort(a,a + n,cmpy);
for(int i = 0; i < n - 1; i++)
{
addedge(a[i].id,a[i + 1].id,
min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i + 2 * n);
addedge(a[i + 1].id,a[i].id,
min(abs(a[i].x - a[i + 1].x),abs(a[i].y - a[i + 1].y)),2 * i + 1 + 2 * n);
} long long ans = dijkstra();
printf("%lld\n",ans);
}
return 0;
}
hihocoder 1138 Islands Travel dijkstra+heap 难度:2的更多相关文章
- hihocoder #1138 : Islands Travel
题意,求1到n的最短路.不难想到单源最短路,难点在于数量级太大,因此如何建图是关键: 因为cost = min{|Xi-Xj|, |Yi-Yj|}:所以,点i的移动只有两种情况,. x距离最近的点,. ...
- 【最短路算法】Dijkstra+heap和SPFA的区别
单源最短路问题(SSSP)常用的算法有Dijkstra,Bellman-Ford,这两个算法进行优化,就有了Dijkstra+heap.SPFA(Shortest Path Faster Algori ...
- 【BZOJ-1576】安全路径Travel Dijkstra + 并查集
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1044 Solved: 363[Sub ...
- [dijkstra+heap优化] 模板
var n,m,s,i,j,x,y,z,l,tot :longint; pre,last,other,len :..] of longint; heap,d,pl :Array[..] of long ...
- 【CF20C】Dijkstra?(DIJKSTRA+HEAP)
没什么可以说的 做dijk+heap模板吧 以后考试时候看情况选择SFPA和DIJKSTRA ; ..]of longint; dis:..]of int64; a:..]of int64; b:.. ...
- hihocoder Counting Islands II(并查集)
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...
- Dijkstra+Heap模板
普通Dijkstra: void DijkstraPath(int v0,int vis[],int dist[],int path[]) { int onePath[maxn]; int d; in ...
- Hihocoder #1081 最短路径一 dijkstra
#1081 : 最短路径·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 万圣节的早上,小Hi和小Ho在经历了一个小时的争论后,终于决定了如何度过这样有意义的一天—— ...
- Dijkstra、Dij + heap、Floyd、SPFA、 SPFA + SLF Template
Dijkstra in Adjacency matrix : int Dijkstra(int src,int tec, int n){ ]; ]; memset(done,,sizeof(done) ...
随机推荐
- poj2187Beauty Contest(凸包直径)
链接 利用旋转卡壳 参考博客http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <iostream> ...
- poj1113Wall(凸包)
链接 顺便整理出来一份自己看着比较顺眼的模板 #include <iostream> #include<cstdio> #include<cstring> #inc ...
- Oracle Regexp_substr
Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','2 ...
- Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- Android SQLite数据库
SQLite数据库 SQLiteDatabase //管理操作数据库 管理 openDatabase //打开 openOrCreateDatabase //打开或创建 操作 ...
- linux 文件类型 文件权限
linux中常见的文件类型有: “—”表示普通文件 :-rw-r--r-- 1 root root 41727 07-13 02:56 install.log “d”表示目录 :drwxr-xr- ...
- smarty 学习记录
smarty模版是比较大众化的一个模版,在php开发过程当中被很多开发者视为最友好的模版之一,学习smarty课程对于很多培训机构来说也是列入了培训课程之一,那么很多方面就需要我们学习了一. 安装首先 ...
- UTF-7编码
目录 1 编码 1 2 编码代码(C++) 2 3 解码代码(C++) 4 4 测试代码(VC++) 7 1 编码 UTF-7编码的规则及特点为: 1)UTF16小于等于 0x ...
- javaWeb1 tomcat
tomcat使用常见问题: 1.闪退: 原因:tomcat 软件是由java语言开发的,当它启动时,会默认到系统 的环境变量中查找 JAVA_HOME 的变量.找它的目的时tomcat 启动 时需要j ...
- 笔记8:winfrom连接数据库DBHelp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...