【启发式搜索】Remmarguts' Date

来源:互联网 发布:暗黑3数据挖掘者成就 编辑:程序博客网 时间:2024/05/29 04:47

Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 13546 Accepted: 3728

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 21 2 52 1 41 2 2

Sample Output

14


求第K短路,因为坑人的地方比较多,看了两个Discuss才过。

f[i]=g[i]+h[i]。

h[i]设计成i到T的最短路,即可满足第k次扩展到T求出的路线即是第K短路。

求最短路用dijkstra+heap。但是要注意,因为是以T为源点,必须建反向边,而样例是检查不出来这个的。

A*里最坏情况下,每个点要走k次,所以要判断扩展次数是否小于k。


接下来是坑爹的地方。

1、如果没有边(m=0),则输出-1。我输出的0

2、如果起点终点相同(S=T),要注意S点的vis初值应设为-1,因为S刚出堆的时候会自增一,但是不合理,因为起点不算走过一次。



#include <iostream>#include <queue>using std::cout;using std::cin;using std::priority_queue;const long maxn = 10100;long n;long m;long S;long T;long K;long h[maxn];const long inf = 0x7f7f7f7f;long vis[maxn];long ans = 0;struct hnode{    long index;    long dist;    hnode(long i,long d):index(i),dist(d){}    hnode(){}    bool operator<(const hnode& hn2)const{return dist>hn2.dist;}};priority_queue<hnode> que;struct node{    long index;    node* next;    long val;};node* head[maxn];node* head2[maxn];void insert(long a,long b,long c){    node* tmp = new node;    tmp->index = b;    tmp->val = c;    tmp->next = head[a];    head[a] = tmp;}void insert2(long a,long b,long c){    node* tmp = new node;    tmp->index = b;    tmp->val = c;    tmp->next = head2[a];    head2[a] = tmp;}void dijkstra(){    for (long i=1;i<n+1;i++)    {        h[i] = inf;    }    h[T] = 0;    que.push(hnode(T,0));    while (!que.empty())    {        hnode now = que.top();        que.pop();        if (!vis[now.index])        {            for (node* next=head2[now.index];next;next=next->next)            {                                if (h[next->index]>h[now.index]+next->val)                {                    h[next->index] = h[now.index]+next->val;                    que.push(hnode(next->index,h[next->index]));                }                }        }        vis[now.index] = true;    }}void Astar(){    while (!que.empty())        que.pop();    memset(vis,0,sizeof(vis));    if (h[S] == inf)    {        ans = -1;        return;    }    vis[S] = -1;//////////////////    que.push(hnode(S,h[S]));    while (!que.empty())    {        hnode now = que.top();        que.pop();        vis[now.index] ++;        if (vis[T] == K)        {            ans = now.dist;            return;        }        if (vis[now.index]<=K)/////////////        {            for (node* next=head[now.index];next;next=next->next)            {                que.push(hnode(next->index,now.dist+next->val-h[now.index]+h[next->index]));            }        }    }    ans = -1;    return;}int main(){    freopen("rd.in","r",stdin);    freopen("rd.out","w",stdout);    scanf("%ld%ld",&n,&m);    if (m == 0){printf("-1"); return 0;}//////////////    for (long i=1;i<m+1;i++)    {        long a;long b;long c;        scanf("%ld%ld%ld",&a,&b,&c);        insert2(b,a,c);////////////////        insert(a,b,c);    }    scanf("%ld%ld%ld",&S,&T,&K);    dijkstra();    Astar();    printf("%ld",ans);    return 0;}