标签:jks mes continue operator inf ace ios struct cin
1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 const int INF=0x3f3f3f; 5 const int m=200005; 6 const int n=100005; 7 8 int head[n],cnt; 9 10 struct edge{ 11 int to,next,w; 12 }e[m*2]; 13 14 void add(int u,int v,int w){ 15 cnt++; 16 e[cnt].to=v; 17 e[cnt].next=head[u]; 18 e[cnt].w=w; 19 head[u]=cnt; 20 } 21 22 struct node{ 23 int u,d; 24 bool operator < (const node &rhs) const{ 25 return d>rhs.d; 26 } 27 }; 28 29 int dis[n]; 30 31 void dijkstra(int s){ 32 for(int i=1;i<=n;i++) 33 { 34 dis[i]=INF; 35 } 36 dis[s]=0; 37 priority_queue<node> q; 38 q.push((node){s,dis[s]}); 39 int v,w; 40 while(!q.empty()){ 41 node x=q.top(); 42 q.pop(); 43 int u=x.u; 44 if(x.d!=dis[u])//vis 45 { 46 continue; 47 } 48 for(int i=head[u];i;i=e[i].next) 49 { 50 w=e[i].w; 51 v=e[i].to; 52 if(dis[u]+w<dis[v]) 53 { 54 dis[v]=dis[u]+w; 55 q.push((node){v,dis[v]}); 56 } 57 } 58 } 59 } 60 61 int main(){ 62 int u,v,w; 63 int n,m,s; 64 cin>>n>>m>>s; 65 for(int i=1;i<=m;i++) 66 { 67 cin>>u>>v>>w; 68 add(u,v,w); 69 } 70 dijkstra(s); 71 for(int i=1;i<=n;i++) 72 { 73 cout<<dis[i]<<" "; 74 } 75 return 0; 76 }
标签:jks mes continue operator inf ace ios struct cin
原文地址:https://www.cnblogs.com/kohano/p/11622738.html