标签:mes ref lan 模板 cheng article pac bit oid
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const int maxn=1e5+5; 5 const ll inf=1e17; 6 struct node 7 { 8 ll dis; 9 int num,pos; 10 node() {} 11 node(ll dis,int num,int pos):dis(dis),num(num),pos(pos) {} 12 bool operator< (const node& a)const 13 { 14 return dis>a.dis; 15 } 16 }; 17 struct edge 18 { 19 int to,next; 20 ll z; 21 } e[maxn*2];//双边,无向图,所以乘以2 22 int head[maxn],cnt; 23 void add(int x,int y,ll w)// w边值 24 { 25 e[cnt].to=y; 26 e[cnt].z=w; 27 e[cnt].next=head[x]; 28 head[x]=cnt++; 29 } 30 ll dist[maxn][12],vis[maxn][12]; 31 32 int n,m,k; 33 void dijkstra() 34 { 35 priority_queue<node>Q;//优先队列 36 Q.push(node(0,0,1)); 37 while(!Q.empty()) 38 { 39 node v=Q.top(); 40 Q.pop(); 41 if(vis[v.pos][v.num]) 42 continue; 43 vis[v.pos][v.num]=1; 44 for(int i=head[v.pos]; ~i; i=e[i].next) 45 { 46 int ne=e[i].to; 47 if(dist[ne][v.num]>v.dis+e[i].z) 48 { 49 dist[ne][v.num]=v.dis+e[i].z; 50 Q.push(node(dist[ne][v.num],v.num,ne)); 51 } 52 if(v.num<k && v.dis<dist[ne][v.num+1]) 53 { 54 dist[ne][v.num+1]=v.dis; 55 Q.push(node(v.dis,v.num+1,ne)); 56 } 57 } 58 } 59 } 60 void init()//初始化图,有点之间距离为无穷,每个点的标志初始为0,边的条数cnt初始为0 61 { 62 for(int i=0; i<maxn; i++) 63 { 64 head[i]=-1; 65 for(int j=0; j<=10; j++) 66 dist[i][j]=inf,vis[i][j]=0; 67 } 68 cnt=0; 69 } 70 int main() 71 { 72 int t; 73 scanf("%d",&t); 74 while(t--) 75 { 76 scanf("%d%d%d",&n,&m,&k); 77 int x,y; 78 ll z; 79 init(); 80 for(int i=1; i<=m; i++) 81 { 82 scanf("%d%d%lld",&x,&y,&z); 83 add(x,y,z); 84 } 85 dist[1][0]=0; 86 dijkstra(); 87 ll ans=inf; 88 for(int i=0; i<=k; i++) 89 ans=min(ans,dist[n][i]); 90 printf("%lld\n",ans); 91 92 } 93 return 0; 94 }
参看原博客:https://blog.csdn.net/tianyizhicheng/article/month/2018/09
标签:mes ref lan 模板 cheng article pac bit oid
原文地址:https://www.cnblogs.com/weixq351/p/9581666.html