POJ 2449-Remmarguts' Date(K短路 A*+dijstra)

                                      Remmarguts' Date

 

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 35184 Accepted: 9506

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

Source

POJ Monthly,Zeyuan Zhu

[Submit]   [Go Back]   [Status]   [Discuss]

题目大意:让你求两个点之间的第K短路径。

解题思路:运用Astar算法解k短路问题,我就简单讲一下A*算法在求解k短路的过程,首先,需要求出每个点到最短路径,然后从起点开始搜,这个过程其实很像dijstra算法,我们在每个节点中存3个信息:1.当前节点(from),2.从起点走到当前节点的实际花费(g),3.从当前节点到终点的最终总花费估价(f) == g+当前节点到终点的最短距离,这样当我们第k次经过终点的时候我们的g就是k短路的长度了。这样我们我们就开始了k短路的搜索之旅,上代码。有兴趣的同学可以去深入学习下A*算法。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<double, int> dpar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const int N = 1010;
const double pi = 3.1415926;

using namespace std;

int n, m, cnt, cntr, s, t, k, sum;
int head[1010], headr[1010], d[1010], vis[1010];

struct edge
{
    int to;
    int next;
    int len;
}e[100010], er[100010];

void add(int u, int v, int w)
{
    e[cnt].to = v;
    e[cnt].len = w;
    e[cnt].next = head[u];
    head[u] = cnt ++;
    er[cntr].to = u;
    er[cntr].len = w;
    er[cntr].next = headr[v];
    headr[v] = cntr ++;
}

void dijstra() //反向跑
{
    for(int i = 1; i <= n; i ++) {
        d[i] = INF;
    }
    d[t] = 0;
    priority_queue<par> q;
    q.push(make_pair(0, t));
    while(!q.empty()) {
        par cur = q.top();
        q.pop();
        int u = cur.second;
        int w = -cur.first;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = headr[u]; ~i; i = er[i].next) {
            int en = er[i].to;
            int len = w+er[i].len;
            if(d[en] > len) {
                d[en] = len;
                q.push(make_pair(-len, en));
            }
        }
    }
}

struct node
{
    int from;
    int g;
    int f;
    bool operator<(const node a) const { //注意写好重载
        if(a.f == f) return a.g < g;
        return a.f < f;
    }
};

int Astar()
{
    sum = 0;
    node st;
    if(s == t) k ++; //如果起点是终点的话k需要,否则当>=2时求出来的是k-1短路
    if(d[s] == INF) return -1; //最短路都不存在,k短路肯定也不会存在
    st.from = s;
    st.g = 0;
    st.f = 0; //这里有些人会按实际意义初始化为d[s],其实什么值都可以,反正用不到
    priority_queue<node> q;
    q.push(st);
    while(!q.empty()) {
        node cur = q.top();
        q.pop();
        int u = cur.from;
        if(u == t) {
            sum ++;
            if(sum == k) {
                return cur.g;
            }
        }
        for(int i = head[u]; ~i; i = e[i].next) {
            int en = e[i].to;
            node next;
            next.from = en;
            next.g = cur.g + e[i].len; //到当前节点的实际花费
            next.f = next.g + d[en]; //到终点的预估花费
            q.push(next);
        }
    }
    return -1;
}

void init()
{
    mem1(head);
    mem1(headr);
    mem0(vis);
    cnt = cntr = 0;
}

int main()
{
    int x, y, z;
    while(~scanf("%d%d", &n, &m)) {
        init();
        for(int i = 0; i < m; i ++) {
            scanf("%d%d%d", &x, &y, &z);
            add(x, y, z);
        }
        scanf("%d%d%d", &s, &t, &k);
        dijstra();
        printf("%d\n", Astar());
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值