#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N=1e5+10; //数据较大时
int dis[N],h[N],e[N],w[N],ne[N],idx; //用邻接表存储
bool st[N];
int n,m,q;
int hh,ww;
typedef pair<int,int> PII;
void add(int a,int b,int x)
{
e[idx]=b,w[idx]=x,ne[idx]=h[a],h[a]=idx++;
}
int find()
{
memset(dis,0x3f,sizeof dis);
dis[1]=0;
memset(st,0,sizeof st); //st必须初始化为0,否则if过不去
priority_queue<PII,vector<PII>,greater<PII> > heap; //优先队列+堆
heap.push({0,1}); //距离,节点
while(heap.size())
{
PII t=heap.top();
heap.pop();
int dian=t.second,di=t.first;
if(st[dian])continue; //如果该点已经是最短 就跳过 (存在多边情况是,链表存储可能又回到这个点)
st[dian]=true;
for(int i=h[dian];i!=-1;i=ne[i])
{
//cout<<"hh";
int j=e[i]; //i只是下表,e存储下标中对应的点
if(dis[j]>di+w[i]) //更新
{
dis[j]=di+w[i];
heap.push({dis[j],j});
}
}
}
return dis[n];
}
int main(void)
{
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--)
{
int a,b,x;
cin>>a>>b>>x;
add(a,b,x);
}
cout<<find();
return 0;
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复