poj 2449 Remmarguts' Date(第K短路)

来源:互联网 发布:期货跟现货的区别知乎 编辑:程序博客网 时间:2024/05/21 12:41
Dijkstra + A*
搜索和图论中的经典题目。
终于学了一下传说中的A*算法

Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 21404 Accepted: 5826

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

1
4

Source

POJ Monthly,Zeyuan Zhu


题目大意:求S到T的第K短路
解题思路:先逆向建图,从终点做一遍Dijkstra, 预处理出终点到各个点的最短距离,
关于A*算法,A*(A-Star)算法:大概介绍如下:

  启发式方程表示为: f(n)=g(n)+h(n),

  其中f(n) 是从初始点经由节点n到目标点的估价函数,

  g(n) 是在状态空间中从初始节点到n节点的实际代价,

  h(n)是从n到目标节点最佳路径的估计代价。

                   
当g(x) 为0时,A*就成了bfs,当h(x)为0时,A*就成了dfs 

  保证找到最短路径(最优解的)条件,关键在于估价函数h(n)的选取:
             A*算法就通过构造这样一个启发函数,将所有的待扩展状态加入到队列里,每次从队列里选择f(s)值最小的状态进行扩展。由于启发函数的作用,使得计算机在进行状态转移的时候尽量避开了不可能产生最优解的分支,而选择相对较接近最优解的路径进行搜索,提高了搜索效率。

对于此题, g(x)就是每次更新后从起点出发新的路径长度,h(x)则是预处理出的到终点最短路。
用一个优先队列每次选择f(x) = g(x) + h(x)的最小值,不断更新。
如果经过某个点超过K次,那么可以舍去, 一定不是第K短的了,以为一定存在K次比K+1次路径少
如果经过终点K次,那么就是答案了。
注意s == t(即起点和终点重合)的特殊情况, 此时需要计算(k+1)短路,因为s到t这条距离为0的路不能算在这k短路中。

 

#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;const int N = 1010;const int M = 1000010;typedef pair<int, int> P;struct Data{int v, h, g;}fr, nx;struct Edge{int to, next, cost;} e[M * 2];int d[N];int vis[N];int first[N];int tu[M], tv[M], tw[M];int cnt[N];int n, m;int s, t, k;int tot = 0;int res;void addedge(int v, int u, int w){    ++tot;    e[tot].to = u;    e[tot].cost = w;    e[tot].next = first[v];    first[v] = tot;}void dijkstra(int s){    memset(d, 63, sizeof(d));    memset(vis, 0, sizeof(vis));    d[s] = 0;    priority_queue<P, vector<P>, greater<P> > qu;    qu.push(P(0, s));    while(!qu.empty()){        P p = qu.top();        qu.pop();        int u = p.second;        if(d[u] < p.first) continue;        for(int i = first[u]; i != -1; i = e[i].next){            int v = e[i].to;            if(d[v] > d[u] + e[i].cost){                d[v] = d[u] + e[i].cost;                qu.push(P(d[v], v));            }        }    }}bool operator < (Data x, Data y){    return x.g + x. h > y.g + y.h;}void getK(int s){    priority_queue<Data> Q;    fr.v = s;    fr.g = 0;    fr.h = d[s];    Q.push(fr);    while(!Q.empty()){        fr = Q.top();        Q.pop();        //cout << "fr.v = " << fr.v << " " << fr.g << " " << fr.h << endl;        if(++cnt[fr.v] > k) continue;        if(cnt[t] == k){            printf("%d\n", fr.g + fr.h);            return;        }        for(int i = first[fr.v]; i != -1; i = e[i].next){            int u = e[i].to, w = e[i].cost;            nx.v = u;            nx.g = fr.g + w;            nx.h = d[u];            //cout << "nx " << nx.v << " " << nx.g << " " << nx.h << endl;            Q.push(nx);        }   }   puts("-1");   return;}int main(){    while(scanf("%d%d", &n, &m) != EOF){        memset(first, -1, sizeof(first));        tot = 0;        for(int i = 0; i < m; i++){            scanf("%d%d%d", &tv[i], &tu[i], &tw[i]);            addedge(tu[i], tv[i], tw[i]);        }        scanf("%d%d%d", &s, &t, &k);        if(s == t) k++;        dijkstra(t);        //for(int i = 1; i <= n; i++) cout << d[i] << " "; cout << endl;        tot = 0;        memset(first, -1, sizeof(first));        for(int i = 0; i < m; i++)            addedge(tv[i], tu[i], tw[i]);        memset(cnt, 0, sizeof(cnt));        getK(s);    }    return 0;}/*3 31 2 12 3 21 3 41 3 23 41 2 12 3 21 3 43 1 41 3 32 31 2 51 2 62 1 41 2 22 41 2 11 2 21 2 32 1 51 2 32 41 2 11 2 21 2 32 1 51 2 42 41 2 11 2 21 2 32 1 51 2 5*/

0 0
原创粉丝点击