比赛 |
板子大赛 |
评测结果 |
AAAAAA |
题目名称 |
单源最短路 |
最终得分 |
100 |
用户昵称 |
AeeE5x |
运行时间 |
0.019 s |
代码语言 |
C++ |
内存使用 |
3.47 MiB |
提交时间 |
2025-01-22 10:58:11 |
显示代码纯文本
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n,m,s;
struct edge{int to,nxt,val;}e[1010];int head[110],cnt=1;
inline void add(int x,int y,int w){e[cnt]=(edge){y,head[x],w},head[x]=cnt++;}
int d[110];
bool vis[110];
struct node{
int x,val;
bool operator<(const node&qq)const{return val>qq.val;}
};
priority_queue<node> q;
void dij(int u){
memset(d,0x3f,sizeof d);
d[u]=0;
q.push((node){u,0});
while(!q.empty()){
node fr=q.top();
q.pop();
int x=fr.x;
if(vis[x]) continue;
vis[x]=1;
for(int i=head[x];i;i=e[i].nxt){
int y=e[i].to;
if(d[y]>d[x]+e[i].val){
d[y]=d[x]+e[i].val;
q.push((node){y,d[y]});
}
}
}
}
int main(){
freopen("path.in","r",stdin);
freopen("path.out","w",stdout);
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=m;i++){
int a,b,c;scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dij(s);
for(int i=1;i<=n;i++) printf("%d ",d[i]);
return 0;
}