poj 2449 Remmarguts' Date 求第k短路(SPFA+A*)

Language:
Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 22947 Accepted: 6275

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


n个点 m条有向边 给出边的起点终点权值 求出S T之间第K短路

基本思路:

首先反向图中求出终点 t 到其他所有点的距离(预处理优化),

再从起点开始使用优先队列进行宽搜,用cnt记录到达终点的次数,当cnt==k时的路径长度即为所得。

搜索的方向用一个估价函数 f=g+h 来确定,其中g表示到当前点的路径长度,h表示当前点到终点的最短路径,即之前的预处理。

A*算法结合了启发式搜索(充分利用题目所给信息来动态的做出决定,使搜索次数大大降低),和形式化方法(不利用图给出的信息,仅利用数学的形式分析,如dij算法)。

它通过一个估价函数 f(h) 来决定搜索方向。估价函数=当前值+当前位置到终点的距离,每次扩展估价函数值最小的一个。


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

struct Edge
{
    int v,w,next;
}e[MAXM*2],e2[MAXM*2];

struct node
{
    int v,g,f;
    bool operator<(const node &p)const
    {
        if(f==p.f) return g>p.g;
        else return f>p.f;
    }
};

int dis[MAXN],head[MAXN],rehead[MAXN];
int n,m,tot;
bool vis[MAXN];

void init()
{
    MEM(head,-1); MEM(rehead,-1);
    tot=0;
    for(int i=0;i<=n;i++)
        dis[i]=INF;
}

void addedge(int u,int v,int w)
{
    e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot;
    e2[tot].v=u; e2[tot].w=w; e2[tot].next=rehead[v]; rehead[v]=tot++;
}

bool SPFA(int s)
{
    MEM(vis,0);
    queue<int> q;
    dis[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int now=q.front(); q.pop();
        vis[now]=0;
        for(int i=rehead[now];i!=-1;i=e2[i].next)
        {
            if(dis[now]+e2[i].w<dis[e2[i].v])
            {
                dis[e2[i].v]=dis[now]+e2[i].w;
                if(!vis[e2[i].v])
                {
                    vis[e2[i].v]=1;
                    q.push(e2[i].v);
                }
            }
        }
    }
}

int astar(int s,int t,int k)
{
    if(s==t) k++;
    if(dis[s]==INF) return -1;
    priority_queue<node> q;
    int cnt=0;
    node tmp,to;
    tmp.v=s; tmp.g=0; tmp.f=tmp.g+dis[tmp.v];
    q.push(tmp);
    while(!q.empty())
    {
        tmp=q.top(); q.pop();
        if(tmp.v==t) cnt++;
        if(cnt==k) return tmp.g;
        for(int i=head[tmp.v];i!=-1;i=e[i].next)
        {
            to.v=e[i].v;
            to.g=tmp.g+e[i].w;
            to.f=to.g+dis[to.v];
            q.push(to);
        }
    }
    return -1;
}

int main()
{
//    fread;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int u,v,w;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        int s,t,k;
        scanf("%d%d%d",&s,&t,&k);
        SPFA(t);
        int ans=astar(s,t,k);
        printf("%d\n",ans);
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值