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. Hibernate执行SQL语句实现查询修改功能!

    今天玩Hibernate时突然就想写写SQL语句查询... DAO : //查询 public List<?> createSqlQueryList(final String queryS ...

  2. scp远程拷贝文件及文件夹

    scp : 远程copy 命令 -r : 递归copy 从Linux Copy 到 Linux 从Linux Copy 到 Windows (当前目录使用. 就可以了) scp -r root@10. ...

  3. Jenkins 配置 Git 错误解决:CAfile: C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt

    错误信息: Failed to connect to repository : Command "C:/tools/Git/bin/git.exe ls-remote -h https:/X ...

  4. Pycharm安装并配置jupyter notebook

    Pycharm安装并配置jupyter notebook Pycharm安装并配置jupyter notebook 一: 安装命令jupyter: pip install jupyter 如果缺少依赖 ...

  5. wordpress如何利用插件添加优酷土豆等视频到自己的博客上

    wordpress有时候需要添加优酷.土豆等网站的视频到自己的博客上,传统的分享方法不能符合电脑端和手机端屏幕大小的需求,又比较繁琐,怎样利用插件的方法进行添加呢,本视频向你介绍一款这样的插件——Sm ...

  6. 自反ACL(第三组)

    一.实验拓扑 二.配置过程 此处我用了学号后两位来划分网段,注意:先把网络做通再配ACL 1)网络连通测试 内网可以telnet外网 ----------- 外网可以telnet内网 2)ACL配置( ...

  7. 系统运行缓慢,CPU 100%,以及Full GC次数过多问题的排查思路

    前言 处理过线上问题的同学基本上都会遇到系统突然运行缓慢,CPU 100%,以及Full GC次数过多的问题.当然,这些问题的最终导致的直观现象就是系统运行缓慢,并且有大量的报警. 本文主要针对系统运 ...

  8. node.js、js读取excel、操作excel、创建excel之js-xlsx.js

    node.js篇 第一步引入包 npm install xlsx -save 第二步使用 var xl =require('xlsx'); //workbook 对象,指的是整份 Excel 文档.我 ...

  9. Linux系统下Mysql安装与配置

    一,使用系统 Centos7: 在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 二,下载安装 ...

  10. python中使用redis发布订阅者模型

    redis发布订阅者模型: Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者,订阅者和Channel.发布者和订阅者都是Redis客户端,Channel则 ...