#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;void print(int x) {
if(x<0) {
putchar('-');
x=-x;
}
if(x>9)
print(x/10);
putchar(x%10+'0');
}
void read(int &x) {
int f=1;
x=0;
char s=getchar();
while(s<'0'||s>'9') {
if(s=='-')f=-1;
s=getchar();
}
while(s>='0'&&s<='9') {
x=x*10+s-'0';
s=getchar();
}
x*=f;
}
vector<int> to[10001], dis[10001];
queue<int> now;
int f[10001];
int main()
{
int n, m, s;
read(n), read(m), read(s);
for(register int i=0; i<=n; ++i) f[i]=0x7fffffff;
f[s]=0;
now.push(s);
for(register int i=0; i<m; ++i)
{
int a, b, c;
read(a), read(b), read(c);
to[a].push_back(b);
dis[a].push_back(c);
}
do
{
int tmp=now.front();
now.pop();
for(register int i=0; i<to[tmp].size(); ++i)
{
if(f[tmp]+dis[tmp][i]<f[to[tmp][i]])
{
f[to[tmp][i]]=f[tmp]+dis[tmp][i];
now.push(to[tmp][i]);
}
}
}while(!now.empty());
for(register int i=1; i<=n; ++i)
{
print(f[i]);
putchar(' ');
}
return 0;
}```
SPFA(模板)
最新推荐文章于 2024-02-08 01:16:35 发布