zoukankan      html  css  js  c++  java
  • K短路模板POJ 2449 Remmarguts' Date

     
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions:32863   Accepted: 8953

    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”,解果一直WA
    板子不会多打几遍。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 #include<algorithm>
     6 #include<cmath>
     7 #define ll long long
     8 #define inf 1047483600
     9 #define mod 317847191
    10 using namespace std;
    11 inline int read()
    12 {
    13     int x=0,w=1;char ch=getchar();
    14     while(!isdigit(ch)){if(ch=='-') w=-1;ch=getchar();}
    15     while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch-'0'),ch=getchar();
    16     return x*w;
    17 }
    18 const int N=2000010;
    19 struct node{
    20     int u,v,c,ne;
    21 }e[N],e2[N];
    22 int h[N],h2[N],tot,n,m;
    23 void add(int u,int v,int c)
    24 {
    25     tot++;e[tot]=(node){u,v,c,h[u]};h[u]=tot;
    26     e2[tot]=(node){v,u,c,h2[v]};h2[v]=tot;
    27 }
    28 int S,T,K;
    29 int d[N];
    30 bool v[N];
    31 struct kk{
    32     int id,f,g;
    33     bool operator<(const kk&x)const{
    34       if(f!=x.f) return f>x.f;
    35       else return g>x.g;
    36     }
    37 };
    38 priority_queue<kk>q;
    39 void spfa(int s)
    40 {
    41     queue<int>q;
    42     for(int i=1;i<=n;++i) d[i]=inf;
    43     d[s]=0;v[s]=1;q.push(s);
    44     while(!q.empty())
    45     {
    46         int ff=q.front();q.pop();v[ff]=0;
    47         for(int i=h2[ff];i;i=e2[i].ne)
    48         {
    49             int rr=e2[i].v;
    50             if(d[rr]>d[ff]+e2[i].c)
    51             {
    52                 d[rr]=d[ff]+e2[i].c;
    53                 if(!v[rr]) v[rr]=1,q.push(rr);
    54             }
    55         }
    56     }
    57 }
    58 void A_star(int S,int T,int K)
    59 {
    60     if(S==T) K++;
    61     int cnt=0;
    62     q.push((kk){S,0,0});
    63     while(!q.empty())
    64     {
    65         kk ff=q.top();q.pop();
    66         if(ff.id==T)
    67         {
    68             cnt++;
    69             if(cnt==K){printf("%d",ff.f);exit(0);}
    70         }
    71         for(int i=h[ff.id];i;i=e[i].ne)
    72         {
    73             kk rr;rr.id=e[i].v;
    74             rr.g=ff.g+e[i].c;
    75             rr.f=rr.g+d[rr.id];
    76             q.push(rr);
    77         }
    78     }
    79     cout<<"-1";
    80 }
    81 int main()
    82 {
    83     n=read();m=read();
    84     for(int i=1;i<=m;++i)
    85     {
    86         int x,y,z;x=read();y=read();z=read();
    87         add(x,y,z);
    88     }
    89     S=read();T=read();K=read();
    90     spfa(T);
    91     A_star(S,T,K);
    92     return 0;
    93 }

    败后或反成功,顾拂心处莫便放手。

  • 相关阅读:
    分布式服务框架的雪崩问题
    分布式系统中的幂等性
    Exception引起的性能问题
    TFS2017新特性(一)
    云平台架构变迁
    MQ基本概念
    SVN版本管理
    1年内4次架构调整,谈Nice的服务端架构变迁之路
    鏖战双十一-阿里直播平台面临的技术挑战
    统一日志平台初探
  • 原文地址:https://www.cnblogs.com/adelalove/p/8463986.html
Copyright © 2011-2022 走看看