1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int MAXM=200005; const int MAXN=100005; const int INF=2147483647; struct edge{ int next; int to; ll w; }; edge g[MAXM]; int head[MAXN],nume=-1; int n,m,s; inline void adde(int f,int t,int w){ g[++nume].next=head[f]; g[nume].to=t; g[nume].w=w; head[f]=nume; }
struct node{ int no; ll dist; node(){} node(int x,ll y){ no = x; dist = y; } bool operator <(const node &s)const{ return s.dist<dist; } bool operator >(const node &s)const{ return s.dist>dist; } };
node q[MAXN]; int siz=0;
void push(node x){ q[++siz]=x; int now=siz; while(now){ int nxt=now>>1; if(q[nxt]<q[now])swap(q[nxt],q[now]); else break; now=nxt; } return; }
void pop(){ swap(q[siz],q[1]);siz--; int now=1; while((now<<1)<=siz){ int nxt=now<<1; if(nxt+1<=siz&&q[nxt+1]>q[nxt])nxt++; if(q[nxt]>q[now])swap(q[now],q[nxt]); else break; now=nxt; } }
node top(){ return q[1]; }
ll dist[MAXN]; bool vis[MAXN]={0}; void dij(){ int i,j; node now; for(i=1;i<=n;i++) dist[i]=INF; dist[s]=0; push(node(s,0)); while(siz>0){ now=top(); pop(); if(!vis[now.no]){ vis[now.no]=1; for(i=head[now.no];i!=-1;i=g[i].next) if(g[i].w+now.dist<dist[g[i].to]){ dist[g[i].to]=g[i].w+now.dist; push(node(g[i].to,dist[g[i].to])); } } } }
int main(){ memset(head,-1,sizeof(head)); scanf("%d%d%d",&n,&m,&s); int x,y,z; for(int i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); adde(x,y,z); } dij(); for(int i=1;i<=n;i++) printf("%lld ",dist[i]); return 0; }
|