1 #include <iostream> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 struct name 7 { 8 int end; 9 int l; 10 }; 11 bool vis[100020]; 12 int n,m,s; 13 long long dis[100020]; 14 vector <struct name> v[100020]; 15 struct node 16 { 17 int distance; 18 int pn; 19 friend bool operator <(const node a,const node b) 20 { 21 return a.distance>b.distance; 22 } 23 }; 24 priority_queue <struct node> q; 25 void dijstra() 26 { 27 struct node now; 28 now.distance=0; 29 now.pn=s; 30 dis[s]=0; 31 q.push(now); 32 while(!q.empty()) 33 { 34 int x=q.top().pn; 35 q.pop(); 36 if(vis[x]) continue; 37 vis[x]=true; 38 for(int i=0;i<v[x].size();i++) 39 { 40 if(vis[v[x][i].end]) continue; 41 int next=v[x][i].end; 42 if(dis[next]>dis[x]+v[x][i].l) 43 { 44 dis[next]=dis[x]+v[x][i].l; 45 struct node nextt; 46 nextt.distance=dis[next]; 47 nextt.pn=next; 48 q.push(nextt); 49 } 50 } 51 } 52 for(int i=1;i<=n;i++) 53 { 54 cout<<dis[i]<<" "; 55 } 56 return ; 57 } 58 int main () 59 { 60 61 cin>>n>>m>>s; 62 memset(dis,0x3f3f3f3f,sizeof(dis)); 63 for(int i=1;i<=m;i++) 64 { 65 int x,y,z; 66 cin>>x>>y>>z; 67 struct name now; 68 now.end=y; 69 now.l=z; 70 v[x].push_back(now); 71 } 72 dijstra(); 73 return 0; 74 }