結果

問題 No.1 道のショートカット
ユーザー chrom_shchrom_sh
提出日時 2015-01-13 23:27:27
言語 Perl
(5.38.2)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,916 bytes
コンパイル時間 60 ms
コンパイル使用メモリ 6,040 KB
実行使用メモリ 45,136 KB
最終ジャッジ日時 2023-09-22 11:52:11
合計ジャッジ時間 9,375 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
6,152 KB
testcase_01 AC 13 ms
6,220 KB
testcase_02 AC 14 ms
6,212 KB
testcase_03 AC 16 ms
6,156 KB
testcase_04 AC 14 ms
6,116 KB
testcase_05 AC 16 ms
6,124 KB
testcase_06 AC 15 ms
6,036 KB
testcase_07 AC 14 ms
6,248 KB
testcase_08 AC 54 ms
7,008 KB
testcase_09 AC 19 ms
6,664 KB
testcase_10 AC 62 ms
7,000 KB
testcase_11 AC 1,200 ms
45,136 KB
testcase_12 AC 328 ms
7,956 KB
testcase_13 AC 225 ms
8,216 KB
testcase_14 AC 15 ms
6,208 KB
testcase_15 AC 14 ms
6,164 KB
testcase_16 AC 15 ms
6,252 KB
testcase_17 AC 14 ms
6,180 KB
testcase_18 AC 15 ms
6,292 KB
testcase_19 AC 15 ms
6,092 KB
testcase_20 AC 36 ms
7,416 KB
testcase_21 AC 15 ms
6,340 KB
testcase_22 AC 15 ms
6,188 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
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 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.pl syntax OK

ソースコード

diff #

# http://yukicoder.me/problems/17                                                                       
use strict;
use warnings;
use 5.010;
use Data::Dumper;

my $goal    = _read_line();
my $money   = _read_line();
my $roads   = _read_line();

my $map = read_map();

# 前処理終了。
# 解く
my $ans = solve($map, $money, $goal);
say $ans;


sub _read_line {
    my $line    = <STDIN>;
    chomp($line);
    return $line;
}

sub read_map {
    my $s   = [split(/ /, _read_line())];
    my $t   = [split(/ /, _read_line())];
    my $y   = [split(/ /, _read_line())];
    my $m   = [split(/ /, _read_line())];

    my $ret = {};
    ## これ同じ街間に別経路が存在する場合があるみたい。
    ## なので、コストと距離は配列で持つ。
    for (my $i = 0 ; $i < @$s ; $i++) {
        my $start   = $s->[$i];
        my $dest    = $t->[$i];
        $ret->{$start}  ||= {};
        $ret->{$start}->{$dest} ||= [];
        ## 入れたいものよりコストが安く、かつ短い物があるならば無視する
        my $skip    = 0;
        for my $e (@{$ret->{ $start }->{ $dest }}) {
            if ($e->{ cost } < $y->[$i] and $e->{ dist } < $m->[$i]) {
                $skip   = 1;
                last;
            }
        }
        next if $skip;
        push @{ $ret->{$start}->{$dest} }, {
            cost    => $y->[$i],
            dist    => $m->[$i],
        };
    }
    return $ret;
}

sub solve {
    my ($map, $money, $goal)    = @_;
    #die Dumper($map);
    my $cur = [
        {
            pos     => 1,
            cost    => 0,
            dist    => 0,
        }
    ];

    my $res = [];
    while (my $state = pop(@$cur)) {
        while (my ($k, $v_array) = each(%{$map->{ $state->{ pos } }})) {
            for my $v (@$v_array) {
                my $cost    = $v->{ cost } + $state->{ cost };
                my $dist    = $v->{ dist } + $state->{ dist };
                next if ($cost > $money);
                if ($k == $goal) {
                    push @$res, {
                        cost    => $cost,
                        dist    => $dist,
                    };
                    next;
                }
                ## 同じ位置で、コストも安く、距離も短いものがあるならば、入れない
                if (has_reasonable($cur, $k, $cost, $dist)) {
                    next;
                }

                push @$cur, {
                    pos     => $k,
                    cost    => $cost,
                    dist    => $dist,
                };
            }
        }
    }
    $res    = [sort {$a->{dist} <=> $b->{dist}} @$res];
    return -1 if @$res == 0;
    return $res->[0]->{dist};
}

sub has_reasonable {
    my ($cur, $k, $cost, $dist) = @_;

    for my $e (@$cur) {
        next if $e->{ pos } != $k;
        return 1 if ($e->{ cost } < $cost and $e->{ dist } < $dist);
    }
}
0