POJ——2449 Remmarguts' Date

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

 

 
Solution:
  K短路的板子题,直接spfa+A*。
  简单讲下A*的思路,就是在优先队列广搜的基础上,对于当前状态有一个估价函数,每次选择估价函数和当前实际值之和最优的去扩展。
  对于本题,我们先spfa预处理出源点到各点的最短距离。再反向进行A*(反向是因为处理出的最短距离$dis[i]$表示的是源点到$i$的最短路,要用$dis$作为估价函数,就必须使源点变为终点),设$f(x)$表示估价函数,$g(x)$表示$t$到$x$的实际距离,那么显然$f(x)=g(x)+dis[x]$,最后广搜出前$k$小的值就好了。
  注意,设$h(x)$表示实际的代价,$h'(x)$表示估计的代价,则必须满足$h(x)geq h'(x)$(这很显然,大于就会切掉正确的方案)。
  然后本题坑点是$s$可能等于$t$,但又必须往外走一遍回来,所以当$s==t$时记得$k++$。
 
代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<cstdio>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)>(b)?(b):(a))
using namespace std;
const int N=200005,inf=233333333;
int s,t,n,m,k,w[N],W[N],dis[N],ans[N],tot;
int to[N],net[N],h[N],cnt1,To[N],Net[N],H[N],cnt2;
struct node{
    int f,g,id;
    bool operator<(const node a)const{return f>a.f;}
};
bool vis[N];
priority_queue<node>Q;

il int gi(){
    int a=0;char x=getchar();bool f=0;
    while((x<'0'||x>'9')&&x!='-')x=getchar();
    if(x=='-')x=getchar(),f=1;
    while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+x-48,x=getchar();
    return f?-a:a;
}

il void add(int u,int v,int c){
    to[++cnt1]=v,net[cnt1]=h[u],h[u]=cnt1,w[cnt1]=c;
    To[++cnt2]=u,Net[cnt2]=H[v],H[v]=cnt2,W[cnt2]=c;
}

il void spfa(){
    queue<int>q;
    For(i,1,n) dis[i]=inf;
    dis[s]=0,vis[s]=1,q.push(s);
    while(!q.empty()){
        int u=q.front();vis[u]=0;q.pop();
        for(int i=h[u];i;i=net[i])
            if(dis[to[i]]>dis[u]+w[i]){
                dis[to[i]]=dis[u]+w[i];
                if(!vis[to[i]])q.push(to[i]),vis[to[i]]=1;
            }
    }
}

il void Astar(){
    if(dis[t]==inf) return;
    node tmp;
    tmp.g=0,tmp.f=dis[t],tmp.id=t;
    Q.push(tmp);
    while(!Q.empty()){
        tmp=Q.top();Q.pop();
        if(tmp.id==s) {ans[++tot]=tmp.g;if(tot>=k)return;}
        for(int i=H[tmp.id];i;i=Net[i]){
            node tp;
            tp.g=tmp.g+W[i];
            tp.f=tp.g+dis[To[i]];
            tp.id=To[i];
            Q.push(tp);
        }
    }
}

int main(){
    while(scanf("%d%d",&n,&m)==2){
        memset(h,0,sizeof(h));cnt1=0;
        memset(H,0,sizeof(H));cnt2=0;
        int u,v,c;
        For(i,1,m) u=gi(),v=gi(),c=gi(),add(u,v,c);
        s=gi(),t=gi(),k=gi();
        if(s==t)k++;
        spfa();
        Astar();
        if(tot<k)cout<<-1;
        else cout<<ans[k];
    }
    return 0;
}
原文地址:https://www.cnblogs.com/five20/p/9242853.html