結果

問題 No.2565 はじめてのおつかい
ユーザー Yoichiro NishimuraYoichiro Nishimura
提出日時 2023-12-02 15:59:49
言語 PHP
(8.3.4)
結果
AC  
実行時間 499 ms / 2,000 ms
コード長 4,776 bytes
コンパイル時間 4,884 ms
コンパイル使用メモリ 30,732 KB
実行使用メモリ 157,876 KB
最終ジャッジ日時 2024-11-13 22:42:31
合計ジャッジ時間 15,327 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
31,080 KB
testcase_01 AC 42 ms
31,104 KB
testcase_02 AC 42 ms
30,864 KB
testcase_03 AC 499 ms
157,876 KB
testcase_04 AC 388 ms
157,872 KB
testcase_05 AC 42 ms
31,072 KB
testcase_06 AC 224 ms
56,460 KB
testcase_07 AC 132 ms
43,916 KB
testcase_08 AC 138 ms
51,620 KB
testcase_09 AC 199 ms
51,468 KB
testcase_10 AC 159 ms
54,628 KB
testcase_11 AC 326 ms
93,256 KB
testcase_12 AC 92 ms
40,332 KB
testcase_13 AC 153 ms
51,228 KB
testcase_14 AC 244 ms
73,484 KB
testcase_15 AC 250 ms
68,364 KB
testcase_16 AC 225 ms
98,520 KB
testcase_17 AC 75 ms
42,640 KB
testcase_18 AC 88 ms
52,816 KB
testcase_19 AC 281 ms
85,944 KB
testcase_20 AC 250 ms
80,644 KB
testcase_21 AC 224 ms
80,652 KB
testcase_22 AC 144 ms
63,244 KB
testcase_23 AC 175 ms
64,268 KB
testcase_24 AC 60 ms
37,516 KB
testcase_25 AC 267 ms
81,936 KB
testcase_26 AC 168 ms
61,200 KB
testcase_27 AC 234 ms
85,132 KB
testcase_28 AC 276 ms
83,432 KB
testcase_29 AC 313 ms
99,340 KB
testcase_30 AC 243 ms
90,252 KB
testcase_31 AC 258 ms
84,108 KB
testcase_32 AC 161 ms
61,624 KB
testcase_33 AC 253 ms
87,692 KB
testcase_34 AC 277 ms
76,832 KB
testcase_35 AC 244 ms
96,572 KB
testcase_36 AC 274 ms
86,952 KB
testcase_37 AC 169 ms
63,832 KB
testcase_38 AC 260 ms
78,220 KB
testcase_39 AC 233 ms
68,604 KB
testcase_40 AC 284 ms
98,752 KB
testcase_41 AC 307 ms
102,156 KB
testcase_42 AC 174 ms
59,892 KB
testcase_43 AC 235 ms
73,488 KB
testcase_44 AC 144 ms
51,980 KB
testcase_45 AC 90 ms
45,068 KB
testcase_46 AC 271 ms
70,264 KB
testcase_47 AC 174 ms
48,632 KB
testcase_48 AC 156 ms
51,796 KB
testcase_49 AC 92 ms
39,568 KB
testcase_50 AC 201 ms
53,388 KB
testcase_51 AC 43 ms
31,156 KB
testcase_52 AC 135 ms
38,540 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
[$N, $M] = ints();
$di1 = new Di($N);
$di2 = new Di($N);
$di3 = new Di($N);
for($i = 0; $i < $M; $i++){
    [$a, $b] = ints();
    $di1->connect($a, $b, 1);
    $di2->connect($a, $b, 1);
    $di3->connect($a, $b, 1);
}
$di1->solve(1);
$di2->solve($N-1);
$di3->solve($N);
$ans =  
    min(
        $di1->distance[$N]+$di3->distance[$N-1]+$di2->distance[1], 
        $di1->distance[$N-1]+$di2->distance[$N]+$di3->distance[1],
);
if($ans < 10**10){
    echo $ans;
}else{
    echo "-1";
}

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(1, $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;
      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