結果

問題 No.2855 Move on Grid
ユーザー ynishi2015ynishi2015
提出日時 2024-08-25 14:37:01
言語 PHP
(8.3.4)
結果
TLE  
実行時間 -
コード長 5,145 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 32,144 KB
実行使用メモリ 84,028 KB
最終ジャッジ日時 2024-08-25 14:38:11
合計ジャッジ時間 12,591 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 866 ms
51,548 KB
testcase_01 AC 1,252 ms
48,700 KB
testcase_02 AC 835 ms
44,600 KB
testcase_03 AC 486 ms
40,376 KB
testcase_04 AC 370 ms
38,716 KB
testcase_05 AC 759 ms
44,984 KB
testcase_06 AC 872 ms
44,728 KB
testcase_07 AC 104 ms
32,404 KB
testcase_08 AC 618 ms
42,812 KB
testcase_09 AC 332 ms
38,328 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
[$H, $W, $K] = ints();
for($y = 0; $y < $H; $y++){
    $map[] = ints();
}
$ng = 2**30;
$ok = 0;

while($ng-$ok > 1){
    $mid = ($ng+$ok)/2;
    $di = new Di($H*$W);
    for($y = 0; $y < $H; $y++){
       // echo "-";
        for($x = 0; $x < $W; $x++){
      //      echo $map[$y][$x] < $mid;
            if($x > 0)$di->connect($y*$W+$x+1-1, $y*$W+$x+1, $map[$y][$x] < $mid);
            if($y > 0)$di->connect(($y-1)*$W+$x+1, $y*$W+$x+1, $map[$y][$x] < $mid);
            if($x < $W-1)$di->connect($y*$W+$x+1+1, $y*$W+$x+1, $map[$y][$x] < $mid);
            if($y < $H-1)$di->connect(($y+1)*$W+$x+1, $y*$W+$x+1, $map[$y][$x] < $mid);
        }
    //    echo "\n";
    }
    $di->solve(1);
    if($di->distance[$H*$W] + ($map[0][0] < $mid) <= $K){
        $ok = $mid;
    }else{
        $ng = $mid;
    }
}
echo min($ok, 1000000000);


class Di{
  public $pq;
  public $distance;
  public $G;
  public $from;
  public $inf = 10**18;
  function __construct($n) {
    $this->distance = array_fill(1, $n, $this->inf);
//    $this->from = array_fill(1,$n,0);
    $this->G = array_fill(1,$n,[]);
  }
  function connect($from, $to, $cost){
    if(isset($this->G[$from][$to])){
      $this->G[$from][$to] = min($this->G[$from][$to], $cost);
    }else{
      $this->G[$from][$to] = $cost;
    }
  }
  function solve($from){
    $pq = new SplPriorityQueue();
    $distance = $this->distance;
    $tfrom = $this->from;
    $G = $this->G;
    $pq->insert($from, 0);
    $distance[$from] = 0;
    while($pq->count()){
      $f = $pq->extract();
      if(!isset($map[$f])){
        $map[$f] = true;
        foreach($G[$f] as $t => $dist){
          $new = $distance[$f] + $dist;
          if($distance[$t] > $new){
            $distance[$t] = $new;
            //$tfrom[$t] = $f;
            $pq->insert($t, -$new);
          }
        }
      }
    }
    $this->distance = $distance;
    //$this->from = $tfrom;
  }
}
function str(){return trim(fgets(STDIN));}
function ints($n = false){
  if($n===false){
    $str = trim(fgets(STDIN));if($str == '')return [];
    return array_map('intval',explode(' ',$str));
  }else{$ret = [];for($i = 0; $i < $n; $i++)foreach(array_map('intval',explode(' ',trim(fgets(STDIN)))) as $j => $v)$ret[$j][] = $v;return $ret;}
}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function isqrt($n):int{$res=intval(sqrt($n))+1;while($res*$res>$n)$res--;return $res;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function swap(&$a,&$b){$tmp=$a;$a=$b;$b=$tmp;}
function o(...$val){
  if(count($val)==1)$val=array_shift($val);
  echo debug_backtrace()[0]['line'].")";
  if(is_array($val)){
    if(count($val) == 0)echo "empty array\n";
    elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
    else{
      echo "array:array\n";
      if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
      else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
    }
  }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function atsu1($array){$a=array_flip($array);$fuku=array_flip($a);sort($fuku);array_unshift($fuku,0);unset($fuku[0]);$atsu=array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function median($a){sort($a);$n=count($a);return $n%2?$a[($n-1)/2]:($a[$n/2-1]+$a[$n/2])/2;}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}
function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$M = $GLOBALS['M'];
  $G = array_fill(0, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      if($both == 0){
        $G[$a][] = $b;
      }elseif($both == 1){
        $G[$a][] = $b;
        $G[$b][] = $a;
      }else{
        $G[$b][] = $a;
      }
    }else{
      list($a, $b, $d) = $values;
      $a--; $b--;
      if($both == 0){
        $G[$a][] = [$b, $d];
      }elseif($both == 1){
        $G[$a][] = [$b, $d];
        $G[$b][] = [$a, $d];
      }else{
        $G[$b][] = [$a, $d];
      }
    }
  }
  return $G;
}
0