【模板】分层图
作者:互联网
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #define INF 0x3f3f3f3f using namespace std; int dis[10000001],vis[10000001],head[10000001],n,m,k,N; struct Node{ int nxt,to,edge; }a[10000001]; struct node{ int id,Dis; friend bool operator < (node AA,node BB){ if(AA.Dis==BB.Dis) return AA.id>BB.id; return AA.Dis>BB.Dis; } }; int add(int U,int V,int VAL){ a[++N].to=V; a[N].edge=VAL; a[N].nxt=head[U]; head[U]=N; return 0; } int Min(int AAA,int BBB){ return AAA<BBB ? AAA : BBB ; } inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ s=(s<<1)+(s<<3)+(ch^48); ch=getchar(); } return s*w; } int dijiesitela(){ memset(dis,INF,sizeof(dis)); priority_queue<node> q; node useless; useless.id=1; useless.Dis=0; q.push(useless); dis[1]=0; while(!q.empty()){ node u=q.top(); q.pop(); if(vis[u.id]==1) continue; vis[u.id]=1; for(int i=head[u.id];i;i=a[i].nxt){ int v=a[i].to; if(vis[v]==0&&dis[u.id]+a[i].edge<dis[v]){ dis[v]=dis[u.id]+a[i].edge; node vv; vv.id=v; vv.Dis=dis[v]; q.push(vv); } } } int minn=INF; for(int i=0;i<=k;i++) minn=Min(minn,dis[n+i*n]); return minn; } int main(){ n=read(); m=read(); k=read(); for(int i=1;i<=m;i++){ int X=read(),Y=read(),Z=read(); add(X,Y,Z); add(Y,X,Z); for(int j=1;j<=k;j++){ add(X+(j-1)*n,Y+j*n,Z/2); add(Y+(j-1)*n,X+j*n,Z/2); add(X+j*n,Y+j*n,Z); add(Y+j*n,X+j*n,Z); } } printf("%d",dijiesitela()); return 0; }
标签:node,AA,10000001,int,分层,id,模板,Dis 来源: https://www.cnblogs.com/latent-Lin/p/15526018.html