[知识点]-[最短路]

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e5+10;

int n,m,A;
int dis[N];
int vis[N];

vector<pair<int,int> > e[N];

void spfa()
{
	for(int i=1;i<=n;i++) dis[i] = 1e18;
	queue<int > q;
	q.push(A);
	dis[A] = 0;
	vis[A] = 1;
	while(q.size())
	{
		int now = q.front();
		q.pop();vis[now] = 0;
		for(auto t:e[now])
		{
			int spot = t.first,w = t.second;
			if(dis[spot]>dis[now]+w)
			{
				dis[spot] = dis[now]+w;
				if(vis[spot]==0)
				{
					vis[spot] = 1;
					q.push(spot);
				}
			}
		}
	}	
}

signed main()
{
	cin>>n>>m>>A;
	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		e[a].push_back({b,c});
		e[b].push_back({a,c});
	}
	spfa();
	
	for(int i=1;i<=n;i++)
	{
		if(dis[i]==1e18) cout<<(1ll<<31)-1;
		else cout<<dis[i]<<" ";
	}
}
相关推荐
码途行者4 分钟前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK2 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
风落青芜4 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z4 小时前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode
h_a_o777oah5 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
草履虫AI课5 小时前
强化学习从零到一 07|重要性采样:借别人的数据,算自己的期望
算法·强化学习
AAA修煤气灶刘哥6 小时前
明明输入了完整报错,知识库怎么净给我相似答案?
人工智能·后端·算法
凌冰_7 小时前
数据结构 - 渐进符号和递归的复杂度(二)
数据结构·算法·软件工程
Highcharts.js8 小时前
Highcharts 在商业智能中的实战应用:释放数据洞察
前端·人工智能·算法·信息可视化·数据分析·数据可视化·highcharts
漂流瓶jz15 小时前
【AI】大模型LoRA微调原理与实验:基于CMRC2018与Qwen3-0.6B训练实测
人工智能·算法·llm