程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 2449 Remmarguts Date

POJ 2449 Remmarguts Date

編輯:C++入門知識

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(); 
        for(int i = 1; i <= m; i++) 
        { 
            scanf("%d%d%d", &x, &y, &w); 
            insert(x, y, w); 
        } 
        scanf("%d%d%d", &s, &t, &k); 
        spfa(t); www.2cto.com
        printf("%d\n", Astar(s, t)); 
    } 
    return 0; 


 


作者:ultimater

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved