zoukankan      html  css  js  c++  java
  • 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 25216   Accepted: 6882

    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
      
      这题就是求K短路,裸的模板题~~~
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <queue>
     6 using namespace std;
     7 
     8 const int MAXN=1010,MAXM=100010;
     9 int cnt,fir[MAXN],nxt[MAXM],to[MAXM],val[MAXM];
    10 int rcnt,rfir[MAXN],rnxt[MAXM],rto[MAXM],rval[MAXM];
    11 int dis[MAXN];
    12 struct A{
    13     int f,g,dot;
    14     bool operator <(const A a)const{
    15         return a.f<f;
    16     }
    17 };
    18 
    19 void raddedge(int a,int b,int v)
    20 {
    21     rnxt[++rcnt]=rfir[a];rto[rcnt]=b;rfir[a]=rcnt;rval[rcnt]=v;
    22 }
    23 
    24 void addedge(int a,int b,int v)
    25 {
    26     nxt[++cnt]=fir[a];to[cnt]=b;fir[a]=cnt;val[cnt]=v;
    27     raddedge(b,a,v);
    28 }
    29 int vis[MAXN];
    30 void Spfa(int src)
    31 {
    32     queue<int>q;
    33     memset(dis,127,sizeof(dis));
    34     memset(vis,0,sizeof(vis));
    35     dis[src]=0;vis[src]=1;q.push(src);
    36     while(!q.empty())
    37     {
    38         int node=q.front();q.pop();vis[node]=0;
    39         for(int i=rfir[node];i;i=rnxt[i])
    40             if(dis[rto[i]]>dis[node]+rval[i]){
    41                 dis[rto[i]]=dis[node]+rval[i];
    42                 if(!vis[rto[i]])
    43                     q.push(rto[i]);
    44                 vis[rto[i]]=1;
    45             }
    46     }
    47 }
    48 
    49 int Astar(int s,int t,int k)
    50 {
    51     int cont=0;
    52     priority_queue<A>Q;
    53     if(dis[s]==dis[0])return -1;
    54     if(s==t)k++;
    55     Q.push((A){dis[s],0,s});
    56     A node;
    57     while(!Q.empty())
    58     {
    59         node=Q.top();Q.pop();
    60         if(node.dot==t&&++cont==k)
    61             return node.f;
    62         for(int i=fir[node.dot];i;i=nxt[i])
    63             Q.push((A){dis[to[i]]+node.g+val[i],node.g+val[i],to[i]});
    64     }
    65     return -1;
    66 }
    67 
    68 void Init()
    69 {
    70     cnt=rcnt=0;
    71     memset(fir,0,sizeof(fir));
    72     memset(rfir,0,sizeof(rfir));
    73 }
    74 int main()
    75 {
    76     int x,y,k,n,m,v,s,t;
    77     while(~scanf("%d%d",&n,&m)){
    78         Init();
    79         for(int i=1;i<=m;i++){
    80             scanf("%d%d%d",&x,&y,&v);
    81             addedge(x,y,v);
    82         }
    83         scanf("%d%d%d",&s,&t,&k);
    84         Spfa(t);
    85         printf("%d
    ",Astar(s,t,k));
    86     }
    87     return 0;
    88 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    WCF开发实战系列二:使用IIS发布WCF服务
    电脑远程登录控制Android手机Webkey For Android使用教程
    WCF的https安全(ssl)访问实例
    IIS中“使用 XSL 样式表无法查看 XML 输入”问题的解决
    服务器禁止被ping的设置方法(图文)
    Windows Server 2008 R2 MSDN
    IIS7配置https
    C# 检查网络是否连通 判断远程文件是否存在 C#获取程序路径的方法中需要注意的地方
    c#,winform,treeview,选中节点,选中相应的全部子节点,取消节点,取消父节点,小技巧
    sql大全(一)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5215329.html
Copyright © 2011-2022 走看看