题目大意: 给你一个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. 【刷题】BZOJ 4000 [TJOI2015]棋盘

    Description Input 输入数据的第一行为两个整数N,M表示棋盘大小.第二行为两个整数P,K, 表示攻击范围模板的大小,以及棋子在模板中的位置.接下来三行, 每行P个数,表示攻击范围的模版 ...

  2. HGOI 20181103 题解

    problem:把一个可重集分成两个互异的不为空集合,两个集合里面的数相乘的gcd为1(将集合中所有元素的质因数没有交集) solution:显然本题并不是那么容易啊!考场上想了好久.. 其实转化为上 ...

  3. 洛谷 P2245 星际导航 解题报告

    P2245 星际导航 题目描述 sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为了方便起见,我们可以认为宇宙是一张有N 个顶点和M 条边的带权无向 ...

  4. linux查看进程的线程数

    top -H -p $PID  #查看对应进程的那个线程占用CPU过高 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行 ...

  5. 关闭centos自动升级内核

    八.关闭Centos自动更新chkconfig --list yum-updatesd  #显示当前系统状态yum-updatesd    0:关闭  1:关闭  2:启用  3:启用  4:启用  ...

  6. 35个java代码性能优化总结

    前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑 的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用 ...

  7. 阿里云Tengine和Openresty/1.11.2.3 数据对比

    HLS播放延迟测试:阿里云48s ,openresy 31s Cache-Control: max-age=300 NGINX下配置CACHE-CONTROL   Content-Length:637 ...

  8. JMS学习(六)--提高非持久订阅者的可靠性 以及 订阅恢复策略

    一,非持久订阅者 和 实时消费消息 在这篇文章中区分了Domain为Pub/Sub.Destination为Topic时,消费者有两种:持久订阅者 和 非持久订阅者. 对于持久订阅者而言,只要订阅了某 ...

  9. 第9月第9天 CTFramesetterCreateWithAttributedString

    1. NSString *text = @"This\nis\nsome\nmulti-line\nsample\ntext."; UIFont *uiFont = [UIFont ...

  10. Sortable.js参数

    所有的事件回调函数都有两个参数:event和ui,浏览器自有event对象,和经过封装的ui对象ui.helper - 表示sortable元素的JQuery对象,通常是当前元素的克隆对象ui.pos ...