zoukankan      html  css  js  c++  java
  • POJ——T 2449 Remmarguts' Date

    http://poj.org/problem?id=2449

    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 30754   Accepted: 8394

    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

    Source

    POJ Monthly,Zeyuan Zhu
     
    K短路模板
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <queue>
     6 
     7 using namespace std;
     8 
     9 const int INF(0x3f3f3f3f);
    10 const int M(100005);
    11 const int N(1005);
    12 int n,m,s,t,k;
    13 int sumedge,had[N],hed[N];
    14 struct Edge
    15 {
    16     int v,next,w;
    17 }edge1[M<<1],edge2[M<<1];
    18 inline void ins(int u,int v,int w)
    19 {
    20     edge1[++sumedge].v=v;
    21     edge1[sumedge].next=hed[u];
    22     edge1[sumedge].w=w;
    23     hed[u]=sumedge;
    24     edge2[sumedge].v=u;
    25     edge2[sumedge].next=had[v];
    26     edge2[sumedge].w=w;
    27     had[v]=sumedge;
    28     
    29 }
    30 
    31 int dis[N];
    32 bool inq[N];
    33 void  SPFA(int s)
    34 {
    35     for(int i=1;i<=n;i++) dis[i]=INF;
    36     queue<int>que; que.push(s);
    37     inq[s]=true; dis[s]=0;
    38     for(;!que.empty();)
    39     {
    40         int u=que.front();
    41         que.pop(); inq[u]=0;
    42         for(int v,i=had[u];i;i=edge2[i].next)
    43         {
    44             v=edge2[i].v;
    45             if(dis[v]>dis[u]+edge2[i].w)
    46             {
    47                 dis[v]=dis[u]+edge2[i].w;
    48                 if(!inq[v]) que.push(v),inq[v]=1;
    49             }
    50         }
    51     }
    52 }
    53 
    54 struct Node
    55 {
    56     int g,f,to;
    57     bool operator < (const Node x) const
    58     {
    59         if(f==x.f) return g>x.g;
    60         return f>x.f;
    61     }
    62 };
    63 int Astar()
    64 {
    65     priority_queue<Node>que;
    66     if(dis[s]==INF) return -1;
    67     int cnt=0; Node now,v;
    68     now.f=dis[s];now.g=0;now.to=s;
    69     que.push(now);
    70     for(;!que.empty();)
    71     {
    72         now=que.top(); que.pop();
    73         if(now.to==t) cnt++;
    74         if(cnt==k) return now.g;
    75         for(int i=hed[now.to];i;i=edge1[i].next)
    76         {
    77              v.to=edge1[i].v;
    78             v.g=now.g+edge1[i].w;
    79             v.f=v.g+dis[edge1[i].v];
    80             que.push(v);
    81         }
    82     }
    83     return -1;
    84 }
    85 
    86 int main()
    87 {
    88     scanf("%d%d",&n,&m);
    89     for(int u,v,w;m--;)
    90     {
    91         scanf("%d%d%d",&u,&v,&w);
    92         ins(u,v,w);
    93     }
    94     scanf("%d%d%d",&s,&t,&k);
    95     if(s==t) k++; SPFA(t);
    96     printf("%d
    ",Astar());
    97     return 0;
    98 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    队列:队列在线程池等有限资源池中的应用
    栈:如何实现浏览器的前进和后退
    如何优雅的写出链表?
    数据结构与算法之美(python)(课程整理A-02)
    数据结构与算法之美(python)(课程整理A-01)
    django 数据库迁移成功 但是表没有创建
    beego框架学习-000001(go get下载速度过慢、导包及其初始化问题)
    【转载】HTML5自定义data属性
    【转载】OAuth的机制原理讲解及开发流程
    浏览器的同源策略
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7416331.html
Copyright © 2011-2022 走看看