紧急救援
作者:互联网
题目详情 - L2-001 紧急救援 (25 分) (pintia.cn)
#include<iostream> #include<cstring> #include<queue> using namespace std; typedef pair<int,int> PAII; int n,m,A,B; const int N=550,M=N*N; int h[N],e[M],ne[M],w[M],dist[N],pre[N],idx,s[N],cnt[N],sum[N]; bool st[N]; void add(int a,int b,int c) { e[idx]=b; w[idx]=c; ne[idx]=h[a]; h[a]=idx++; } void dijkstra(){ priority_queue<PAII,vector<PAII>,greater<PAII> > heap; memset(dist,0x3f,sizeof(dist)); dist[A]=0; pre[A]=-1; cnt[A]=1; sum[A]=s[A]; heap.push({0,A});//距离和点 while(heap.size()) { auto t=heap.top(); heap.pop(); int dis=t.first,ver=t.second; if(st[ver]) continue; else st[ver]=true; for(int i=h[ver];i!=-1;i=ne[i]) { int j=e[i]; if(dist[j]>dist[ver]+w[i]) { dist[j]=dist[ver]+w[i]; cnt[j]=cnt[ver]; pre[j]=ver; sum[j]=sum[ver]+s[j]; heap.push({dist[j],j}); } else if(dist[j]==dist[ver]+w[i]) { cnt[j]+=cnt[ver]; if(sum[j]<sum[ver]+s[j]) { sum[j]=sum[ver]+s[j]; pre[j]=ver; } } } } } int main(){ cin>>n>>m>>A>>B; memset(h,-1,sizeof(h)); for(int i=0;i<n;i++) cin>>s[i]; while(m--) { int a,b,c; cin>>a>>b>>c; add(a,b,c); add(b,a,c); } dijkstra(); cout<<cnt[B]<<" "<<sum[B]<<endl; deque<int> vis; int x=B; while(x!=-1) { vis.push_back(x); x=pre[x]; } cout<<vis[vis.size()-1]; for(int i=vis.size()-2;i>=0;i--) cout<<" "<<vis[i]; }
标签:cnt,ver,救援,int,sum,紧急,heap,dist 来源: https://www.cnblogs.com/ccwz7/p/16172385.html