当前位置:编程学习 > C/C++ >>

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
题目大意就是给出一个图,然后给出一个起点个一个终点,求这两点间的第K短路。

本题中是可以走重复的路的,所以如果一张图中有一个环的话,无论求第几短路都存在。

思路:前向星+SPFA+A*

LANGUAGE:C++

CODE:

[html] 
#include <cstring> 
#include <cstdio> 
#include <queue> 
#define MAXN 1005 
#define MAXM 500005 
#define INF 1000000000 
using namespace std; 
struct node 

    int v, w, next; 

edge[MAXM], revedge[MAXM]; 
struct A 

    int f, g, v; 
    bool operator <(const A a) 
    const 
    { 
        if(a.f == f) return a.g < g; 
        return a.f < f; 
    } 
}; 
int e, vis[MAXN], d[MAXN], q[MAXM * 5]; 
int 
head[MAXN], revhead[MAXN]; 
int n, m, s, t, k; 
void init() 

    e = 0; 
    memset(head, -1, sizeof(head)); 
    memset(revhead, -1, sizeof(revhead)); 

void insert(int x, int y, int w) 

    edge[e].v = y; 
    edge[e].w = w; 
    edge[e].next = head[x]; 
    head[x] = e; 
    revedge[e].v = x; 
    revedge[e].w = w; 
    revedge[e].next =revhead[y]; 
    revhead[y] = e++; 

void spfa(int src) 

    for(int i = 1; i <= n; i++) d[i] = INF; 
    memset(vis, 0, sizeof(vis)); 
    vis[src] = 0; 
    int h = 0, t = 1; 
    q[0] = src; 
    d[src] = 0; 
    while(h < t) 
    { 
        int u = 
            q[h++]; 
        vis[u] = 0; 
        for(int i = revhead[u] ; i != -1; i = revedge[i].next) 
        { 
            int v = revedge[i].v; 
            int w = revedge[i].w; 
            if(d[v] > d[u] + w) 
            { 
                d[v] = d[u] + w; 
                if(!vis[v]) 
                { 
                    q[t++] = v; 
                    vis[v] = 1; 
                } 
            } 
        } 
    } 

int Astar(int src, int des) 

    int cnt = 0; 
    priority_queue<A>Q; 
    if(src == des) k++; 
    if(d[src] == INF) return -1; 
    A t, tt; 
    t.v = src, t.g = 0, t.f = t.g + d[src]; 
    Q.push(t); 
    while(!Q.empty()) 
    { 
        tt = Q.top(); 
        Q.pop(); 
        if(tt.v == des) 
        { 
            cnt++; 
            if(cnt == k) return tt.g; 
        } 
        for(int i = head[tt.v]; i != -1; i = edge[i].next) 
        { 
            t.v = edge[i].v; 
            t.g = tt.g + edge[i].w; 
            t.f = t.g + d[t.v]; 
            Q.push(t); 
        } 
    } 
    return -1; 

int main() 

    int x, y, w; 
    while(scanf("%d%d", &n, &m) != EOF) 
    { 
        init(); 
      &nbs

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,