Submission #67107060


Source Code Expand

<?php

fscanf(STDIN, "%d %d", $N, $M);

// グラフ構築
$graph = array_fill(0, $N + 1, []);
for ($i = 0; $i < $M; $i++) {
    fscanf(STDIN, "%d %d %d %d", $a, $b, $c, $d);
    $graph[$a][] = [$b, $c, $d]; // [to, cost, tree]
    $graph[$b][] = [$a, $c, $d];
}

// 最短距離・木の数
$dist = array_fill(0, $N + 1, INF);
$trees = array_fill(0, $N + 1, -INF);
$dist[1] = 0;
$trees[1] = 0;

// 優先度付きキュー(SplPriorityQueue 使用)
class PQ extends SplPriorityQueue {
    public function compare($a, $b): int {
        // 辞書順:(距離が小) → (木が多)
        if ($a[0] === $b[0]) {
            return $b[1] <=> $a[1]; // 木の数多い方が優先
        }
        return $b[0] <=> $a[0]; // 距離小さい方が優先
    }
}

$pq = new PQ();
$pq->insert([0, 0, 1], [0, 0]); // [距離, -木の数, ノード], 優先度

while (!$pq->isEmpty()) {
    [$cost, $negTree, $u] = $pq->extract();
    $tree = -$negTree;

    if ($cost > $dist[$u]) continue;
    if ($cost === $dist[$u] && $tree < $trees[$u]) continue;

    foreach ($graph[$u] as [$v, $c, $t]) {
        $newCost = $cost + $c;
        $newTree = $tree + $t;

        if (
            $newCost < $dist[$v] ||
            ($newCost === $dist[$v] && $newTree > $trees[$v])
        ) {
            $dist[$v] = $newCost;
            $trees[$v] = $newTree;
            $pq->insert([$newCost, -$newTree, $v], [$newCost, -$newTree]);
        }
    }
}

echo $dist[$N] . " " . $trees[$N] . "\n";

Submission Info

Submission Time
Task A73 - Marathon Route
User myoshizumi
Language PHP (php 8.2.8)
Score 1000
Code Size 1543 Byte
Status AC
Exec Time 186 ms
Memory 81236 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 1
AC × 21
Set Name Test Cases
Sample 00_sample_00
All 00_sample_00, 10_random_00, 10_random_01, 10_random_02, 10_random_03, 10_random_04, 10_random_05, 10_random_06, 10_random_07, 10_random_08, 10_random_09, 10_random_10, 10_random_11, 10_random_12, 10_random_13, 10_random_14, 20_max_00, 20_max_01, 20_max_02, 20_max_03, 20_max_04
Case Name Status Exec Time Memory
00_sample_00 AC 13 ms 20800 KiB
10_random_00 AC 37 ms 32572 KiB
10_random_01 AC 77 ms 48600 KiB
10_random_02 AC 69 ms 41412 KiB
10_random_03 AC 101 ms 50880 KiB
10_random_04 AC 182 ms 78436 KiB
10_random_05 AC 143 ms 70752 KiB
10_random_06 AC 131 ms 66752 KiB
10_random_07 AC 123 ms 70836 KiB
10_random_08 AC 50 ms 39936 KiB
10_random_09 AC 126 ms 59724 KiB
10_random_10 AC 85 ms 45080 KiB
10_random_11 AC 45 ms 32576 KiB
10_random_12 AC 122 ms 62992 KiB
10_random_13 AC 24 ms 26460 KiB
10_random_14 AC 105 ms 52472 KiB
20_max_00 AC 182 ms 81108 KiB
20_max_01 AC 179 ms 80952 KiB
20_max_02 AC 184 ms 80944 KiB
20_max_03 AC 186 ms 81236 KiB
20_max_04 AC 182 ms 81204 KiB