跳到正文

题解

P2551 题解

OIerr约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟
获赞
3
题目
P2551

版本

  1. v1当前

解题思路

光是看题目就让人不想做。但仔细分析一下,其实就是一个最短路的问题。那么怎么建模呢,这和平时的最短路有所不同。不妨把每一个状态当做一个点,包含高度与速度两个参数,那么,表格里的每一个数据都相当于连接两个状态的一条边,权值为时间。这样最大也只会产生 300 个点。用堆优化的 Dijkstra 完全可以胜任。

Code

无注释版,可放心食用~

C++
#include <bits/stdc++.h>
#define Pair pair<int,pair<int,int> >
#define N 300+10
#define M 90000+10
using namespace std;
int n,m,num,head[N][N],dis[N][N],vis[N][N],hm,dh,pre[N][N],ans[N];
pair<int,int> s,t;
struct Edge{
    int dis,next,exi,n;
    pair<int,int> to,from;
}edge[M];
void add(pair<int,int> from,pair<int,int> to,int dis,int n)
{
    edge[++num].next=head[from.first][from.second];
    edge[num].to=to;
    edge[num].dis=dis;
    edge[num].n=n;
    edge[num].from=from;
    head[from.first][from.second]=num;
}
void doing()
{
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    priority_queue<Pair,vector<Pair>,greater<Pair> > h;
    for(int i=0;i<=6;i++)
        for(int j=0;j<=m;j++)
     dis[i][j]=2147483647;
    dis[s.first][s.second]=0;
    h.push(Pair(dis[s.first][s.second],s));
    while(h.size()>0)
    {
        pair<int,int> k=h.top().second;h.pop();
        if(vis[k.first][k.second]) continue;
        vis[k.first][k.second]=1;
        for(int i=head[k.first][k.second];i;i=edge[i].next)
        if(dis[k.first][k.second]+edge[i].dis<=dis[edge[i].to.first][edge[i].to.second])
        {
            dis[edge[i].to.first][edge[i].to.second]=dis[k.first][k.second]+edge[i].dis;
            pre[edge[i].to.first][edge[i].to.second]=i;
            h.push(Pair(dis[edge[i].to.first][edge[i].to.second],edge[i].to));
        }
    }
}
int main()
{
    scanf("%d%d%d%d%d%d",&s.second,&s.first,&t.second,&t.first,&dh,&hm);
    m=hm/dh;s.second/=dh;t.second/=dh;
    for(int h=0;h<=m-1;h++)
    {
        for(int v=1;v<=6;v++)
        {
            int x;
            pair<int,int> a,b;
            scanf("%d",&x);
            a.first=v;a.second=h;
            b.first=v;b.second=h+1;
            add(a,b,x,'R');
        }
    }
    for(int h=0;h<=m;h++)
    {
        for(int v=1;v<=5;v++)
        {
            int x;pair<int,int> a,b;
            scanf("%d",&x);
            a.first=v;a.second=h;
            b.first=v+1;b.second=h;
            add(a,b,x,'A');
        }
    }
    for(int h=2;h<=m;h++)
    {
        for(int v=1;v<=5;v++)
        {
            int x;pair<int,int> a,b;
            scanf("%d",&x);
            a.first=v;a.second=h;
            b.first=v+1;b.second=h-1;
            add(a,b,x,'D');
        }
    }
    doing();
    printf("%d\n",dis[t.first][t.second]);
    for(int i=pre[t.first][t.second];i;i=pre[edge[i].from.first][edge[i].from.second])ans[++ans[0]]=edge[i].n;
    for(int i=ans[0];i>=1;i--)printf("%c",ans[i]);
	printf("\n");
    return 0;
}

后记

record

原文