黑鼻子小红 @ 2023-03-10 13:27:40
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int,int> PII;
const int N = 100010,M=N*5;
int n,m,s;
int h[N],e[M],ne[M],w[M],idx;
int dist[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void dijkstra()
{
dist[1]=0;
priority_queue<PII,vector<PII>,greater<PII> >heap;
heap.push({0,1});
while(heap.size())
{
auto t=heap.top();
heap.pop();
if(st[t.second]) continue;
st[t.second]=true;
for(int i=h[t.second];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t.second]+w[i]){
dist[j]=dist[t.second]+w[i];
heap.push({dist[j],j});
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
memset(h,-1,sizeof h);
memset(dist,0x3f,sizeof dist);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
dijkstra();
for(int i=1;i<=n;i++)
{
printf("%d ",dist[i]);
}
return 0;
}