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) ...
随机推荐
- 项目问题总结:Block内存泄露 以及NSTimer使用问题
BLock的内存泄露 在我们代码中关于block的使用可以说随处可见,第一次接触block的时候是关于UIView的块动画,那时觉得block的使用好神奇,再后来分析总结为block其实就是一个c语言 ...
- Linux下查看文件权限、修改文件权限的方法
查看权限命令查看目录的相关权限可以采用命令ls -lD,或者直接用ls -la 如 ls -l www.jb51.net //这里表示查看www.jb51.net目录 修改权限命令 chmod 77 ...
- 省市县distpicker的使用
下载地址https://github.com/fengyuanchen/distpicker 1.引入 <!-- 引入地址 begin --> <script type=" ...
- Android 让输入框输入指定字符的办法
让输入框输入指定字符的办法 有一个需求 让输入密码的时候只能输入数字字母可见字符 不能输入中文 之前还以为要写代码 还来发现有一个属性可以直接实现 <EditText android:layou ...
- Android_程序结构分析
一.Android程序运行过程 二.Android项目结构
- Windows BAT文件使用技巧[转载]
首先,批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(notepad)等任 ...
- 任务调度quartz
http://www.cnblogs.com/cnjava/archive/2013/02/28/2937291.html
- vsphere vcenter 添加域管理员
选择 vcenter server 管理权限
- 多重比对multiple alignment
之前只接触过双序列比对,现在需要开始用多序列比对了. 基本概念:多序列比对 - 百科 常用的 multiple alignment 软件: Muscle ClustalW T-coffee 软件之间的 ...
- 项目二:使用机器学习(SVM)进行基因预测
SVM软件包 LIBSVM -- A Library for Support Vector Machines(本项目所用到的SVM包)(目前最新版:libsvm-3.21,2016年7月8日) C-S ...