题目大意: 给你一个n × m 的图,有p种宝箱, 每个点上有一个种类为a[ i ][ j ]的宝箱,a[ i ][ j ] 的宝箱里有 a[ i ][ j ] + 1的钥匙,第一种宝箱是没有锁的,

第p类宝箱只有一个且里面由宝藏,你现在在(1 ,1)问你最少需要多少步才能拿到宝藏。 (n, m <= 300)

思路:这题真的好恶心啊。。。  我们考虑p类宝箱只能从p - 1类转移过来, 这样我们就能从第一类宝箱开始往后递推, 但是最坏的情况, p 类 和p - 1类,都有45000

个点, 那么复杂度就会很大。。。  所以我们要分类讨论, 对于 (p.size()) * (p + 1. size()) > n * m 的点我们在图上进行暴力转移,复杂度微n × m, 否则我们直接暴力

转移。。。

 #include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int>>
using namespace std; const int N=+;
const int M=1e4+;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9 + ; vector<pair<int, pii>> vec[N * N];
int n, m, p, idx, a[N][N], S, T, f[N * N], hs[N][N], vis[N][N];
bool flag[N][N];
int dx[] = {, -, , }, dy[] = { , , , -}; int dis(pii &a, pii &b) {
return abs(a.fi - b.fi) + abs(a.se - b.se);
} bool check(int x, int y) {
if(x < || x > n) return false;
if(y < || y > m) return false;
return true;
} void bfs(int p) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
flag[i][j] = false;
vis[i][j] = inf;
}
}
queue<piii> Q;
for(auto u : vec[p]) {
Q.push(mk(f[u.fi], u.se));
vis[u.se.fi][u.se.se] = f[u.fi];
} while(!Q.empty()) {
piii u = Q.front(); Q.pop();
for(int i = ; i < ; i++) {
piii nx = mk(vis[u.se.fi][u.se.se] + , mk(u.se.fi + dx[i], u.se.se + dy[i]));
if(check(nx.se.fi, nx.se.se)) {
if(vis[nx.se.fi][nx.se.se] > nx.fi)
{
vis[nx.se.fi][nx.se.se] = nx.fi;
if(!flag[nx.se.fi][nx.se.se]) {
flag[nx.se.fi][nx.se.se] = true;
Q.push(nx);
}
}
}
}
flag[u.se.fi][u.se.se] = false;
} for(auto u : vec[p + ]) {
f[u.fi] = vis[u.se.fi][u.se.se];
}
} int main() {
memset(f, inf, sizeof(f)); scanf("%d%d%d", &n, &m, &p);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
scanf("%d", &a[i][j]); for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
vec[a[i][j]].push_back(mk(++idx, mk(i, j)));
hs[i][j] = idx;
if(a[i][j] == p) {
S = ;
T = idx;
}
}
}
f[] = ;
vec[].push_back(mk(, mk(, )));
for(int i = ; i <= p; i++) {
if(vec[i - ].size() * vec[i].size() <= n * m) {
for(auto u : vec[i - ]) {
for(auto v : vec[i]) {
f[v.fi] = min(f[v.fi], f[u.fi] + dis(u.se, v.se));
}
}
} else {
bfs(i - );
}
}
printf("%d\n", f[T]);
return ;
}
/*
*/

Codeforces Round #355 (Div. 2) D. Vanya and Treasure的更多相关文章

  1. Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

    D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...

  2. Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块

    题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...

  3. Codeforces Round #355 (Div. 2) C. Vanya and Label 水题

    C. Vanya and Label 题目连接: http://www.codeforces.com/contest/677/problem/C Description While walking d ...

  4. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题

    B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...

  5. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

  6. Codeforces Round #355 (Div. 2)-B. Vanya and Food Processor,纯考思路~~

    B. Vanya and Food Processor time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Codeforces Round #355 (Div. 2)C - Vanya and Label

    啊啊啊啊啊啊啊,真的是智障了... 这种题目,没有必要纠结来源.只要知道它的结果的导致直接原因?反正这句话就我听的懂吧... ">>"/"&" ...

  8. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor

    菜菜菜!!!这么撒比的模拟题,听厂长在一边比比比了半天,自己想一想,然后纯模拟一下,中间过程检测一下,妥妥的就可以过. 题意:有N个东西要去搞碎,每个东西有一个高度,然后有一台机器支持里面可以达到的最 ...

  9. 水题 Codeforces Round #308 (Div. 2) A. Vanya and Table

    题目传送门 /* 水题:读懂题目就能做 */ #include <cstdio> #include <iostream> #include <algorithm> ...

随机推荐

  1. 字符编码问题mysql

    2019-02-27 07:32:17.108 ERROR 21745 --- [nio-8086-exec-2] c.h.h.rest.configurer.WebMvcConfigurer : 接 ...

  2. BZOJ 2039 [2009国家集训队]employ人员雇佣 网络流

    链接 BZOJ 2039 题解 这题建图好神,自己瞎搞了半天,最后不得不求教了企鹅学长的博客,,,,发现建图太神了!! s向每个人连sum(e[i][x]) 的边,每个人向T连a[i]的边.两两人之间 ...

  3. USACO Section 2.1 The Castle 解题报告

    题目 题目描述 有一个城堡,城堡中有若干个房间,房间与房间之间用墙来进行分隔.现在我们需要统计这个城堡有多少个房间,并且还要找出最大的房间的面积是多少(一个单元格就代表一个单元面积).城堡的主人现在想 ...

  4. luogu1082 [NOIp2012]同余方程 (扩展欧几里得)

    由于保证有解,所以1%gcd(x,y)=0,所以gcd(x,y)=1,直接做就行了 #include<bits/stdc++.h> #define pa pair<int,int&g ...

  5. 解题:HNOI 2013 Cards

    题面 除了不洗牌以外,每种洗牌方式的每个循环里的颜色必须一样,然后大力背包一下就好了.最后记得把不洗牌的方案也算进去 #include<cstdio> #include<cstrin ...

  6. Linux上vi编辑文件非正常退出后文件恢复

    Vim另存文件的命令为 编辑完文件后Esc,输入以下指令 :w filename 编辑文件时非正常退出,会生成.hello.txt.swp的文件,还有一些其他信息 恢复文件要使用以下命令: [keys ...

  7. Hadoop实战:Hadoop分布式集群部署(一)

    一.系统参数优化配置 1.1 系统内核参数优化配置 修改文件/etc/sysctl.conf,使用sysctl -p命令即时生效.   1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. ElasticStack系列之十九 & bulk时 index 和 create 的区别

    区别: 两篇文章 id 都一样的情况下,index 是将第二篇文章覆盖第一篇:create 是在第二篇插入的时候抛出一个已经存在的异常 解释: 在批量请求的时候最好使用 create 方式进行导入.假 ...

  9. node的path.join 和 path.resolve的区别

    直接上图: join resolve 明显可以看出,join只会帮你把路径连接起来,而resolve会以当前路径为父路径来把你提供的路径连接起来

  10. Python模块调用方式详解

    Python模块调用方式详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其 ...