zoukankan      html  css  js  c++  java
  • 【bzoj1598/Usaco2008 Mar】牛跑步——A*

    Description

    BESSIE准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘, 然后走回牛棚. BESSIE也不想跑得太远,所以她想走最短的路经. 农场上一共有M (1 <= M <= 10,000)条路, 每条路连接两个用1..N(1 <= N <= 1000)标号的地点. 更方便的是,如果X>Y,则地点X的高度大于地点Y的高度. 地点N是BESSIE的牛棚;地点1是池塘. 很快, BESSIE厌倦了一直走同一条路.所以她想走不同的路,更明确地讲,她想找出K (1 <= K <= 100)条不同的路经.为了避免过度劳累,她想使这K条路经为最短的K条路经. 请帮助BESSIE找出这K条最短路经的长度.你的程序需要读入农场的地图, 一些从X_i到Y_i 的路经和它们的长度(X_i, Y_i, D_i). 所有(X_i, Y_i, D_i)满足(1 <= Y_i < X_i; Y_i < X_i <= N, 1 <= D_i <= 1,000,000).

    Input

    * 第1行: 3个数: N, M, 和K

    * 第 2..M+1行: 第 i+1 行包含3个数 X_i, Y_i, 和 D_i, 表示一条下坡的路.

    Output

    * 第1..K行: 第i行包含第i最短路经的长度,或-1如果这样的路经不存在.如果多条路经有同样的长度,请注意将这些长度逐一列出.

    Sample Input

    5 8 7
    5 4 1
    5 3 1
    5 2 1
    5 1 1
    4 3 4
    3 1 1
    3 2 1
    2 1 1

    Sample Output

    1
    2
    2
    3
    6
    7
    -1

    HINT

    输出解释:

    路经分别为(5-1), (5-3-1), (5-2-1), (5-3-2-1), (5-4-3-1),
    (5-4-3-2-1).


    这道题就是A*的模版题(求k短路),先建反向边求出每个点到终点1的最短路,对于每条路径的估价定义为这个点到n走过的距离加上这个点到终点的最短路,每次从堆里取出最小值直到走到1时统计答案。

    代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #include<algorithm>
     5 #define mem(a,p) memset(a,p,sizeof(a))
     6 const int N=1005,M=1e4+10;
     7 int n,m,k,dis[N],fir[N],fi[N],tot=0;
     8 struct point{int ne,to,d;}e[M*2],p[M*2];
     9 struct node{
    10     int to,d;
    11     bool operator <(const node&p)const{return p.d<d;}
    12 };
    13 int read(){
    14     int ans=0,f=1;char c=getchar();
    15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    16     while(c>='0'&&c<='9'){ans=ans*10+c-48;c=getchar();}
    17     return ans*f;
    18 }
    19 void ins(int u,int v,int w){
    20     e[++tot]=(point){fir[u],v,w};fir[u]=tot;
    21     p[tot]=(point){fi[v],u,w};fi[v]=tot;
    22 }
    23 std::priority_queue<node>q;
    24 int ans[105],sum=0;
    25 void clear(){while(!q.empty())q.pop();}
    26 void dj(){
    27     dis[1]=0;q.push((node){1,0});
    28     while(!q.empty()){
    29         node rt=q.top();q.pop();
    30         int x=rt.to,w=rt.d;
    31         if(dis[x]!=w)continue;
    32         for(int i=fi[x];i;i=p[i].ne){
    33             int to=p[i].to;
    34             if(dis[to]>dis[x]+p[i].d)dis[to]=dis[x]+p[i].d,q.push((node){to,dis[to]});
    35         }
    36     }
    37 }
    38 void astar(){
    39     if(dis[n]==dis[0])return;
    40     q.push((node){n,dis[n]});
    41     while(!q.empty()){
    42         node rt=q.top();q.pop();
    43         if(rt.to==1){
    44             ans[++sum]=rt.d;
    45             if(sum==k)return;
    46         }
    47         int x=rt.to;rt.d-=dis[x];
    48         for(int i=fir[x];i;i=e[i].ne){
    49             int to=e[i].to;
    50             if(dis[to]==dis[0])continue;
    51             q.push((node){to,dis[to]+rt.d+e[i].d});
    52         }
    53     }
    54 }
    55 int main(){
    56     n=read();m=read();k=read();
    57     for(int i=1,a,b,c;i<=m;i++){
    58         a=read();b=read();c=read();
    59         ins(a,b,c);
    60     }
    61     mem(dis,127);
    62     dj();clear();
    63     astar();
    64     for(int i=1;i<=sum;i++)printf("%d
    ",ans[i]);
    65     for(int i=sum+1;i<=k;i++)printf("-1
    ");
    66     return 0;
    67 }
    bzoj1598
  • 相关阅读:
    Qt内存回收机制
    Qt坐标系统
    Qt信号与槽的使用
    Qt初始化代码基本说明
    Qt下载、安装及环境搭建
    搭建CentOs7的WebServer
    Laying out a webpage is easy with flex
    Let's write a framework.
    使用go语言开发一个后端gin框架的web项目
    个人总结的J2EE目前知道的涵盖面,从大方向入手,多写单元测试,加强基础
  • 原文地址:https://www.cnblogs.com/JKAI/p/7695749.html
Copyright © 2011-2022 走看看