晴天小猪历险记之Hill(Dijkstra优先队列优化)

描述

这一天,他来到了一座深山的山脚下,因为只有这座深山中的一位隐者才知道这种药草的所在。但是上山的路错综复杂,由于小小猪的病情,晴天小猪想找一条需时最少的路到达山顶,但现在它一头雾水,所以向你求助。

山用一个三角形表示,从山顶依次向下有1段、2段、3段等山路,每一段用一个数字T(1<=T<=100)表示,代表晴天小猪在这一段山路上需要爬的时间,每一次它都可以朝左、右、左上、右上四个方向走。山是环形的。(注意:在任意一层的第一段也可以走到本层的最后一段或上一层的最后一段)。

晴天小猪从山的左下角出发,目的地为山顶,即隐者的小屋。

格式

输入格式

第一行有一个数n(2<=n<=1000),表示山的高度。

从第二行至第n+1行,第i+1行有i个数,每个数表示晴天小猪在这一段山路上需要爬的时间。

输出格式

一个数,即晴天小猪所需要的最短时间。

样例1

样例输入1[复制]

 
5
1
2 3
4 5 6
10 1 7 8
1 1 4 5 6

样例输出1[复制]

 
10

限制

各个测试点1s

思路:最短路,关键是建图,建图之后用Dijkstra优先队列优化最短路一下即可。

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<queue>
  5 #include<stdlib.h>
  6 #include<string.h>
  7 #include<queue>
  8 #include<vector>
  9 using namespace std;
 10 void dj(int n,int ans);
 11 typedef long long LL;
 12 int ma[1002][1002];
 13 int biao[1002][1002];
 14 int d[600000];
 15 typedef struct node
 16 {int to;
 17  int cost;
 18  bool operator<(node&cx)const
 19  {
 20      return cx.cost<cost;
 21  }
 22 }ss;
 23 typedef pair<int,int>P;
 24 vector<ss>vec[600000];
 25 priority_queue<P,vector<P>,greater<P> >que;
 26 int main(void)
 27 {
 28     int i,j,k,p,q;
 29     scanf("%d",&k);
 30     int cnt=1;
 31     for(i=1; i<=k; i++)
 32     {
 33         for(j=1; j<=i; j++)
 34         {
 35             scanf("%d",&ma[i][j]);
 36             biao[i][j]=cnt++;
 37         }
 38     }ss MM;
 39     for(i=k; i>=2; i--)
 40     {
 41         for(j=1; j<=i; j++)
 42         {
 43             if(j==1)
 44             {
 45                 int x=biao[i][j];
 46                 int y=biao[i][j+1];
 47                 int z=biao[i][i];
 48                 int xx=biao[i-1][j];
 49                 int uu=biao[i-1][i-1];
 50                 MM.to=y;MM.cost=ma[i][j+1];
 51                 vec[x].push_back(MM);
 52                  MM.to=z;MM.cost=ma[i][i];
 53                  vec[x].push_back(MM);
 54                 MM.to=xx;MM.cost=ma[i-1][j];
 55                 vec[x].push_back(MM);
 56                 MM.to=uu;MM.cost=ma[i-1][i-1];
 57                 vec[x].push_back(MM);
 58             }
 59             if(j==i)
 60             {
 61                 int x=biao[i][j];
 62                 int y=biao[i][j-1];
 63                 int z=biao[i-1][j-1];
 64                 int xx=biao[i][1];
 65                 int yy=biao[i-1][1];
 66                 MM.to=y;MM.cost=ma[i][j-1];
 67                 vec[x].push_back(MM);
 68                 MM.to=z;MM.cost=ma[i-1][j-1];
 69                 vec[x].push_back(MM);
 70                 MM.to=xx;MM.cost=ma[i][1];
 71                  vec[x].push_back(MM);
 72                  MM.to=yy;MM.cost=ma[i-1][1];
 73                   vec[x].push_back(MM);
 74             }
 75             else
 76             {
 77                 int x=biao[i][j];
 78                 int y=biao[i][j-1];
 79                 int z=biao[i][j+1];
 80                 int xx=biao[i-1][j];
 81                 int yy=biao[i-1][j-1];
 82                 MM.to=y;MM.cost=ma[i][j-1];
 83                 vec[x].push_back(MM);
 84                 MM.to=z;MM.cost=ma[i][j+1];
 85                 vec[x].push_back(MM);
 86                 MM.to=xx;MM.cost=ma[i-1][j];
 87                 vec[x].push_back(MM);
 88                 MM.to=yy;MM.cost=ma[i-1][j-1];
 89                 vec[x].push_back(MM);
 90             }
 91         }
 92     }dj(k,biao[k][i]);
 93        printf("%d ",d[1]);
 94     return 0;
 95 }
 96 void dj(int n,int ans)
 97 {   int N=1e8;
 98     fill(d,d+600000,N);
 99     d[ans]=ma[n][1];
100     que.push(P(d[ans],ans));
101     while(!que.empty())
102     {P UU=que.top();que.pop();
103     int v=UU.second;
104     if(d[v]<UU.first)continue;
105     for(int i=0;i<vec[v].size();i++)
106     {
107         node K=vec[v][i];;
108         if(K.cost+d[v]<d[K.to])
109         {
110             d[K.to]=K.cost+d[v];
111             que.push(P(d[K.to],K.to));
112         }
113     }
114     }
115 }
油!油!you@
原文地址:https://www.cnblogs.com/zzuli2sjy/p/5218103.html