zoukankan      html  css  js  c++  java
  • poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449

    "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. 

    题意描述:王子和喜欢的女孩儿在不同的城堡里,王子为了见女孩儿,必须从自己的城堡走第K条最短的路径到达女孩儿所在的城堡里。求第K条最短路径的长度。

    算法分析:K短路的模板题,一般运用A*算法求解。

    说明:这道题的K达1000之多,应该是POJ上面这道题的数据不强吧,数据极限的时候估计A*TLE吧。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<queue>
      8 #define inf 0x7fffffff
      9 using namespace std;
     10 const int maxn=1000+10;
     11 const int M = 100000+10;
     12 
     13 int n,m,from,to,K;
     14 struct Edge
     15 {
     16     int to,w;
     17     int next;
     18 }edge[M*4],edge2[M*4];
     19 int head[maxn],edgenum;
     20 int head2[maxn],edgenum2;
     21 
     22 void add(int u,int v,int w)
     23 {
     24     edge[edgenum].to=v ;edge[edgenum].w=w;
     25     edge[edgenum].next=head[u] ;head[u]=edgenum++ ;
     26 }
     27 void add2(int u,int v,int w)
     28 {
     29     edge2[edgenum2].to=v ;edge2[edgenum2].w=w;
     30     edge2[edgenum2].next=head2[u] ;head2[u]=edgenum2++ ;
     31 }
     32 
     33 int dis[maxn],vis[maxn];
     34 void spfa()
     35 {
     36     for (int i=1 ;i<=n ;i++) {dis[i]=inf ;vis[i]=0 ; }
     37     queue<int> que;
     38     que.push(to);
     39     vis[to]=1;
     40     dis[to]=0;
     41     while (!que.empty())
     42     {
     43         int u=que.front() ;que.pop() ;
     44         vis[u]=0;
     45         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
     46         {
     47             int v=edge[i].to;
     48             if (dis[v]>dis[u]+edge[i].w)
     49             {
     50                 dis[v]=dis[u]+edge[i].w;
     51                 if (!vis[v])
     52                 {
     53                     vis[v]=1;
     54                     que.push(v);
     55                 }
     56             }
     57         }
     58     }
     59     return ;
     60 }
     61 
     62 struct node
     63 {
     64     int to,g,f;///评估函数: f=g+h;
     65     friend bool operator < (node a,node b)
     66     {
     67         if (a.f != b.f) return a.f > b.f;
     68         return a.g > b.g;
     69     }
     70 }cur,tail;
     71 
     72 int A_star()
     73 {
     74     if (from==to) K++;
     75     if (dis[from]==inf) return -1;
     76     priority_queue<node> Q;
     77     cur.to=from ;cur.g=0 ;cur.f=cur.g+dis[from];
     78     Q.push(cur);
     79     int cnt=0;
     80     while (!Q.empty())
     81     {
     82         cur=Q.top() ;Q.pop() ;
     83         int u=cur.to;
     84         if (u==to) cnt++;
     85         if (cnt==K) return cur.g;
     86         for (int i=head2[u] ;i!=-1 ;i=edge2[i].next)
     87         {
     88             tail.to=edge2[i].to;
     89             tail.g=cur.g+edge2[i].w;
     90             tail.f=tail.g+dis[edge2[i].to ];
     91             Q.push(tail);
     92         }
     93     }
     94     return -1;
     95 }
     96 
     97 //int flag[maxn][maxn];
     98 int main()
     99 {
    100     while (scanf("%d%d",&n,&m)!=EOF)
    101     {
    102         memset(head,-1,sizeof(head));
    103         memset(head2,-1,sizeof(head2));
    104         edgenum=0;
    105         edgenum2=0;
    106         //memset(flag,0,sizeof(flag));
    107         int a,b,c;
    108         for (int i=0 ;i<m ;i++)
    109         {
    110             scanf("%d%d%d",&a,&b,&c);
    111             //if (flag[a][b]) continue;
    112             //flag[a][b]=1;
    113             add(b,a,c);
    114             add2(a,b,c);
    115         }
    116         scanf("%d%d%d",&from,&to,&K);
    117         spfa();
    118         int ans=A_star();
    119         printf("%d
    ",ans);
    120     }
    121     return 0;
    122 }
  • 相关阅读:
    第48天-shell(2013.09.25)
    嵌入式Linux gdb core dump快速定位程序crash问题
    va_arg va_start va_end 获取函数参数内容
    Ubunut apache2 服务器 搭建 文件下载
    CRC16 串口校验
    Linux 处理中文字符串 :/区分中文和英文的方法
    LINUX 工具移植:scp 快速拷贝文件到开发板
    打印内存变量定位 程序出现内存方面的问题
    Linux C 调用 ping命令 检测网络状态
    Ubuntu Gitlab 备份代码服务器
  • 原文地址:https://www.cnblogs.com/huangxf/p/4362368.html
Copyright © 2011-2022 走看看