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
| #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #define ll long long #define maxn 1000001 #define pp pair<int,int> using namespace std; inline int read() { int l=0,w=1; char ch=0; while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { l=(l<<3)+(l<<1)+(ch^'0'); ch=getchar(); } return w*l; } int n,s,wi[maxn],to[maxn],Next[maxn],head[maxn],dis[maxn]; bool vis[maxn]; priority_queue<pp,vector<pp>,greater<pp> >q; int main() { int m,u,v,w,t=0; memset(head,-1,sizeof(head)); n=read(),m=read(),s=read(); for(int i=1;i<=n;i++) dis[i]=2147483647; for(int i=0;i<m;i++) { u=read(),v=read(),w=read(); wi[t]=w,to[t]=v,Next[t]=head[u],head[u]=t++; } dis[s]=0; q.push(make_pair(0,s)); while(!q.empty()) { pp u=q.top(); q.pop(); int x=u.second; if(vis[x]) continue; vis[x]=true; for(int i=head[x];i!=-1;i=Next[i]) if(dis[to[i]]>dis[x]+wi[i]) dis[to[i]]=dis[x]+wi[i],q.push(make_pair(dis[to[i]],to[i])); } for(int i=1;i<=n;i++) printf("%d ",dis[i]); return 0; }
|