#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; using ll = long long ; const int M=1e5+10,N=M; struct node{ int next,to,val,id; }e[M<<1]; int head[M<<1],kk; void add(int from,int to,int val,int id){ e[++kk]={head[from],to,val,id}; head[from]=kk; } ll dis[N]; struct nod{ int x,dis; bool operator<(const nod&p)const{ return dis>p.dis; } }; int n,m,k; void dij(int s){ memset(dis,0x3f,sizeof dis); dis[s]=0; priority_queue<nod>q; q.push({s,dis[s]}); while(!q.empty()){ auto t=q.top();q.pop(); for(int i=head[t.x];i;i=e[i].next){ int x=e[i].to,y=e[i].val; if(dis[x]>dis[t.x]+y){ dis[x]=dis[t.x]+y; q.push({x,dis[x]}); } } } } bool vis[N]; vector<int>ans; void dfs(int s){ vis[s]=true; for(int i=head[s];i;i=e[i].next){ int x=e[i].to; if(!vis[x]&&dis[x]==dis[s]+e[i].val){ if(ans.size()<k)ans.push_back(e[i].id); vis[x]=true; dfs(x); } } } int main() { ios::sync_with_stdio(false); cin>>n>>m>>k; for(int i=1,u,v,val;i<=m;i++){ cin>>u>>v>>val; add(u,v,val,i); add(v,u,val,i); } dij(1); dfs(1); cout<<ans.size()<<"\n"; for(auto &t:ans) cout<<t<<" "; }
原文:https://www.cnblogs.com/qq1415584788/p/14856078.html