数据有锅 n,m<=2e5
解题报告:
建立二维最短路dis[i][j],表示到达i景点用了j次观光车加速的最短时间花费。
#define first f
#define second s
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define sl(p) strlen(p)
#define SZ(p) p.size()
#define pii pair<int,int>
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define fast_cin ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
const int MOD=1e9+7;
const int maxn=2e5+5;
const double eps=1e-9;
const ll inf=2e18;
const double PI=acos(-1.0);
const double e=2.718281828459;
struct node
{
int to,next,w;
}edge[maxn<<1];
int head[maxn<<1],cnt,n,m,k;
ll dis[maxn][10];
struct Node
{
int to,k;
ll cost;
bool operator <(const Node &ls)const {return cost>ls.cost;}
};
void add(int from,int to,int w)
{
edge[++cnt].next=head[from];
edge[cnt].to=to;
edge[cnt].w=w;
head[from]=cnt;
}
void dij_queue()
{
dis[1][0]=0;
priority_queue<Node>q;
q.push({1,0,0});
Node t,z;
while(!q.empty()){
t=q.top();q.pop();
if(t.cost>dis[t.to][t.k]){continue;}
for(int i=head[t.to];~i;i=edge[i].next){
int to=edge[i].to,w=edge[i].w;
if(t.cost+w<dis[to][t.k]){//表示不采用观光车加速
dis[to][t.k]=t.cost+w;
q.push({to,t.k,dis[to][t.k]});
}
if(t.k<k&&t.cost+1<dis[to][t.k+1]){//采用观光车加速
dis[to][t.k+1]=t.cost+1;
q.push({to,t.k+1,dis[to][t.k+1]});
}
}
}
}
int main()
{
mem(head,-1);
int u,v,w;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
for(int i=1;i<=n;i++){
for(int j=0;j<=k;j++){
dis[i][j]=inf;
}
}
dij_queue();
ll ans=inf;
for(int i=0;i<=k;i++){
ans=min(ans,dis[n][i]);
}
if(ans==inf){printf("-1\n");}
else{printf("%lld\n",ans);}
return 0;
}