题意:在图上,有k次机会可以直接通过一条边,问起点与终点之间的最短路径。
题解:模板题,分成k+1层,每一层往下连一条单向边 讲解
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn= 1e3+5;
const ll INF =1e18;
struct edge{int to,cost;
edge(){}
edge(int v,int w){to=v;cost=w;}
};
typedef pair < int,int > P;
int n,m,s,t,k;
vector<edge>G[maxn*maxn];
ll d[maxn*maxn];
void dij(int s){
priority_queue<P,vector<P>,greater<P> >que;
fill(d,d+n*n+1,INF);
d[s]=0;
que.push(P(0,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(d[v]<p.first)continue;
for(int i=0;i<G[v].size();i++){
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}
int main(){
scanf("%d%d%d%d%d",&n,&m,&s,&t,&k);
for(int i=0;i<m;i++){
int u,v,l;
scanf("%d%d%d",&u,&v,&l);
for(int j=0;j<=k;j++){
G[u+j*n].push_back(edge(v+j*n,l));
G[v+j*n].push_back(edge(u+j*n,l));
if(j!=k){
G[u+j*n].push_back(edge(v+(j+1)*n,0));
G[v+j*n].push_back(edge(u+(j+1)*n,0));
}
}
}
dij(s);
ll ans=INF;
for(int i=0;i<=k;i++){
ans=min(ans,d[t+i*n]);
}
cout<<ans<<endl;
return 0;
}