比赛 |
板子大赛 |
评测结果 |
AAAAAA |
题目名称 |
单源最短路 |
最终得分 |
100 |
用户昵称 |
李金泽 |
运行时间 |
0.018 s |
代码语言 |
C++ |
内存使用 |
3.03 MiB |
提交时间 |
2025-01-22 14:59:12 |
显示代码纯文本
#include<cstdio>
#include<queue>
#define N 105
#define M 505
using namespace std;
int n,m,s,h[N],cnt,d[N],x,y,z;bool b[N];
struct node{int u,w;bool operator<(node y)const{return w>y.w;}};
struct edge{int v,n,w;}e[M<<1];
void ad(int u,int v,int w){e[++cnt]={v,h[u],w};h[u]=cnt;}
void dijkstra()
{
priority_queue<node>q;
q.push({s,0});
while(q.size())
{
node u=q.top();q.pop();
if(b[u.u])continue;
d[u.u]=u.w,b[u.u]=1;
for(int i=h[u.u];i;i=e[i].n)
{
int v=e[i].v,w=u.w+e[i].w;
if(b[v])continue;
q.push({v,w});
}
}
}
int main()
{
freopen("path.in","r",stdin);freopen("path.out","w",stdout);
scanf("%d%d%d",&n,&m,&s);
while(m--)scanf("%d%d%d",&x,&y,&z),ad(x,y,z),ad(y,x,z);
dijkstra();
for(int i=1;i<=n;i++)printf("%d ",d[i]);
return 0;
}