西湖奇遇记Ⅰ( 最短路 )

题目链接

在这里插入图片描述
数据有锅 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值