poj 2449 Remmarguts' Date 求第k短路 Astar算法

=.=好菜

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <queue>

using namespace std;
const int N = 1e3+10;
const int M = 100000+10;
typedef long long ll;
const ll INF = 1e15;

int n,m,head[N],rehead[N],tot;
struct node {
    int v,w,next;
}E[M],reE[M];

void init() {
    tot=0;
    memset(head,0,sizeof(head));
    memset(rehead,0,sizeof(rehead));
}
void addEdge(int u,int v,int w) {
    ++tot;
    E[tot].next = head[u];
    head[u]=tot;
    E[tot].v = v;
    E[tot].w = w;

    reE[tot].next = rehead[v];
    rehead[v]=tot;
    reE[tot].v = u;
    reE[tot].w = w;
}

bool inq[N]; ll d[N];
queue<int>que;
void spfa(int src) {
    while(!que.empty())
        que.pop();
    for(int i=0;i<N;i++) d[i]=INF;
    memset(inq,0,sizeof(inq));
    d[src]=0;
    inq[src] = 1;
    que.push(src);
    while(!que.empty()) {
        int now = que.front();
        que.pop();
        //if(vis[now]) continue;
        //vis[now]=1;
        inq[now] = 0;
        for(int i=rehead[now]; i; i=reE[i].next) {
            int v = reE[i].v;
            int w = reE[i].w;
            if(d[v] > d[now] + w) {
                d[v] = d[now] + w;
                if(!inq[v])
                    que.push(v);
            }
        }
    }
}

struct A {
    int v;
    ll f,g;
    ///v是current点 f(v)=g(v)+h(v) g(v):st到v的估值, h(v):v到ed的估值
    bool operator<(const A other) const {
        if(other.f == f) return other.g < g;
        return other.f < f;
    }
};

int Astar(int st,int ed,int k) {
    priority_queue<A> Q;
    if(st==ed) k++;
    if(d[st]==INF) return -1;
    int cnt = 0;
    A t,tt;
    t.v=st,t.g=0,t.f=t.g+d[st];
    Q.push(t);
    while (!Q.empty()) {
        tt = Q.top(); Q.pop();
        int u=tt.v;
        if(u == ed) {
            cnt++;
            if(cnt==k) return tt.g;
        }
        for(int i=head[u]; i; i=E[i].next) {
            t.v = E[i].v;
            t.g = tt.g + E[i].w;
            t.f = t.g + d[t.v];
            Q.push(t);

        }
    }
    return -1;
}

int main () {
    //freopen("in.txt","r",stdin);
    while (scanf("%d %d", &n, &m)==2) {
        init();
        for(int i=1;i<=m;i++) {
            int x,y,z; scanf("%d%d%d",&x,&y,&z);
            addEdge(x,y,z);
        }
        int s,t,k; scanf("%d%d%d",&s, &t, &k);
        spfa(t);
        //for(int i=1;i<=n;i++) cout << d[i]<<endl;
        cout<<Astar(s,t,k)<<endl;
    }
    return 0;
}

poj 2449 Remmarguts' Date 求第k短路 Astar算法

原文地址:https://www.cnblogs.com/Draymonder/p/9634958.html

时间: 2024-08-02 10:28:01

poj 2449 Remmarguts' Date 求第k短路 Astar算法的相关文章

poj 2449 Remmarguts&#39; Date A*+spfa求第k短路

题意: 经典的第k短路,A*算法的经典应用之一. 分析: A*,已走的路程g+到终点的最短距离为启发函数,搜索过程中不判重,第k次到t节点时就求出了第k短路. 代码: //poj 2449 //sep9 #include <iostream> #include <queue> using namespace std; const int maxN=1024; const int maxM=100024; int n,m,s,t,k,e,ne; int head[maxN],nhea

POJ 2449 Remmarguts&#39; Date (A*搜索求K短路)

传送门 这是一道裸的K短路的问题,我们将会用A*解决. 我们设计估值函数h的时候可以像这样想.因为h(n) <= h*(n)而且要尽量接近h*(n),所以我们想到,可以求一个从目标节点到其余节点的最短路,这个一定是小于等于实际值的.然后就用A*从起点开始搜索,找到一个节点v,就使cnt[v]加1.当cnt[v] > k时就可以剪枝了,因为这一定不再K短路的路线上了.很好通过反证法得到证明.当目标节点被搜索到了第k次的时候就可以结束搜索了. 要注意这道题有一个很坑的地方,就是若给出的起点=终点,

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

POJ 2449 Remmarguts&#39; Date (第k短路 A*搜索算法模板)

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22412   Accepted: 6085 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

poj 2449 Remmarguts&#39; Date(K短路,A*算法)

http://poj.org/problem?id=2449 大致题意:给出一个有向图,求从起点到终点的第K短路. K短路与A*算法详解  学长的博客... 算法过程 #include <stdio.h> #include <iostream> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #in

poj 2449 Remmarguts&#39; Date (k短路模板)

Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30772   Accepted: 8397 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly tou

POJ 2449 Remmarguts&#39; Date ( 第 k 短路 &amp;&amp; A*算法 )

题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1024; const int maxm = 100008; struct EDGE{ int v

POJ 2449 Remmarguts&#39; Date(k短路模板)

link:https://vjudge.net/problem/POJ-2449 前面输入与大多最短路题相同 最后一行输入s,t,k 求从s到t的第K短路 #include <iostream> #include <cstring> #include <queue> using namespace std; const int MAXN=1010; struct node { int p,g,h; bool operator < (node a) const {

poj 2449 Remmarguts&#39; Date k短路

/*poj 2449 k短路 A* 估价函数是 s到i的距离+i到t的距离 */ #include<cstdio> #include<queue> #include<vector> #define inf 1e7 #define maxn 100010 using namespace std; int n,m,S,T,K,num1,num2,head1[maxn],head2[maxn],dis[maxn]; int q[maxn],hea,tai,f[maxn],cn