POJ 2449 Remmarguts' Date [第k短路]

来源:互联网 发布:lte中d2怎么优化 编辑:程序博客网 时间:2024/04/30 19:35

Remmarguts’ Date
Time Limit: 4000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

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


题说得挺迷的,反正就是要求 S –> T 的第k短路 有向图。
那么可以用A*算法,统计T进队的次数,第k次那么就是答案了。
h函数可以从T求一遍最短路,那么h(x) = h’(x) 精确求解。
不过要注意如果一开始S==T,不算一条路。
另外注意特判S,T 不联通且S集合存在环的情况!!不然会死循环的。。。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 1005;const int maxm = 100005;struct Edge{    int to,next;    int val;}edge1[maxm],edge2[maxm];int head1[maxn],head2[maxn];int maxedge1,maxedge2;inline void addedge1(int u,int v,int w){    edge1[++maxedge1] = (Edge) { v,head1[u],w };    head1[u] = maxedge1;}inline void addedge2(int u,int v,int w){    edge2[++maxedge2] = (Edge) { v,head2[u],w };    head2[u] = maxedge2;}int n,m,S,T,k;int h[maxn];bool inque[maxn];void spfa(int S){    queue <int> que;    h[S]=0; inque[S]=true;    que.push(S);    while(!que.empty())    {        int u = que.front(); que.pop(); inque[u]=false;        for(int i=head2[u];~i;i=edge2[i].next)        {            int v = edge2[i].to;            if(h[v] > h[u] + edge2[i].val)            {                h[v] = h[u] + edge2[i].val;                if(inque[v]) continue;                inque[v] = true;                que.push(v);            }        }    }}struct Node{    int f,g;    int id;    bool operator < (const Node t) const // greater<int>    {        if(f ^ t.f) return f > t.f;        return g > t.g;    }};int A_star(int S,int T){    if(h[S]==INF) return -1; // avoid the case of a circle!!    int cnt = 0;    priority_queue <Node> que;    Node now = (Node) { h[S],0,S }; // g = 0 here!!! not h[S] too!!!!    que.push(now);    while(!que.empty())    {        Node u = que.top(); que.pop();        if(u.id==T)            if(++cnt == k) return u.g;        for(int i=head1[u.id];~i;i=edge1[i].next)        {            Node v;            v.id = edge1[i].to;            v.g = u.g + edge1[i].val;            v.f = v.g + h[v.id];            que.push(v);        }    }    return -1;}inline void init(){    memset(head1,-1,sizeof(head1));    memset(head2,-1,sizeof(head2));    maxedge1 = maxedge2 = -1;    memset(h,0x3f,sizeof(h));}int main(){    freopen("k.in","r",stdin);    freopen("k.out","w",stdout);    init();    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)    {        int u,v,c;        scanf("%d%d%d",&u,&v,&c);        addedge1(u,v,c);        addedge2(v,u,c);    }    scanf("%d%d%d",&S,&T,&k);    if(S==T) k++; // special judge!! The cost 0 isn't a path!!    spfa(T);    int ans = A_star(S,T);    printf("%d",ans);    return 0;}
0 0