【图论】【启发式搜索】[POJ 2449]Remmarguts' Date

实际上就是从T到当前节点的最短路的长度,因为首先要保证h(n) <= h*(n)且h(n)尽量接近h*(n)那么直接令h(n)=h*(n)就行了,然后注意每一次判断是否到达终点的时候要在每一次取出的时候判断,因为这个时候的最短路是经过优先队列的排序的。还有就是注意下S==T的情况默认的时候S到本身的最短路肯定是0,那么我们如果要求现在的最短路那么求得就是当前的次短路,让k++就好了

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 1000;
const int MAXM = 100000;
struct node{
    int v, w;
    node *next;
}Edges[MAXM*2+10], *ecnt = Edges, *adj[MAXN+10], *adj1[MAXN+10];
struct State{
    int f, id;
    bool operator<(const State& s) const { return f>s.f; }
};
priority_queue<State> que;
void addedge(int u, int v, int w){
    ++ecnt;
    ecnt->v = v;
    ecnt->w = w;
    ecnt->next = adj[u];
    adj[u] = ecnt;
}
void _addedge(int u, int v, int w){
    ++ecnt;
    ecnt->v = v;
    ecnt->w = w;
    ecnt->next = adj1[u];
    adj1[u] = ecnt;
}
int dis[MAXN+10];
bool inque[MAXN+10];
void SPFA(int res){
    queue<int> que;
    que.push(res); inque[res] = true;
    memset(dis, 0x3f, sizeof dis);
    dis[res] = 0;
    while(!que.empty()){
        res = que.front(); que.pop(); inque[res] = false;
        for(node *p=adj1[res];p;p=p->next){
            int v=p->v;
            if(dis[v] > dis[res] + p->w){
                dis[v] = dis[res] + p->w;
                if(!inque[v]){
                    inque[v] = true;
                    que.push(v);
                }
            }
        }
    }
}
int main(){
    int n, m;
    int S, T, K, u, v, w;
    scanf("%d%d", &n, &m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d", &u, &v, &w);
        addedge(u, v, w);
        _addedge(v, u, w);
    }
    scanf("%d%d%d", &S, &T, &K);
    if(S == T) K++;
    SPFA(T);
    State t1, t2;
    t1.f = dis[S]; t1.id = S;
    que.push(t1);
    int counter = 0;
    while(!que.empty()){
        t1 = que.top(); que.pop();
        if(t1.id == T){
            counter++;
            if(counter == K){
                printf("%d\n" ,t1.f);
                return 0;
            }
        }
        for(node *p=adj[t1.id];p;p=p->next){
            int v = p->v;
            t2.f = t1.f - dis[t1.id] + p->w + dis[v];
            t2.id = v;
            que.push(t2);
        }
    }
    printf("-1\n");

    return 0;
}

转载于:https://www.cnblogs.com/JeremyGJY/p/5921685.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值