用线段树写Dijkstar
如题
noip前就想用线段树优化Dijkstar
写那啥,感觉挺好玩的
写了个线段树优化的Dijkstar
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn =10007;
const int maxm = 500007;
const int INF = 0x7fffffff;
int n,m;
inline int read() {
int x=0;
char c=getchar();
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x;
}
struct node{
int v,next,w;
}edge[maxm];
int num=0,head[maxn];
inline void add_edge(int a,int b,int c) {
edge[++num].v=b;edge[num].w=c;edge[num].next=head[a];head[a]=num;
}
int dis[maxn],ans[maxn],s,t;
int tree[maxn<<2],leaf;
inline int check(int i,int j) {
return dis[i]<dis[j]?i:j;
}
inline void build() {
std::memset(dis,0x3f,sizeof dis);// for(int i=0;i<=n+1;i++) dis[i]=INF;
for(leaf=1;leaf<=n;leaf<<=1);--leaf;
for(int i=1;i<=n;++i)tree[leaf+i]=i;
}
inline void modify(int x,int y) {
dis[x]=y,x+=leaf,x>>=1;
while(x) tree[x]=check(tree[x<<1],tree[x<<1|1]),x=x>>1;
}
void dijkstra(int s) {
build();
dis[s]=0;
int u=s;
for(int i=1;i<=n;++i) {
ans[u]=dis[u];
const int disu=dis[u];
modify(u,INF);
for(int j=head[u];j;j=edge[j].next){
int v=edge[j].v;
if(dis[v]<INF&&dis[v]>disu+edge[j].w)
modify(v,disu+edge[j].w);
}
u=tree[1];
}
}
inline void put(int x)
{
if (x > 9) put(x / 10);
putchar(x % 10 + 48);
}
int main() {
int k;
n=read(),m=read(),k=read();
for(int a,b,c,i=1;i<=m;++i) {
a=read(),b=read(),c=read();
add_edge(a,b,c);
}
dijkstra(k);
for(int i=1;i<=n;++i) {
if(dis[i]==0x3f3f3f3f)ans[i]=INF;
put(ans[i]), putchar(' ');
}
return 0;
}
用线段树写Dijkstar的更多相关文章
- codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))
题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...
- 【图论】用线段树写Dijikstra!!
速度是没有极限的. 众说周知,Dijikstra是一种最短路算法,复杂度为O(V^2+E) 朴素Dijikstra void Dijikstra(int s){ memset(dis,inf,size ...
- 【BZOJ-4653】区间 线段树 + 排序 + 离散化
4653: [Noi2016]区间 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 107 Solved: 70[Submit][Status][Di ...
- Mango DS Traning #49 ---线段树3 解题手记
Training address: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=38994#overview B.Xenia and B ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- LightOJ 1085(树状数组+离散化+DP,线段树)
All Possible Increasing Subsequences Time Limit:3000MS Memory Limit:65536KB 64bit IO Format: ...
- Vijos P1103 校门外的树【线段树,模拟】
校门外的树 描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……, ...
- BZOJ_3685_普通van Emde Boas树_权值线段树
BZOJ_3685_普通van Emde Boas树_权值线段树 Description 设计数据结构支持: 1 x 若x不存在,插入x 2 x 若x存在,删除x 3 输出当前最小值,若不存 ...
- BZOJ_1503_[NOI2004]郁闷的出纳员_权值线段树
BZOJ_1503_[NOI2004]郁闷的出纳员_权值线段树 Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资. ...
随机推荐
- python中文件操作的基本方法
在python中对一个文件进行操作,分为三大步:打开,操作,关闭 首先创建一个文件hello,里面内容为hello world 一.打开一个文件 1.#open(‘文件名或文件路径’,‘操作模式’,文 ...
- exp分析
1 from pwn import* 2 3 local =1 4 debug = 1 5 6 if local: 7 p = process('./pwn1') 8 else: 9 p = remo ...
- LeetCode(165) Compare Version Numbers
题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...
- WIN 备份 重装
title: WIN 备份 重装 date: 2018-09-01 22:35:31 updated: tags: [windows,记录,折腾] description: keywords: com ...
- powerdesigner约束名唯一出错的解决办法
powerdesigner中自动生成的约束名有时会因为表的前缀一样而不具有唯一性,这样在生成时就会出错,一般的解决办法有以下两种: 1.模型=>Reference中可以看到当前模型中的所有Ref ...
- selenium - 下拉框操作
# 9. 下拉框操作# (1)等待下拉列表和下拉列表中值存在# (2)在下拉列表中选择一个值 # 三种方式# A. 获取所有的下拉列表值,然后用循环去匹配相同的值 select_by_index(下标 ...
- 常用软件URL
1.MSDN:https://msdn.itellyou.cn/ 2.软碟通(UltraISO)http://rj.baidu.com/soft/detail/11522.html?ald Ultra ...
- xtrabackup: error: last checkpoint LSN (3409281307) is larger than last copied LSN (3409274368). #2
1.错误发生场景:使用2.4.1版本的xtrabackup工具进行全备,备份日志中报出此错误2.知识要点:MySQL中,redo 日志写进程会在三种条件下被触发从log buffer中写日志到redo ...
- JDBC 学习笔记(七)—— CallableStatement
在大型关系型数据库中,有一组为了完成特定功能的 SQL 语句集被称为存储过程(Stored Procedure),它是数据库中的对象. JDBC 使用 CallableStatement 对象,完成对 ...
- iOS学习笔记45-Swift(五)协议
一.Swift协议 协议是为方法.属性等定义一套规范,没有具体的实现,类似于Java中的抽象接口,它只是描述了方法或属性的骨架,而不是实现.方法和属性实现还需要通过定义类,函数和枚举完成. 1. 协议 ...