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. linux的常用命令介绍

    1.ls  列出当前目录下的所有的文件和文件夹的名称. 参数如下:-a 显示隐藏文件 -l 显示方式为列表 -h 以可读性高的方式输出 eg: ls -lh /logs/tran  目录如果不指定(相 ...

  2. 我的第一个python web开发框架(40)——后台日志与异常处理

    后台权限和底层框架的改造终于完成了,小白也终于可以放下紧悬着的心,可以轻松一下了.这不他为了感谢老菜,又找老菜聊了起来. 小白:多谢老大的帮忙,系统终于改造完成了,可以好好放松一下了. 老菜:呵呵,对 ...

  3. 单向链表的Java实现

    package testOffer.linkedList; import org.w3c.dom.Node; public class SingleLinkedList { //测试用例 public ...

  4. slice 与 splice 的区别

    slice: 定义一个数组:let b = ['a','b','c','d','e'] b:["a", "b", "c", "d& ...

  5. Object 与 T的差别 导致swagger 的model 显示的数据为空

    情景复现: 在整合swagger的时候,自己对原本定于的Object的data做了修改,把Object修改为了T,data的set方法的返回类型由于编译器没有报错,就没有去做修改, 这个时候就导致了, ...

  6. mondb 常用命令学习记录

    mondb 常用命令学习记录 一.MongoDB 下载安装 MongoDB官网 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制 ...

  7. Map the Debris 轨道周期

    返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: avgAlt}. 至于轨道周期怎么求, ...

  8. 【算法】深度优先 马走日 Hamilton routes

    在n*m的棋盘中,马只能走“日” 字.马从位置(x,y)处出发,把棋盘的每一格都走一次,且只走一次.找出所有路径. ××××××××××××× 类似问题: 在半个中国象棋棋盘上,马在左下角(1,1)处 ...

  9. Python_range

    range 当作定义的数字范围列表. 满足顾头不顾腚,可以加步长,与for循环结合使用. 一般使用 for i in range(0, 101): print(i) 结果: #[0,1,2,3,... ...

  10. How do I copy files that need root access with scp

    server - How do I copy files that need root access with scp? - Ask Ubuntuhttps://askubuntu.com/quest ...