第k短路 A*啓發式搜索解決

(同步個人博客 http://sxysxy.org/blogs/19 到csdn)

給一張圖,求兩點間第k短的路徑的長度。

查了相關資料,這個竟然可以用啓發式搜索來解決!

其實是,在啓發式搜索時,同時記錄目標點t入隊(意味着走到了t)多少次(被”啓發”了多少次)。第一次啓發到t是第1短路,第二次是第2短…..第k次就是第k短路。通過這個題,我漲了姿勢:啓發式搜索還能這樣用!!

搜索中,距離目標點t的估價可以這樣得來: 建立一張原圖的反向圖,從目標點t開始做單源帶權最短路得到t到所有其它節點的最短路徑(自然用spfa了),得到結果rdist[x]表示x節點到t節點的距離,用作x到t的估價。

之後就來牛刀小試了 http://syzoj.com/problem/101 uscao上的題目,求節點1到n的第2短路,即參數k = 2而已。代碼:

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <list>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
#define MAXN 100002
struct edge
{
    int from, to;
    int cost;
};
//圖
vector<int> G[MAXN];
vector<edge> edges;

//反向圖
vector<int> RG[MAXN];
vector<edge> redges;

//然而這個題的邊是雙向邊23333...
void addedge(int u, int v, int c)
{
    edges.push_back((edge){u, v, c});
    edges.push_back((edge){v, u, c});
    G[u].push_back(edges.size()-2);
    G[v].push_back(edges.size()-1);

    redges.push_back((edge){v, u, c});
    redges.push_back((edge){u, v, c});
    RG[v].push_back(redges.size()-2);
    RG[u].push_back(redges.size()-1);
}
//反向圖sssp
int rdist[MAXN];
int rvis[MAXN];
void spfa(int s, int t, int n)
{
    for(int i = 1; i <= n; i++)
    {
        rdist[MAXN] = 0x3f3f3f3f;
        rvis[MAXN] = false;
    }
    queue<int> q;
    q.push(s);
    rvis[s] = true;
    rdist[s] = 0;
    while(q.size())
    {
        int c = q.front();
        q.pop();
        for(int i = 0; i < RG[c].size(); i++)
        {
            edge &e = redges[RG[c][i]];
            if(rdist[e.to] > rdist[c] + e.cost)
            {
                rdist[e.to] = rdist[c] + e.cost;
                if(!rvis[e.to])
                {
                    rvis[e.to] = true;
                    q.push(e.to);
                }
            }
        }
        rvis[c] = false;
    }
}

//下面是核心的代碼,啓發式搜索
struct node
{
    int f, g, to;
            //f 啓發
            //g 已用代價
            //to 節點點
    bool operator< (const node& o) const
    {
        return f > o.f;
    }
};
int cnt[MAXN];    //記錄入隊次數
int astar(int s, int t, int k)
{
    memset(cnt, 0, sizeof(cnt));
    priority_queue<node> q;
    if(rdist[s] == 0x3f3f3f3f)return -1; //unreachable
    q.push((node){rdist[s], 0, s});
    while(q.size())
    {
        node c = q.top();
        q.pop();
        cnt[c.to]++;        //入隊次數++
        if(t == c.to && cnt[t] == k)   //目標點入隊k次,是第k短路
            return c.f;
        if(cnt[c.to] > k)       //剪枝,超過k,沒必要再計算
            continue;
        for(int i = 0; i < G[c.to].size(); i++)
        {
            edge &e = edges[G[c.to][i]];
              //f = 估價(距目標點距離)+已用代價(c.g+e.cost)
            q.push((node){rdist[e.to] + e.cost + c.g , c.g + e.cost, e.to});
        }
    }
    return -1;
}

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    while(m--)
    {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        addedge(a, b, c);
    }
    spfa(n, 1, n);
    printf("%d\n", astar(1, n, 2));
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章