zoukankan      html  css  js  c++  java
  • POJ 2449 Remmarguts' Date(第k短路のA*算法)

    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.
     
    题目大意:求S到T的第K短路
    思路:A*算法,f(n) = h(n) + g(n),h(n)为从S走过重重关卡到点n的距离,g(n)为从n走到T的最短距离,在这里估价函数是完美的,所以是不会出错的。至于第k短路,只要找第k次走到终点即可。
    PS:S == T的情况下,k要加一,因为不走好像是不算的……英文抓鸡完全看不到题目有这么说过>_<
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <utility>
     5 using namespace std;
     6 
     7 const int INF = 0x3f3f3f3f;
     8 const int MAXN = 1010;
     9 const int MAXM = 100010;
    10 
    11 #define X first
    12 #define Y second
    13 
    14 typedef pair<int, int> PII;
    15 
    16 int head[MAXN], rhead[MAXN];
    17 int next[MAXM], rnext[MAXM], to[MAXM], rto[MAXM], cost[MAXM];
    18 int ecnt;
    19 
    20 void init() {
    21     ecnt = 1;
    22     memset(head, 0, sizeof(head));
    23     memset(rhead, 0, sizeof(rhead));
    24 }
    25 
    26 void add_edge(int u, int v, int c) {
    27     cost[ecnt] = c;
    28     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt;
    29     rto[ecnt] = u; rnext[ecnt] = rhead[v]; rhead[v] = ecnt++;
    30 }
    31 
    32 int dis[MAXN];
    33 bool vis[MAXN];
    34 
    35 void dijkstra(int st, int n) {
    36     for(int i = 1; i <= n; ++i) dis[i] = INF;
    37     memset(vis, 0, sizeof(vis));
    38     priority_queue<PII> Q;
    39     dis[st] = 0; Q.push(make_pair(0, st));
    40     while(!Q.empty()) {
    41         int u = Q.top().Y; Q.pop();
    42         if(vis[u]) continue;
    43         vis[u] = true;
    44         for(int p = rhead[u]; p; p = rnext[p]) {
    45             int v = rto[p];
    46             if(dis[v] > dis[u] + cost[p]) {
    47                 dis[v] = dis[u] + cost[p];
    48                 Q.push(make_pair(-dis[v], v));
    49             }
    50         }
    51     }
    52 }
    53 
    54 int a_star(int st, int ed, int n, int k) {
    55     priority_queue<PII> Q;
    56     Q.push(make_pair(-dis[st], st));
    57     while(!Q.empty()) {
    58         int u = Q.top().Y, c = -Q.top().X; Q.pop();
    59         if(u == ed && --k == 0) return c;
    60         for(int p = head[u]; p; p = next[p])
    61             Q.push(make_pair(-(c - dis[u] + cost[p] + dis[to[p]]), to[p]));
    62     }
    63     return -1;
    64 }
    65 
    66 int main() {
    67     int n, m, st, ed, k;
    68     while(scanf("%d%d", &n, &m) != EOF) {
    69         init();
    70         for(int i = 0; i < m; ++i) {
    71             int u, v, c;
    72             scanf("%d%d%d", &u, &v, &c);
    73             add_edge(u, v, c);
    74         }
    75         scanf("%d%d%d", &st, &ed, &k);
    76         dijkstra(ed, n);
    77         if(dis[st] == INF) {
    78             printf("-1
    ");
    79             continue;
    80         }
    81         if(st == ed) ++k;
    82         printf("%d
    ", a_star(st, ed, n, k));
    83     }
    84 }
    View Code
  • 相关阅读:
    js函数柯理化
    Promise异步编程解决方案
    set和map结构,class类
    原创:用node.js搭建本地服务模拟接口访问实现数据模拟
    原创:微信小程序如何使用自定义组件
    原创:微信小程序开发要点总结
    Nodejs CMS——基于 NestJS/NuxtJS 的完整开源项目
    浅谈js对象之数据属性、访问器属性、Object.defineProperty方法
    Promise初步详解(resolve,reject,catch)
    原生js面向对象实现简单轮播
  • 原文地址:https://www.cnblogs.com/oyking/p/3223069.html
Copyright © 2011-2022 走看看