[模板]单源最短路 spfa版 会T三个点
作者:
多米尼克領主的致意
,
2024-05-07 19:07:20
,
所有人可见
,
阅读 3
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
//typedef pair<int, int>pii;
int h[N], idx;
int n, m, s;
struct edge{
int from, to, w;
int ne;
}E[M];
int dist[N];
bool st[N];
void add(int a, int b, int c)
{
E[idx].to = b;
E[idx].w = c;
E[idx].ne = h[a];
h[a] = idx++;
}
void spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
queue<int>q;
q.push(s);
st[s] = true;
while(q.size())
{
auto no = q.front();
q.pop();
st[no] = false;
for(int i = h[no];~i;i = E[i].ne)
{
int j = E[i].to;
if(dist[j] > dist[no] + E[i].w)
{
dist[j] = dist[no] + E[i].w;
if(!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(h, -1, sizeof h);
cin >> n >> m >> s;
for(int i = 1;i <= m;i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
spfa();
for(int i = 1;i <= n;i++)cout << dist[i] << " ";
return 0;
}