zoukankan      html  css  js  c++  java
  • POJ:2449-Remmarguts' Date(单源第K短路)

    Remmarguts’ Date

    Time Limit: 4000MS Memory Limit: 65536K
    Total Submissions: 33081 Accepted: 8993

    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


    #include <stdio.h>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    using namespace std;
    const int maxn = 1e3+10;
    vector <pair<int,int> > ve[maxn],ve2[maxn];
    int n,m,s,e,k,dis[maxn];
    bool vis[maxn];
    
    struct NODE {
        int to,g,f;
        bool operator < (const NODE &a) const {
            if(a.f == f)
                return a.g < g;
            return a.f < f;
        }
    };
    
    void init(){
        for(int i=0;i<=n;i++) {
            ve[i].clear();
            ve2[i].clear();
        }
        for(int i=1;i<=m;i++){
            int a,b,len;
            scanf("%d%d%d",&a,&b,&len);
            ve[a].push_back(make_pair(len,b));
            ve2[b].push_back(make_pair(len,a));
        }
        scanf("%d%d%d",&s,&e,&k);
    }
    
    void spfa() {
        memset(dis,0x7f,sizeof(dis));
        queue <int> qu;
        qu.push(e);
        dis[e] = 0;
        vis[e] = true;
        while(!qu.empty()) {
            int u = qu.front(); qu.pop();
            int d = dis[u];
            vis[u] = false;
            for(int i=0;i<ve2[u].size();i++) {
                int v = ve2[u][i].second;
                int d2 = d + ve2[u][i].first;
                if(d2 < dis[v]){
                    dis[v] = d2;
                    if(!vis[v]) {
                        qu.push(v);
                        vis[v] = true;
                    }
                }
            }
        }
    }
    
    int A_star() {
        if(dis[s] == 0x7f7f7f7f)
            return -1;
        NODE temp,Next;
        int cnt = 0;
        if(s == e)
            k++;
        temp.to = s; temp.g = 0; temp.f = temp.g + dis[temp.to];
        priority_queue <NODE> qu;
        qu.push(temp);
    
        while(!qu.empty()){
            temp = qu.top(); qu.pop();
            if(temp.to == e) cnt++;
            if(cnt == k) return temp.g;
    
            int u = temp.to;
            int d = temp.g;
            for(int i=0;i<ve[u].size();i++) {
                Next.to = ve[u][i].second;
                Next.g = d + ve[u][i].first;
                Next.f = Next.g + dis[Next.to];
                qu.push(Next);
            }
        }
        return -1;
    }
    
    int main(){
        while(scanf("%d%d",&n,&m) != EOF) {
            init();
            spfa();
            int ans = A_star();
            printf("%d
    ", ans);
            return 0;
        }
    }
  • 相关阅读:
    我的架构经验系列文章 前端架构
    我的架构经验系列文章 后端架构 安全层面
    Adhesive框架系列文章报警服务模块使用和实现
    Adhesive框架系列文章Mongodb数据服务模块实现(上)
    在Silverlight程序中使用Thread一个很容易被忽略的问题
    .net(偏web) vs j2ee的一些框架选型
    Wcf扩展
    Adhesive框架系列文章内存队列服务模块使用和实现
    【翻译】C#编程语言和JAVA编程语言的比较(下)
    在测试Adhesive的时候发现一个Mongodb官方驱动1.1.0.4184比较严重的BUG
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107159.html
Copyright © 2011-2022 走看看