POJ 2449 Remmarguts' Date (第K短路)

Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 17098   Accepted: 4696

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. 

"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." 

"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" 

Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! 

DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 

Input

The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T. 

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

Output

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

Sample Input

2 2
1 2 5
2 1 4
1 2 2

Sample Output

14

Source

POJ Monthly,Zeyuan Zhu

题意:大意是 有N个station 要求从s点到t点 的第k短路 (不过我看题意说的好像是从t到s 可能是出题人写错了)

思路: 这是一道 经典的第k短路算法,只要你会就能过。PS:这也是我第一k短路题 学到了很多新的东西 因为没学过A* 算法 所以在网上找了好久,但讲了都不是清楚 解题报告也都不带注释的 这里我就附上详细的解题报告 也好给以后要学的人 一点帮助。

从这题中还真的学到了很多
1.第k短路的算法 A* 还有用边表实现dij
2.第一次使用优先队列 这个高级啊!! 附个优先队列的一些资料吧

跟queue用法差不多 下面是它的一些操作:

push():入队,即插入元素

pop():出队,即删除元素

front()或 top():读取队首元素

back():读取队尾元素

empty():判断队列是否为空

size():队列当前元素

优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素。但是它有一个特性,就是队列中最大的元素总是位于队首,所以出队时,并非按照先进先出的原则进行,而是将当前队列中最大的元素出队。这点类似于给队列里的元素进行了由大互小的顺序排序。元素的比较规则默认按元素值由大到小排序,可以重载“<”操作符来重新定义比较规则。


(注:以下部份资料来源于网上)
所谓A*就是启发是搜索 说白了就是给搜索一个顺序使得搜索更加合理减少无谓的搜索. 如何来确定搜索的顺序?..也就是用一个值来表示 这个值为f[n]..每次搜索取f[x]最小的拓展 那么这个f[n]=h[n]+g[n]
其中f(n) 是节点n的估价函数,g(n)是在状态空间中从初始节点到n节点的实际代价,h(n)是从n到目标节点最佳路径的估计代价。在这里主要是h(n)体现了搜索的启发信息,因为g(n)是已知的。如果说详细 点,g(n)代表了搜索的广度的优先趋势。但是当h(n) >> g(n)时,可以省略g(n),而提高效率。

A*算法的估价函数可表示为:   
  f’(n) = g’(n) + h’(n)   
这里,f’(n)是估价函数,g’(n)是起点到终点的最短路径值,h’(n)n到目标的最短路经的启发值。由 于这个f’(n)其实是无法预先知道的,所以我们用前面的估价函数f(n)做近似。g(n)代替g’(n),但 g(n)>=g’(n) 才可(大多数情况下都是满足的,可以不用考虑),h(n)代替h’(n),但h(n)<=h’(n)才可(这一点特别的重 要)。可以证明应用这样的估价函数是可以找到最短路径的,也就是可采纳的。我们说应用这种估价函数的 最好优先算法就是A*算法。
下面以这道题为例,结合着理解相信你就能理解了!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

const int VM=1010;
const int EM=100010;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt;
    int cap;
}edge[EM<<1];

int n,m,cnt,src,des;
int head[VM],tail[VM],dis[VM],vis[VM];  //head 是正向边,tail是逆向边 dis是des(终点)到各点的距离

void addedge(int cu,int cv,int cw){
    edge[cnt].to=cv;  edge[cnt].cap=cw;
    edge[cnt].nxt=head[cu];  head[cu]=cnt++;

    edge[cnt].to=cu;  edge[cnt].cap=cw;
    edge[cnt].nxt=tail[cv];  tail[cv]=cnt++;
}

struct data{
    int g,h;    //g 表示起点到当前点的距离,h表终点到当前点的距离
    int to;
    bool operator < (const data &a) const{  //优先队列的排序(其实也不能这么讲) 使g+h小的在队首
        return a.g+a.h<g+h;
    }
};

void Dijkstra(){    //dijstra算法求des到各点的距离 用于估价函数h
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    dis[des]=0;
    int i,j,k,tmp;
    for(i=1;i<=n;i++){
        tmp=INF;
        for(j=1;j<=n;j++)
            if(!vis[j] && tmp>dis[j]){
                tmp=dis[j];
                k=j;
            }
        if(tmp==INF)    //因为这里图肯定是连通的 可加可不加
            break;
        vis[k]=1;
        for(j=tail[k];j!=-1;j=edge[j].nxt){
            int v=edge[j].to;
            if(!vis[v] && dis[v]>dis[k]+edge[j].cap)
                dis[v]=dis[k]+edge[j].cap;
        }
    }
}

int Astar(int k){   //A*算法求第k短路
    int count[VM];
    memset(count,0,sizeof(count));
    priority_queue<data> q;
    while(!q.empty())
        q.pop();
    data cur,next;  //当前结点 下个结点
    cur.to=src;  cur.g=0;  cur.h=dis[src];  //当前结点初始化 
    q.push(cur);
    while(!q.empty()){
        cur=q.top();
        q.pop();
        count[cur.to]++;
        if(count[cur.to]>k) //如果当前想拓展的点cnt>k就没必要拓展了
            continue;       //因为这个点已经是求到k+1短路了 从这个点继续往下搜肯定得到的是大于等于k+1短路的路径
        if(cur.to==des && count[cur.to]==k)  //找到第K短路 返回
            return cur.g;
        for(int i=head[cur.to];i!=-1;i=edge[i].nxt){    //相连的点入队列
            int v=edge[i].to;
            next.to=v;  next.g=cur.g+edge[i].cap;  next.h=dis[v];
            q.push(next);
        }
    }
    return -1;
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d",&n,&m)){
        cnt=0;
        memset(head,-1,sizeof(head));
        memset(tail,-1,sizeof(tail));
        int u,v,w;
        while(m--){
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        int k;
        scanf("%d%d%d",&src,&des,&k);
        if(src==des)    //起点和终点相同时,k要++
            k++;
        Dijkstra();
        int ans=Astar(k);
        printf("%d\n",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3024597.html