Codeforces 677D Vanya and Treasure(DP+分治) category:

题意:

一个图上有一些宝箱,每一个级别的箱子里有搞一个级别箱子的钥匙,而最高级别的里面有宝藏,每次从(1,1)出发,问你拿到宝藏所要走的最短距离是多少。

解法:

这题如果直接来暴力更新的话,复杂度要炸,但是我们可以发现,每一次更新的时候,之和当前级别和前一个级别有关,所以如果两个级别的数量加起来大于m*n的话,我们就直接bfs就好了,这样可以把复杂度降到mnsqrt(nm)。话说这个方法我以前也看到过,就是按数据情况来分不懂的算法处理来保证复杂度。。
这个bfs的时候,我们也要注意的就是,因为同一个级别的点在bfs树中不在同一层,所以我们不能把他们在最开始的时候一起放到队列里面,而是要放一个最小的,然后随着深度的增加再添加其他的。

//
//  Created by  CQU_CST_WuErli
//  Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.
//
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <sstream>
#define OFF(x) memset(x,-1,sizeof(x))
#define MEM(x,a) memset((x),(a),sizeof(x))
#define BUG cout << "I am here" << endl
#define lookln(x) cout << #x << "=" << x << endl
#define SI(a) scanf("%d", &a)
#define SII(a,b) scanf("%d%d", &a, &b)
#define SIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
const int INF_INT=0x3f3f3f3f;
const long long INF_LL=0x7f7f7f7f;
const int MOD=1e9+7;
const double eps=1e-10;
const double pi=acos(-1);
typedef long long  ll;
using namespace std;

int n, m, p;
int a[333][333];
vector<pair<int, int> > g[333 * 333];
int dp[333][333];

int d[333][333];

int dis(pair<int, int> a, pair<int, int> b) {
    return abs(a.first - b.first) + abs(a.second - b.second);
}

struct P {
    int x, y, val;
    P(int x, int y, int val):x(x), y(y), val(val) {}
    P() {}
    bool operator < (const P& rhs) const {
        return val < rhs.val;
    }
};

int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};

void bfs(int c) {
    OFF(d);
    vector<P> vc, q;
    for (auto& it : g[c - 1]) {
        vc.push_back(P(it.first, it.second, dp[it.first][it.second]));  }
    sort(vc.begin(), vc.end());
    int cnt = 0, tmp = 1;
    q.push_back(vc[0]);
    d[q[0].x][q[0].y] = dp[q[0].x][q[0].y];
    while(cnt < q.size()) {
        int x = q[cnt].x, y = q[cnt].y, val = q[cnt].val;
        cnt++;
        while (tmp < vc.size() && vc[tmp].val <= val) {
            q.push_back(vc[tmp++]);
        }
        for (int i = 0; i < 4; i++) {
            int tx = x + dir[i][0], ty = y + dir[i][1];
            if (tx < 1 || ty < 1 || tx > n || ty > m || d[tx][ty] != -1) continue;
            d[tx][ty] = val + 1;
            q.push_back(P(tx, ty, d[tx][ty]));
        }
    }
    for (auto& it : g[c]) {
        dp[it.first][it.second] = d[it.first][it.second];
    }
}

int main(int argc, char const *argv[]) {
#ifdef LOCAL
    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);
    // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);
#endif
    while(SIII(n, m, p) == 3) {
        for (int i = 1; i <= p; i++) g[i].clear();
        MEM(dp, INF_INT);
        int tx, ty;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                SI(a[i][j]);
                g[a[i][j]].push_back(make_pair(i, j));
                if (a[i][j] == 1) {
                    dp[i][j] = i + j - 2;
                }
                if (a[i][j] == p)
                    tx = i, ty = j;
            }
        }
        for (int i = 2; i <= p; i++) {
            if (g[i].size() * g[i - 1].size() <= m * n) {
                for (auto& u : g[i]) {
                    for (auto& v : g[i - 1]) {
                        dp[u.first][u.second] = min(dp[u.first][u.second], dp[v.first][v.second] + dis(u, v));
                    }
                }
            }
            else bfs(i);
        }
        printf("%d\n", dp[tx][ty]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值