codeforces 677D. Vanya and Treasure(dp+bfs,黑科技)

题目链接

D. Vanya and Treasure
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vanya is in the palace that can be represented as a grid n × m. Each room contains a single chest, an the room located in the i-th row and j-th columns contains the chest of type aij. Each chest of type x ≤ p - 1 contains a key that can open any chest of type x + 1, and all chests of type 1 are not locked. There is exactly one chest of type p and it contains a treasure.

Vanya starts in cell (1, 1) (top left corner). What is the minimum total distance Vanya has to walk in order to get the treasure? Consider the distance between cell (r1, c1) (the cell in the row r1 and column c1) and (r2, c2) is equal to |r1 - r2| + |c1 - c2|.

Input
The first line of the input contains three integers n, m and p (1 ≤ n, m ≤ 300, 1 ≤ p ≤ n·m) — the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively.

Each of the following n lines contains m integers aij (1 ≤ aij ≤ p) — the types of the chests in corresponding rooms. It’s guaranteed that for each x from 1 to p there is at least one chest of this type (that is, there exists a pair of r and c, such that arc = x). Also, it’s guaranteed that there is exactly one chest of type p.

Output
Print one integer — the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type p.

Examples
input
3 4 3
2 1 1 1
1 1 1 1
2 1 1 3
output
5
input
3 3 9
1 3 5
8 9 7
4 6 2
output
22
input
3 4 12
1 2 3 4
8 7 6 5
9 10 11 12
output
11

题意:给出一个 nm n ∗ m 的矩阵,里面有一个 1p ~ 1 ~ p 的数字,代表宝藏的等级,每次获得了第 x x 等级的宝藏你可以获得第 x+1 的宝藏的钥匙,初始时所有等级为1的宝藏都是打开的,对于每个格子,即使没有打开该处宝藏,也能经过该格子,问你最少多少步能够打开任意一个等级为 p p 的宝藏。

题解:记 d[i][j] 为打开 (i,j) ( i , j ) 处宝藏的最少步数,那么我们很容易想到一个 dp d p 方程,得到每个等级 x x d 数组后,我们可以更新等级为 x+1 x + 1 d d 数组,不过这么做是 O(n2×m2) 的,引用一个黑科技,记等级为 i i 的格子有 cnt[i] 个,若 cnt[x]×cnt[x+1]<n×m c n t [ x ] × c n t [ x + 1 ] < n × m ,那么像上面一样暴力更新,否则,将所有等级为 x x 的格子作为起点,对全图做一次多源最短路,这样均摊下来复杂度是 O(nmnm) 的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=1e9;
const ll mod=1000000007;
const int maxn=300+10;
int a[maxn][maxn];
int dis[maxn][maxn],d[maxn][maxn];
vector<PII> V[maxn*maxn];
int ab(int x)
{
    return x>0? x:-x;
}
int cnt=0;
int inq[maxn][maxn];
PII q[maxn*maxn];
int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int main()
{
    int n,m,p;
    scanf("%d%d%d",&n,&m,&p);
    rep(i,1,n+1)
    {
        rep(j,1,m+1)
        {
            int v;
            scanf("%d",&v);
            V[v].pb(make_pair(i,j));
            if(v==1) d[i][j]=i-1+j-1;
            else d[i][j]=inf;
        }
    }
    rep(i,1,p)
    {
        int t=V[i].size(),t1=V[i+1].size();
        if(t*t1<n*m)
        {
            rep(k,0,t) rep(j,0,t1)
            {
                int x1=V[i][k].fi,y1=V[i][k].se,x2=V[i+1][j].fi,y2=V[i+1][j].se;
                d[x2][y2]=min(d[x2][y2],d[x1][y1]+ab(x2-x1)+ab(y2-y1));
            }
        }
        else
        {
            cnt++;
            rep(i,1,n+1) rep(j,1,m+1) dis[i][j]=inf;
            int front=0,rear=0;
            rep(k,0,t)
            {
                dis[V[i][k].fi][V[i][k].se]=d[V[i][k].fi][V[i][k].se];
                inq[V[i][k].fi][V[i][k].se]=cnt;
                q[rear++]=V[i][k];
            }
            while(front!=rear)
            {
                PII u=q[front++];
                inq[u.fi][u.se]=0;
                if(front>=maxn*maxn) front=0;
                rep(k,0,4)
                {
                    int x=u.fi+dx[k],y=u.se+dy[k];
                    if(x<1||x>n||y<1||y>m) continue;
                    if(dis[x][y]>dis[u.fi][u.se]+1)
                    {
                        dis[x][y]=dis[u.fi][u.se]+1;
                        if(inq[x][y]==cnt) continue;
                        inq[x][y]=cnt;
                        q[rear++]=PII(x,y);
                        if(rear>=maxn*maxn) rear=0;
                    }
                }
            }
            rep(k,0,t1) d[V[i+1][k].fi][V[i+1][k].se]=dis[V[i+1][k].fi][V[i+1][k].se];
        }
    }
    int ans=inf;
    rep(i,0,V[p].size()) ans=min(ans,d[V[p][i].fi][V[p][i].se]);
    printf("%d\n",ans);
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值