1 条题解
-
0
dijkstra最短路堆优化
#include<bits/stdc++.h> #define int long long using namespace std; int head[555555],n,m,tot,vis[555555],dis[555555]; struct E{int next,to,d;}edge[555555]; typedef pair<int,int> P; void add(int u,int v,int w) { edge[++tot].next=head[u]; edge[tot].to=v; edge[tot].d=w; head[u]=tot; } void dijkstra() { priority_queue<P,vector<P>,greater<P> >q; q.push(make_pair(0,1));//first:距离 second:点编号 while(!q.empty()) { P x=q.top(); q.pop(); int a=x.first,b=x.second; if(vis[b])continue; vis[b]=1; for(int i=head[b];i;i=edge[i].next) { int j=edge[i].to; if(dis[j]>a+edge[i].d) {dis[j]=a+edge[i].d;q.push({dis[j],j});} } } } signed main() { cin>>n>>m; for(int i=1;i<=m;i++) { int u,v,w; cin>>u>>v>>w; add(u,v,w); } memset(dis,0x3f,sizeof(dis)); dis[1]=0; dijkstra(); for(int i=1;i<=n;i++) { if(dis[i]==0x3f3f3f3f3f3f3f3f)cout<<-1<<" "; else cout<<dis[i]<<" "; } return 0; }
- 1
信息
- ID
- 6815
- 时间
- 2000ms
- 内存
- 512MiB
- 难度
- 3
- 标签
- (无)
- 递交数
- 4
- 已通过
- 4
- 上传者