题目链接
 题目描述
有一盗墓者潜入一金字塔盗宝。当她(难道是Lara Croft ?)打开一个宝箱的时候,突然冒出一阵烟(潘多拉的盒子?),她迅速意识到形势不妙,三十六计走为上计……由于她盗得了金字塔的地图,所以她希望能找出最佳逃跑路线。地图上标有N个室,她现在就在1室,金字塔的出口在N室。她知道一个秘密:那阵烟会让她在直接连接某两个室之间的通道内的行走速度减半。她希望找出一条逃跑路线,使得在最坏的情况下所用的时间最少。
 题解
这道题可以用A*来做
估价函数由两个值组成:该节点到终点的最短距离和当前已经经过路径的最大值(在代码里估价函数为g,实际距离为step,当前节点位置为x,已经经过路径的边权的最大值为maxx)
 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   | #include < cstdio > #include < cstring > #include < algorithm > #include < queue> using namespace std; const int maxn=100005; struct Node {     int to,next,dis; } edge[maxn<<1]; struct node {     int g,step,maxx,x;     bool operator < (node xx) const     {         return xx.g<g;     } }; int k=0,head[maxn],n,m,a,b,c,dist[maxn]; bool visit[maxn]; void add(int u,int v,int w) {     edge[++k].to=v;     edge[k].next=head[u];     edge[k].dis=w;     head[u]=k; } void spfa() {     memset(dist,0x7f7f7f7f,sizeof(dist));     queue<int> q;visit[n]=true;q.push(n);dist[n]=0;     while(!q.empty())     {         int x=q.front();q.pop();visit[x]=false;         for(int i=head[x];i;i=edge[i].next)         {             if(dist[edge[i].to]>dist[x]+edge[i].dis)             {                 dist[edge[i].to]=dist[x]+edge[i].dis;                 if(!visit[edge[i].to])                 {                     visit[edge[i].to]=true;                     q.push(edge[i].to);                 }             }         }     } } void Astar() {     priority_queue<node> q;node temp;temp.step=0;     temp.g=dist[1];temp.maxx=0;temp.x=1;q.push(temp);     while(!q.empty())     {         node now=q.top();q.pop();         if(now.x==n)         {             printf("%d\n",now.step+now.maxx);             return;         }         for(int i=head[now.x];i;i=edge[i].next)         {             temp.step=now.step+edge[i].dis;             temp.maxx=max(now.maxx,edge[i].dis);             temp.g=temp.step+temp.maxx+dist[edge[i].to];             temp.x=edge[i].to;q.push(temp);         }     } } int main() {     scanf("%d%d",&n,&m);     for(int i=1;i<=m;i++)     {         scanf("%d%d%d",&a,&b,&c);         add(a,b,c);add(b,a,c);     }     spfa();     Astar();     return 0; }
   |