http://codeforces.com/contest/677/problem/D

建颗新树,节点元素包含r、c、dis,第i层包含拥有编号为i的钥匙的所有节点。用i-1层更新i层,逐层更新到底层。

不使用就会超时的优化:用i-1层更新时不是所有节点都有必要用到,我们对i-1层排序,取前600节点更新下层。

public class Main {
private static final int c = 330,INF=Integer.MAX_VALUE/2,maxn=c*c*c+100,maxe=maxn*6;
static class Node implements Comparable<Node>{
int x,y, dis; public Node(int x, int y, int dis) {
this.x = x;
this.y = y;
this.dis = dis;
} @Override
public int compareTo(Node o) {
return dis -o.dis;
}
}
public static void main(String[] args) {
IO io=new IO();
int n=io.nextInt(),m=io.nextInt(),p=io.nextInt(); ArrayList<Node>[]al=new ArrayList[p+1];
for (int i = 0; i < al.length; i++) al[i]=new ArrayList<>(c);
int[]dis=new int[c*c];
Arrays.fill(dis,INF); for (int i=1;i<=n;i++)for (int j=1;j<=m;j++)al[io.nextInt()].add(new Node(i,j,INF));
al[0].add(new Node(1,1,0)); int ans=INF;
for (int i=1;i<=p;i++){
Collections.sort(al[i-1]);
for (int j=0;j<Math.min(al[i-1].size(),330);j++)for (Node k:al[i]){
k.dis=Math.min(k.dis,al[i-1].get(j).dis+Math.abs(k.x-al[i-1].get(j).x)+Math.abs(k.y-al[i-1].get(j).y));
if (i==p)ans=Math.min(k.dis,ans);
}
}
io.println(ans);
}
}

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

  1. E. Vanya and Balloons Codeforces Round #355 (Div. 2)

    http://codeforces.com/contest/677/problem/E 题意:有n*n矩形,每个格子有一个值(0.1.2.3),你可以在矩形里画一个十字(‘+’形或‘x’形),十字的四 ...

  2. 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 ...

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

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

  4. Codeforces Round #355 (Div. 2) D. Vanya and Treasure

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

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. kali中的postgres怎么连接

    metasploit,默认使用的是postgresql数据库.在BT5或是Kali等专业的渗透系统中,postgresql已经被默认安装. 执行msfdb init会自动创建一个默认的用户名密码都是m ...

  2. vs code使用Git

    做一夜的搬运工:https://www.cnblogs.com/richard1015/p/8336429.html

  3. Copula函数

    Copula函数 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. Copula介绍 Copula函数把边缘分布函数与联合分布函数联系起来,是研究变 ...

  4. leetcode题解-122买卖股票的最佳时期

    题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...

  5. 北京大学冯哲清北学堂讲课day1

    贪心方案: 答案是第三个策略 二分的一个重点是有顺序性,只有满足这个件才可以二分判断区间,否则你得自己构造顺序. 洛谷跳石头同题: 首先,我们要最小化最大跳远距离 代码如下(此题) #include& ...

  6. VS2019 实用设置

    本文记录了 VS2019 预览版使用过程中的一些设置,这些设置也同样适用于 VS2017,我们可以根据个人的实际情况进行修改. 滚动条(Scroll Bar) 将滚动条设置为 map mode 后,则 ...

  7. 菜鸟学IT之第一次作业

    作业的要求来自于:https://www.cnblogs.com/greyzeng/p/9581624.html 反思· 为何要来上课并且认真参与? 在大学中的师生关系? 自我简述题目 心得· 学习态 ...

  8. 团队开发项目--NABCD模型

    N(need)需求: 鉴于在学校中的大部分爱学习的学生平时都去拍空教室的占有情况,我们发现有的时候太多,导致同学们们拍照会浪费很长的时间,而且空教室的显示不是一下子全出来,有的时候还会出现无法显示的情 ...

  9. git submoudle提交

    进入到各个submoudle文件夹 git status 查看所在branch和文件修改状态 git add [files]; git commit "" git pull ori ...

  10. PHP九大接口视频教程( 支付宝,QQ,短信接口,微信接口开发, 支付宝即时到账接口开发三级分销全套)

    PHP九大接口视频教程(  支付宝,QQ,短信接口,微信接口开发, 支付宝即时到账接口开发三级分销全套) 需要的联系我:QQ: 1844912514 PHP九大接口视频教程(  支付宝,QQ,短信接口 ...