用线段树写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公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资. ...
随机推荐
- printk的使用技巧
在 linux/kernel.h 中有相应的宏对应. #define KERN_EMERG "<0>" /* system is unusable */#d ...
- 金阳光Android自动化测试第一季
第一季:http://www.chuanke.com/v1983382-106000-218422.html 第一节:Android自动化预备课程基础(上) 1. 基于坐标点触屏:monkey ...
- No identifier specified for entity: XXXX 错误
在运行项目的时候报了下面的错误: by: org.hibernate.AnnotationException: No identifier specified for entity: com.exam ...
- Selenium WebDriver-下拉框断言
#encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...
- ptyhon - 接口自动化测试实战case1
work_20181203_httprequest.py: import requestsclass http_request: def http_get(url,params): res = req ...
- 矩阵快速幂在ACM中的应用
矩阵快速幂在ACM中的应用 16计算机2黄睿博 首发于个人博客http://www.cnblogs.com/BobHuang/ 作为一个acmer,矩阵在这个算法竞赛中还是蛮多的,一个优秀的算法可以影 ...
- Pycharm脚本通用部分设置
Python脚本经常要设置同样的注释内容,Pycharm里面提供的模板可以很好的实现这个需求. 查找: File->settings->Editor->File and Code T ...
- Difference between git remote add and git clone
http://stackoverflow.com/questions/4855561/difference-between-git-remote-add-and-git-clone git remot ...
- .net SignalR 聊天室
代码地址:https://gitee.com/srnsrn/netSignalr.git 运行项目打开多个页面 私密聊天
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法
代码改变世界 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pre ...