最短路问题--P4779 单源最短路(标准版)Dijkstra堆优化
题目背景
题目描述
给定一个 n 个点,m 条有向边的带非负权图,请你计算从 s 出发,到每个点的距离。
数据保证你能从 s 出发到任意点。
输入格式
第一行为三个正整数 n, m, s。 第二行起 m 行,每行三个非负整数 ui,vi,wi,表示ui 到vi 有一条权值为wi 的有向边。
输出格式
输出一行 n 个空格分隔的非负整数,表示 s到每个点的距离。
注意:这是一个有向图!!! (存成无向图,36分 d了半个小时,结果删了一行代码就ac了)
因为是模版题,其实和标准Dijkstra的思想差不多,具体详解参照 最短路问题-- Dijkstra Choose the best route
#include <bits/stdc++.h>
using namespace std;
;
long long n,m,s,t;
long long u,v,w;
priority_queue< pair<int ,int > >q; int dis[N],vis[N];
int read()
{
,a = ;
char ch = getchar();
'){
;
ch = getchar();
}
'){
a = a * + ch - ';
ch = getchar();
}
return x*a;
}
struct node
{
int val;
int to;
int next;
}e[N];
int head [N];
;
void add (int u,int v,int w)
{
e[++tot].val=w;
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot;
}
void Dijkstra(int S)
{
q.push(make_pair(,S)); memset(vis,,;
while(!q.empty())
{
int x = q.top().second;
q.pop();
if(vis[x])
continue;
vis[x] = ;
;i=e[i].next)
{
int to1=e[i].to;
if(dis[to1] > dis[x] + e[i].val)
{
dis[to1] = dis[x] + e[i].val ;
q.push(make_pair(-dis[to1],to1));
}
}
}
return;
}
int main()
{
n=read();
m=read();
s=read();
;i <= m;i++)
{
u=read();
v=read();
w=read();
add(u,v,w);
}
Dijkstra(s);
;i <= n;i++)
cout<<dis[i]<<" ";
;
}
最短路问题--P4779 单源最短路(标准版)Dijkstra堆优化的更多相关文章
- 单源最短路模板_SPFA_Dijkstra(堆优化)_C++
随手一打就是标准的SPFA,默认1号节点为出发点,当然不用 f 判断是否在队里也可以,只是这样更优化一点 void spfa() { int i,x,k; ;i<=n;i++) { d[i]=o ...
- 洛谷 P4779 单源最短路径(标准版) 题解
题面 这道题就是标准的堆优化dijkstra: 注意堆优化的dijkstra在出队时判断vis,而不是在更新时判断vis #include <bits/stdc++.h> using na ...
- P4779 【模板】单源最短路径(标准版)
P4779 [模板]单源最短路径(标准版) 求单源最短路, 输出距离 Solution \(nlogn\) 堆优化 \(Djs\) Code #include<iostream> #inc ...
- 洛谷 P4779【模板】单源最短路径(标准版)
洛谷 P4779[模板]单源最短路径(标准版) 题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 10 ...
- 洛谷 P4779 【模板】单源最短路径(标准版) 题解
P4779 [模板]单源最短路径(标准版) 题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 100 ...
- 单源最短路模板(dijkstra)
单源最短路(dijkstra算法及堆优化) 弱化版题目链接 n^2 dijkstra模板 #include<iostream> #include<cstdio> #includ ...
- P4779 【模板】单源最短路径(标准版)题解
原题链接 https://www.luogu.org/problemnew/show/P4779 若还未食用弱化版的同学请先做这个qwq https://www.luogu.org/problemne ...
- P4779 【模板】单源最短路径(标准版)单源最短路Dijkstra
题目描述 给定一个$n$个点,$m$条有向边的带非负权图,请你计算从$s$出发,到每个点的距离. 数据保证你能从$s$出发到任意点. 输入格式 第一行为三个正整数$n,m,s$. 第二行起$m$行,每 ...
- dijkstra P4779 【模板】单源最短路径(标准版) 洛谷luogu
题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 100→60 Ag→Cu 最终,他因此没能与理想的大 ...
随机推荐
- angularJS MVC及$scope作用域
- sql语句中 and 与or 的优先级
- 看完本文,Essay写作再也不需要凑字数
很多同学都说过自己写论文的时候出现“词穷”的情况,无奈只能靠“胡编乱造”来凑字数写出开头段,这其实是大家的阅读量没有达到要求.但不能因为出现这种情况就对自己的论文不负责任,否则你的论文分数可能就不会对 ...
- 吴裕雄--天生自然java开发常用类库学习笔记:正则表达式
public class RegexDemo01{ public static void main(String args[]){ String str = "1234567890" ...
- scan port
$sudo apt-get install nmap $nmap 127.0.0.1 Starting Nmap 7.60 ( https://nmap.org ) at 2020-02-20 15: ...
- Golang的进制转换实战案例
Golang的进制转换实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用进制概述 1>.进制概述 进制也就是进位制,是人们规定的一种进位方法.举个例子:二进制就 ...
- bzoj 2451 Uyuw's Concert
裸的半平面交.感觉这些东西,纯属在考代码能力啊.. #include<cstdio> #include<algorithm> #include<cmath> #de ...
- Python函数(三)
递归函数 在函数内部,可以调用其他函数,如果一个函数的内部调用了自己本身,那么这个函数就是递归函数. 什么?函数还可以自己调用自己?那不是一个死循环吗?请看下例: # 求1-100的和 def sum ...
- Spring-IOC(基于注解)
1.Spring 的 Bean 管理:(注解方式) 1.1 创建 web 项目,引入 Spring 的开发包: 注:在 Spring 的注解的 AOP 中需要引入 spring-aop 的 jar 包 ...
- docker image ubuntu12.04 安装软件源
docker 容器为ubuntu12.04 ,无法添加软件源. apt-get install python-software-properties software-properties-commo ...