#include<bits/stdc++.h> using namespace std; int n,m,s; bool vis[10005]; int INF=0x3f3f3f3f; int d[10005]; struct node{ int v,w; }; vector<node> G[1005]; struct hn{ int u,dist; hn(int _u,int _dist){ u=_u; dist=_dist; } }; struct cmp{ bool operator()(hn a,hn b){ return a.dist>b.dist; } }; priority_queue<hn,vector<hn>,cmp>q; void dijkstra(){ memset(vis,false,sizeof(vis)); memset(d,INF,sizeof(d)); d[s]=0; q.push(hn(s,0)); while(q.size()){ int x=q.top().u; q.pop(); vis[x]=true; for(int i=0; i<G[x].size(); i++){ int y=G[x][i].v; int z=G[x][i].w; if(!vis[y] && d[x]+z<d[y]){ d[y]=d[x]+z; q.push(hn(y,d[y])); } } } } int main(){ cin>>n>>m>>s; for(int i=1; i<=m; i++){ int xx,yy,zz; cin>>xx>>yy>>zz; G[xx].push_back({yy,zz}); G[yy].push_back({xx,zz}); } dijkstra(); for(int i=1; i<=n; i++){ cout<<d[i]<<" "; } cout<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; int n,m,s,t; bool vis[100005]; int INF=0x3f3f3f3f; int d[100005]; struct node{ int v,w; }; vector<node> G[10005]; struct hn{ int u,dist; hn(int _u,int _dist){ u=_u; dist=_dist; } }; struct cmp{ bool operator()(hn a,hn b){ return a.dist>b.dist; } }; priority_queue<hn,vector<hn>,cmp>q; void dijkstra(){ memset(vis,false,sizeof(vis)); memset(d,INF,sizeof(d)); d[s]=0; q.push(hn(s,0)); while(q.size()){ int x=q.top().u; q.pop(); vis[x]=true; for(int i=0; i<G[x].size(); i++){ int y=G[x][i].v; int z=G[x][i].w; if(!vis[y] && d[x]+z<d[y]){ d[y]=d[x]+z; q.push(hn(y,d[y])); } } } } int main(){ cin>>n>>m>>s>>t; for(int i=1; i<=m; i++){ int xx,yy,zz; cin>>xx>>yy>>zz; G[xx].push_back({yy,zz}); G[yy].push_back({xx,zz}); } dijkstra(); cout<<d[t]<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; int n,m,s; bool vis[10005]; int INF=0x3f3f3f3f; int d[10005]; struct node{ int v,w; }; vector<node> G[1005]; struct hn{ int u,dist; hn(int _u,int _dist){ u=_u; dist=_dist; } }; struct cmp{ bool operator()(hn a,hn b){ return a.dist>b.dist; } }; priority_queue<hn,vector<hn>,cmp>q; void dijkstra(){ memset(vis,false,sizeof(vis)); memset(d,INF,sizeof(d)); d[1]=0; q.push(hn(1,0)); while(q.size()){ int x=q.top().u; q.pop(); vis[x]=true; for(int i=0; i<G[x].size(); i++){ int y=G[x][i].v; int z=G[x][i].w; if(!vis[y] && d[x]+z<d[y]){ d[y]=d[x]+z; q.push(hn(y,d[y])); } } } } int main(){ cin>>n>>m; for(int i=1; i<=m; i++){ int xx,yy,zz; cin>>xx>>yy>>zz; G[xx].push_back({yy,zz}); G[yy].push_back({xx,zz}); } int ans=INT_MIN; dijkstra(); for(int i=1; i<=n; i++){ ans=max(ans,d[i]); } cout<<ans<<endl; return 0; }
标签:dist,struct,int,vis,dijkstra,hn,zz From: https://www.cnblogs.com/dxy09tj/p/17606479.html