AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

AcWing 178. 第K短路    原题链接    中等

作者: 作者的头像   Object_ ,  2019-10-25 21:52:19 ,  所有人可见 ,  阅读 1031


1


1

注意点:

  • 起点和终点相同时,k需要提前++.
  • Node内的比较函数应当使用:当前费用+预估费用.

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN=3e3,MAXM=3e5;
struct Edge_First{//反向图
    int from,to,nxt,w;
}e_First[MAXM];
int head_First[MAXN],edgeCnt_First=1;
void addEdge_First(int u,int v,int w){
    e_First[++edgeCnt_First].from=u;
    e_First[edgeCnt_First].to=v;
    e_First[edgeCnt_First].w=w;
    e_First[edgeCnt_First].nxt=head_First[u];
    head_First[u]=edgeCnt_First;
}
int s,t,k;//起点 终点 第k短路
struct Node_First{
    int v,dis;
    bool operator <(Node_First another)const{
        return dis>another.dis;
    }
};
int f[MAXN];//估价函数
void dijkstra(){
    memset(f,0x3f,sizeof(f));
    priority_queue<Node_First> q;
    q.push(Node_First{t,0});
    f[t]=0;
    while(!q.empty()){
        Node_First nowNode=q.top();
        q.pop();
        int nowU=nowNode.v;
        if(nowNode.dis!=f[nowU])continue;
        for(int i=head_First[nowU];i;i=e_First[i].nxt){
            int nowV=e_First[i].to;
            if(f[nowV]>f[nowU]+e_First[i].w){
                f[nowV]=f[nowU]+e_First[i].w;
                q.push(Node_First{nowV,f[nowV]});
            }
        }
    }
}
struct Edge{
    int from,to,w,nxt;
}e[MAXM];
int head[MAXN],edgeCnt=1;
void addEdge(int u,int v,int w){
    e[++edgeCnt].from=u;
    e[edgeCnt].to=v;
    e[edgeCnt].w=w;
    e[edgeCnt].nxt=head[u];
    head[u]=edgeCnt;
}
struct Node{
    int v,f,g;
    bool operator <(Node another)const{
        return f+g>another.f+another.g;
    }
};
int updateTime[MAXN];
int ans;
bool Astar(){
    priority_queue<Node> q;
    q.push(Node{s,f[s],0});
    while(!q.empty()){
        Node nowNode=q.top();
        q.pop();
        int nowU=nowNode.v;
        updateTime[nowU]++;
        if(nowU==t&&updateTime[nowU]==k){
            ans=nowNode.g;
            return 1;
        }
        for(int i=head[nowU];i;i=e[i].nxt){
            int nowV=e[i].to;
            q.push(Node{nowV,f[nowV],nowNode.g+e[i].w});
        }
    }
    return 0;
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int a,b,l;
        scanf("%d%d%d",&a,&b,&l);
        addEdge_First(b,a,l);//反向边
        addEdge(a,b,l);//正向边
    }
    scanf("%d%d%d",&s,&t,&k);
    if(s==t)k++;
    dijkstra();
    if(Astar()){
        cout<<ans<<endl;
    }else cout<<"-1"<<endl;
    return 0;
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息