Remmarguts' Date(POJ-2449)(A*解决)

Problem 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,询问从 S 到 T 的第 k 条路最短路径 

思路:k 短路模板题,利用 SPFA+A* 即可解决

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<strinG>
#include<cstrinG>
#include<cmath>
#include<ctime>
#include<alGorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL GetInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1500+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;

struct Edge {
    int to, next;
    int w;
    Edge() {}
    Edge(int to, int next, int w) : to(to), next(next), w(w) {}
};
struct Map {
    int tot;
    int head[N];
    Edge edge[N * N];
    Map() {
        tot = 0;
        memset(head, -1, sizeof(head));
    }
    void addEdge(int x, int y, int w) {
        edge[++tot].to = y;
        edge[tot].next = head[x];
        edge[tot].w = w;
        head[x] = tot;
    }
    Edge &operator[](int pos) { return edge[pos]; }
};
int n, m;
Map G, GT;
int dis[N];
bool vis[N];
struct Status{
    int node;//点编号
    int diss;//距离
    int priority;//优先级
    Status() : node(0), diss(0), priority(dis[0]) {}
    Status(int node, int diss) : node(node), diss(diss), priority(diss + dis[node]) {}
    bool operator<(Status b) const { return priority > b.priority; }
} status;
bool SPFA(int S, int T, int k) { //对反图求最短路
    memset(dis, INF, sizeof(dis));
    memset(vis, false, sizeof(vis));
    dis[S] = 0;

    queue<int> Q;
    Q.push(S);
    while (!Q.empty()) {
        int x = Q.front();
        Q.pop();
        vis[x] = false;
        for (int i = GT.head[x]; i != -1; i = GT[i].next) {
            int y = GT[i].to;
            if (dis[x] + GT[i].w < dis[y]) {
                dis[y] = dis[x] + GT[i].w;
                if (!vis[y]) {
                    Q.push(y);
                    vis[y] = true;
                }
            }
        }
    }
    return dis[T] != INF;
}
int label[N];
int AStart(int S, int T, int k) {
    memset(label, 0, sizeof(label));
    if (S == T)
        k++;

    priority_queue<Status> Q;
    Q.push(Status(S, 0));
    while (!Q.empty()) {
        Status temp = Q.top();
        Q.pop();
        label[temp.node]++;
        if (temp.node == T && label[temp.node] == k)
            return temp.diss;
        for (int i = G.head[temp.node]; i != -1; i = G[i].next)
            if (label[G[i].to] < k)
                Q.push(Status(G[i].to, temp.diss + G[i].w));
    }
    return -1;
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        int x, y, w;
        scanf("%d%d%d", &x, &y, &w);
        G.addEdge(x, y, w);
        GT.addEdge(y, x, w);
    }

    int s, t, k;
    scanf("%d%d%d", &s, &t, &k);
    if (!SPFA(t, s, k))
        printf("-1\n");
    else
        printf("%d\n", AStart(s, t, k));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值