POJ 2449 Remmarguts' Date k短路 A*+Dijkstra

Remmarguts' Date
Time Limit: 4000MS      Memory Limit: 65536K
Total Submissions: 28497        Accepted: 7739

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
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

inline void read(int&x){
    x=0;
    char t;
    while(isdigit(t=getchar()))
        x=x*10+t-'0';
}

const int N=1000+5;
const int M=100000+5;
int dist[N];//原图 s作为起点 dijkstra
int dist1[N];//新图 t作为起点 dist1[i]=h[i] 原图中i到t的最短距离

int head[N];//原图
int head1[N];//原图所有边取反后得到的新图
struct Edge{
    int to,cost,next;
}edge[M],edge1[M];

inline void addEdge(int*head,Edge*edge,int k,int u,int v,int c){
    edge[k].to=v;
    edge[k].cost=c;
    edge[k].next=head[u];
    head[u]=k;
}

struct cmpDijkstra{
    bool operator()(const int&a,const int&b){
        return dist[a]>dist[b];
    }
};

void Dijkstra(int s,int n,int*head,Edge*edge,int*dist){
    fill(dist,dist+n+1,INF);
    dist[s]=0;
    priority_queue<int,vector<int>,cmpDijkstra>que;
    que.push(s);
    while(!que.empty()){
        int x=que.top();
        que.pop();
        for(int i=head[x];i!=-1;i=edge[i].next){
            int to=edge[i].to,c=edge[i].cost;
            if(dist[to]>dist[x]+c){
                dist[to]=dist[x]+c;
                que.push(to);
            }
        }
    }
}
struct Node{
    int x,g,f;
    bool operator>(const Node&a)const{
        if(a.f==f)
            return g>a.g;
        return f>a.f;
    }
};

int aStar(int s,int t,int k,int*head,Edge*edge,int*h){
    Node e,ne;
    int cnt=0;
    priority_queue<Node,vector<Node>,greater<Node> >que;
    if(s==t)//s,t可能相同
        ++k;
    if(h[s]==INF)//s,t不连通
        return -1;
    e.x=s;
    e.g=0;
    e.f=e.g+h[e.x];
    que.push(e);
    while(!que.empty()){
        e=que.top();
        que.pop();
        if(e.x==t){
            ++cnt;
        }
        if(cnt==k){
            return e.g;
        }
        for(int i=head[e.x];i!=-1;i=edge[i].next){
            ne.x=edge[i].to;
            ne.g=edge[i].cost+e.g;
            ne.f=ne.g+h[ne.x];
            que.push(ne);
        }
    }
    return -1;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n,m,u,v,c,s,t,k;
    scanf("%d%d",&n,&m);
    fill(head,head+n+1,-1);
    fill(head1,head1+n+1,-1);
    for(int i=0;i<m;++i){
        scanf("%d%d%d",&u,&v,&c);
        addEdge(head,edge,i,u,v,c);
        addEdge(head1,edge1,i,v,u,c);
    }
    scanf("%d%d%d",&s,&t,&k);
    Dijkstra(s,n,head,edge,dist);
    Dijkstra(t,n,head1,edge1,dist1);
    printf("%d\n",aStar(s,t,k,head,edge,dist1));
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值