JLOI2011 飞行路线

传送门

分层图\(SPFA\)……听起来好高级的样子……嗯,所以我选分层图\(\rm{dijkstra}\)

o_QQ%e6%88%aa%e5%9b%be20180801193256.png

这题好像是会卡\(SPFA\),要加一个玄学\(SLF\)优化才可以……

所以还是用堆优化\(\rm{dijkstra}\)吧,多好o_QQ%e5%9b%be%e7%89%8720180623180015.png

以上全是瞎扯,但卡\(SPFA\)真的


\(\tt{dis[i][j]}\)表示免费乘坐了\(i\)次到达点\(j\)的最小代价,跑最短路的时候顺推下来就可以了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int read(){
    int k=0; char c=getchar();
    for(;c<'0'||c>'9';) c=getchar();
    for(;c>='0'&&c<='9';c=getchar())
      k=(k<<3)+(k<<1)+c-48;
    return k;
}
struct zzz{
    int t,len,nex;
}e[50010<<1]; int head[10010],tot;
void add(int x,int y,int z){
    e[++tot].t=y;
    e[tot].len=z;
    e[tot].nex=head[x];
    head[x]=tot;
}
int dis[21][10010];
struct hhh{
    int v,cnt,pos;
    bool operator < (const hhh &y) const{
        return v > y.v;
    }
};
priority_queue <hhh> q;
void dijkstra(int s,int num){
    memset(dis,127,sizeof(dis)); dis[0][s]=0;
    q.push(hhh{0,0,s});
    while(!q.empty()){
        hhh k=q.top(); q.pop();
        if(dis[k.cnt][k.pos]!=k.v) continue;
        for(int i=head[k.pos];i;i=e[i].nex){
            int to=e[i].t;
            if(dis[k.cnt][to]>dis[k.cnt][k.pos]+e[i].len){
                dis[k.cnt][to]=dis[k.cnt][k.pos]+e[i].len;
                q.push(hhh{dis[k.cnt][to],k.cnt,to});
            }
            if(k.cnt+1<=num&&dis[k.cnt][k.pos]<dis[k.cnt+1][to]){
                dis[k.cnt+1][to]=dis[k.cnt][k.pos];
                q.push(hhh{dis[k.cnt+1][to],k.cnt+1,to});
            }
        }
    }
}
int main(){
    int 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);
    }
    dijkstra(1,k);
    cout<<dis[k][n];
    
    return 0;
}

转载于:https://www.cnblogs.com/wxl-Ezio/p/9403601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值