connect($f, $t, $c); $di->connect($t, $f, $c); } $di->solve(1); $max = 0; for($i = 1; $i <= $P; $i++){ [$pos, $price] = ints(); $money = max($Y - $di->distance[$pos],0); $cnt = intdiv($money, $price); chmax($max, $cnt); } echo $max; 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 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; }