Kitsune @ 2022-03-02 23:51:23
#include <stdio.h>
#include <stdlib.h>
struct Edge {
int to, nxt, w;
} E[200005];
struct node {
int pos, val;
} heap[200005];
const int inf = 0x7fffffff;
int n, m, s, cnt, siz;
int head[100005], dis[100005];
bool vis[100005];
void addEdge(int x, int y, int w) {
E[++cnt].to = y;
E[cnt].w = w;
E[cnt].nxt = head[x];
head[x] = cnt;
return;
}
void h_in(int p, int wi) {
heap[++siz].val = wi;
heap[siz].pos = p;
int now = siz;
while(now > 1) {
int nt = now >> 1;
if(heap[nt].val < heap[now].val) {
node t = heap[nt];
heap[nt] = heap[now];
heap[now] = t;
} else break;
now = nt;
}
return;
}
void h_out() {
heap[1] = heap[siz--];
int now = 1;
while((now << 1) <= siz) {
int nt = now << 1;
if(heap[nt].val > heap[nt+1].val) nt++;
if(heap[now].val > heap[nt].val) {
node t = heap[nt];
heap[nt] = heap[now];
heap[now] = t;
}
now = nt;
}
return;
}
void dijkstra() {
h_in(s, 0);
while(siz > 0) {
int p = heap[1].pos;
h_out();
if(!vis[p]) {
vis[p] = 1;
for(int i = head[p]; i ;i = E[i].nxt) {
if(dis[E[i].to] > dis[p] + E[i].w) {
dis[E[i].to] = dis[p] + E[i].w;
if(!vis[E[i].to]) h_in(E[i].to, dis[E[i].to]);
}
}
}
}
return;
}
int main() {
int x, y, z;
scanf("%d %d %d",&n,&m,&s);
for(int i = 0;i < m;i++) {
scanf("%d %d %d",&x,&y,&z);
addEdge(x, y, z);
}
for(int i = 1;i <= n;i++) dis[i] = inf;
dis[s] = 0;
dijkstra();
for(int i = 1;i <= n;i++) {
printf("%d ",dis[i]);
}
return 0;
}