結果

問題 No.2712 Play more!
ユーザー ynishi2015ynishi2015
提出日時 2024-03-31 15:22:42
言語 PHP
(8.3.4)
結果
WA  
実行時間 -
コード長 5,761 bytes
コンパイル時間 2,372 ms
コンパイル使用メモリ 32,272 KB
実行使用メモリ 34,492 KB
最終ジャッジ日時 2024-09-30 20:37:25
合計ジャッジ時間 14,056 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
31,072 KB
testcase_01 AC 36 ms
30,932 KB
testcase_02 AC 36 ms
30,856 KB
testcase_03 AC 37 ms
30,888 KB
testcase_04 WA -
testcase_05 AC 35 ms
31,032 KB
testcase_06 AC 35 ms
30,920 KB
testcase_07 AC 869 ms
31,884 KB
testcase_08 AC 886 ms
32,012 KB
testcase_09 AC 871 ms
31,888 KB
testcase_10 WA -
testcase_11 AC 463 ms
31,756 KB
testcase_12 AC 995 ms
31,760 KB
testcase_13 AC 397 ms
31,968 KB
testcase_14 AC 622 ms
31,376 KB
testcase_15 AC 1,862 ms
34,492 KB
testcase_16 AC 253 ms
32,012 KB
testcase_17 AC 89 ms
31,572 KB
testcase_18 AC 193 ms
31,244 KB
testcase_19 AC 134 ms
31,500 KB
testcase_20 AC 567 ms
32,012 KB
testcase_21 AC 357 ms
32,908 KB
testcase_22 AC 1,077 ms
31,888 KB
testcase_23 AC 142 ms
34,360 KB
testcase_24 AC 307 ms
31,500 KB
testcase_25 AC 85 ms
32,012 KB
testcase_26 AC 265 ms
31,244 KB
testcase_27 AC 286 ms
31,376 KB
testcase_28 WA -
testcase_29 AC 288 ms
31,760 KB
testcase_30 AC 1,024 ms
31,888 KB
testcase_31 AC 43 ms
32,780 KB
testcase_32 AC 40 ms
32,268 KB
testcase_33 AC 35 ms
30,848 KB
testcase_34 AC 35 ms
30,896 KB
testcase_35 AC 34 ms
30,932 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
[$N, $M] = ints();
$A = ints();
$bf = new bellman_ford($N);
for($i = 0; $i < $M; $i++){
    [$f, $t, $cost] = ints();
    $f--; $t--;
    $cost -= $A[$t];
    $bf->add_edge($f, $t, $cost);
}
$bf->solve(0);
if($bf->find_negative_loop()){
    echo "inf";exit;
}
echo -$bf->d[$N-1]+$A[0];

class bellman_ford {
    public $V;
    public $inf;
    public $d = array();
    public $es = array();

    public function __construct($node_size) {
        $this->V = $node_size;
        $this->inf = PHP_INT_MAX / 2;
        $this->d = array_fill(0, $this->V, $this->inf);
    }

    public function add_edge($from, $to, $cost) {
        $this->es[] = array('from' => $from, 'to' => $to, 'cost' => $cost);
    }

    public function solve($s) {
        $cnt = 0;
        $this->d[$s] = 0;
        while ($cnt < $this->V) {
            $update = false;
            foreach ($this->es as $e) {
                if ($this->d[$e['from']] != $this->inf && $this->d[$e['to']] > $this->d[$e['from']] + $e['cost']) {
                    $this->d[$e['to']] = $this->d[$e['from']] + $e['cost'];
                    $update = true;
                }
            }
            if (!$update) {
                break;
            }
            $cnt++;
        }
        return ($cnt == $this->V);
    }

    public function find_negative_loop() {
        $this->d = array_fill(0, $this->V, 0);
        $cnt = 0;
        while ($cnt < $this->V) {
            $update = false;
            foreach ($this->es as $e) {
                if ($this->d[$e['to']] > $this->d[$e['from']] + $e['cost']) {
                    $this->d[$e['to']] = $this->d[$e['from']] + $e['cost'];
                    $update = true;
                }
            }
            if (!$update) {
                break;
            }
            $cnt++;
        }
        return ($cnt == $this->V);
    }

    public function shortest_path_infinite($s, $t) {
        $this->d[$s] = 0;
        for ($i = 0; $i < 2 * $this->V - 1; $i++) {
            foreach ($this->es as $e) {
                if ($this->d[$e['from']] != $this->inf && $this->d[$e['to']] > $this->d[$e['from']] + $e['cost']) {
                    $this->d[$e['to']] = $this->d[$e['from']] + $e['cost'];
                    if ($i >= $this->V - 1) {
                        if ($e['to'] == $t) {
                            return true;
                        }
                        $this->d[$e['to']] = -$this->inf;
                    }
                }
            }
        }
        return false;
    }
}

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