zoukankan      html  css  js  c++  java
  • POJ 2449 Remmarguts' Date[k短路]

    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
    链接:http://poj.org/problem?id=2449
    题目大意:N个站,S是大厅号,T是王子所在地。求S->T的第K短路,如果不存在第K短路,输出-1...
    解题思路:K短路模板
    代码如下:
    #include <stdio.h>
    #include <string.h>
    #include <queue>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define N 1005
    #define INF 999999999
    int vis[N], dis[N], times[N], s, t, k;
    struct Forward
    {
        int forv, forw;
    };
    struct Edge
    {
        int v, w;
        friend bool operator < (Edge x, Edge y)
        {
            return x.w > y.w;
        }
    };
    struct Astar
    {
        int asv, asw;
        friend bool operator < (Astar x, Astar y)
        {
            return x.asw+dis[x.asv] > y.asw+dis[y.asv];
        }
    };
    
    vector<Forward>vFor[N];
    vector<Forward>vRes[N];
    void dijkstra()
    {
        int i;
        memset(vis, 0, sizeof(vis));
        for(i=1; i<=1000; i++)
            dis[i]=INF;
        dis[t]=0;
        priority_queue<Edge>Q;
        Edge edge1, edge2;
        edge1.v=t; edge1.w=0;
        Q.push(edge1);
        while(!Q.empty())
        {
            edge1=Q.top();
            Q.pop();
            int v=edge1.v, w=edge1.w;
            vis[v]=1;
            for(i=0; i<vRes[v].size(); i++)
            {
                int vv=vRes[v][i].forv, ww=vRes[v][i].forw;
                if(!vis[vv]&&dis[vv]>dis[v]+ww)
                {
                    dis[vv]=dis[v]+ww;
                    edge2.v=vv;
                    edge2.w=dis[vv];
                    Q.push(edge2);
                }
            }
        }
    }
    int astar()
    {
        memset(times, 0, sizeof(times));
        Astar ss, tt;
        if(dis[s]==INF)  return -1;
        priority_queue<Astar>Q;
        ss.asv=s; ss.asw=0;
        Q.push(ss);
        while(!Q.empty())
        {
            ss=Q.top();
            Q.pop();
            int v=ss.asv;
            int w=ss.asw;
            times[v]++;
            if(v==t&&times[t]==k)
                return w;
            if(times[v]>k) continue;
            for(int i=0; i<vFor[v].size(); i++)
            {
                int forv=vFor[v][i].forv, forw=vFor[v][i].forw+w;
                tt.asv=forv, tt.asw=forw;
                Q.push(tt);
            }
        }
        return -1;
    }
    int main()
    {
        int i, j, a, b, c, m, n;
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            for(i=0; i<=1000; i++)
            {
                vFor[i].clear();
                vRes[i].clear();
            }
            for(i=0; i<m; i++)
            {
                scanf("%d%d%d", &a, &b, &c);
                Forward f;
                f.forv=b, f.forw=c;
                vFor[a].push_back(f);
                f.forv=a;
                vRes[b].push_back(f);
            }
            scanf("%d%d%d", &s, &t, &k);
            dijkstra();
            if(s==t) k++;
            printf("%d
    ", astar());
        }
        return 0;
    }
    View Code
  • 相关阅读:
    linux_ssh用户枚举猜测
    Nginx 主配置文件参数详解
    OSI七层模型
    linux-Python升级安装
    qt多线程
    python 对串口的操作
    keil 下模拟u-boot的cmd功能
    <转载>CentOS 6.3下Samba服务器的安装与配置
    Magento开发完整指南
    飞书信(Facebook Messenger)是什么?
  • 原文地址:https://www.cnblogs.com/Hilda/p/3226700.html
Copyright © 2011-2022 走看看