单源最短路径

#include<iostream>

#include<vector>

#include<queue>

#include <climits>

using namespace std;

const int N=1e5+10;

long long dist[N];

typedef pair<int,int> PII;

vector<PII>graph[N];

priority_queue<PII,vector<PII>,greater<PII>>pq;

int n,m,s;

void dijkstra(){

for(int i=0;i<=n;++i)dist[i]=INT_MAX;

vector<bool>st(n,false);

dist[s]=0;

pq.push({0,s});

while(pq.size()){

auto e=pq.top();pq.pop();

int w=e.first,u=e.second;

if(st[u])continue;

st[u]=true;

for(auto &p:graph[u]){

int ww=p.second,v=p.first;

if(!st[v]&&dist[v]>dist[u]+ww){

dist[v]=dist[u]+ww;

pq.push({dist[v],v});

}

}

}

}

int main(){

cin>>n>>m>>s;

for(int i=0;i<m;++i){

int u,v,w;cin>>u>>v>>w;

graph[u].push_back({v,w});

}

dijkstra();

for(int i=1;i<=n;++i)cout<<dist[i]<<" ";

return 0;

}

相关推荐
磊 子3 分钟前
多态类原理+四种类型转换+异常处理
开发语言·c++·算法
王老师青少年编程7 分钟前
csp信奥赛C++高频考点专项训练之字符串 --【回文字符串】:回文拼接
c++·字符串·csp·高频考点·信奥赛·字符串回文·回文拼接
染指11101 小时前
3.AI大模型-token是什么-大模型底层运行机制
人工智能·算法·机器学习
谙弆悕博士2 小时前
快速学C语言——第19章:C语言常用开发库
c语言·开发语言·算法·业界资讯·常用函数
光影少年2 小时前
前端算法题
前端·javascript·算法
南宫萧幕2 小时前
基于 Simulink 与 Python 联合仿真的 eVTOL 强化学习全链路实战
开发语言·人工智能·python·算法·机器学习·控制
电魂泡哥2 小时前
CMS垃圾回收
java·jvm·算法
Teleger2 小时前
在window上使用c++控制鼠标点击,实现的exe
c++·单片机·计算机外设
hkj88082 小时前
CRC-512算法输出64字节
算法