yuanshuu @ 2024-08-08 17:06:47
#include <bits/stdc++.h>
using namespace std;
int head[100004];
struct node{
int to;
int w;
int nxt;
} edge[200005];
int n,m,s;
int d[100004];
bool visit[100004];
queue <int> que;
int main (){
memset(d,127,sizeof(d));
cin>>n>>m>>s;
d[s]=0;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
edge[i].to=v;
edge[i].w=w;
edge[i].nxt=head[u];
head[u]=i;
}
for(int u=1;u<=n;u++){
que.push(u);
visit[u]=true;
}
while(!que.empty()){
int u=que.front();
que.pop();
visit[u]=false;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
int w=edge[i].w;
if (d[u] + w < d[v]) {
d[v] = d[u] + w;
if (!visit[v]) {
que.push(v);
visit[v] = true;
}
}
}
}
for(int i=1;i<=n;i++){
cout<<d[i]<<" ";
}
return 0;
}
```cpp
谢谢大家
by CCCCOrz @ 2024-08-08 17:12:15
SPFA做这题TLE是正常且应当的
请使用Dijkstra
(((
by Mr_Terminator @ 2024-08-08 17:14:41
艹,不要在去年JT4 T掉15pts的我伤口上撒盐行不
by yuanshuu @ 2024-08-17 17:09:43
@CCCCOrz 知道了,谢谢
by zhoukun @ 2024-10-01 08:56:48
@yuanshuu 用堆优化