use strict; use warnings; my @tmp = split(' ', ); my %ini = (x => $tmp[0], y => $tmp[1]); my $n = ; my $wgt = 0; my @des; for(1..$n){ @tmp = split(' ', ); push @des, {x => $tmp[0], y => $tmp[1], w => $tmp[2]}; $wgt += $tmp[2]; } my $diff = 0; my $t = 0; my @dp; my $inf = 999999999; for my $i (0..(1<<$n)-1){ for my $j(0..$n-1){ $dp[$i][$j] = [$inf, 0]; } } for my $i (0..$n-1){ $diff = abs($des[$i]->{x} - $ini{x}) + abs($des[$i]->{y} - $ini{y}); $t = $diff * ($wgt + 100)/120 + $des[$i]->{w}; $dp[1<<$i][$i] = [$t, $wgt - $des[$i]->{w}]; } my $ni; my $val = 0; for my $i (1..(1<<$n)-1){ for my $j (0..$n-1){ next if($dp[$i][$j]->[0] == $inf); $wgt = $dp[$i][$j]->[1]; for my $k (0..$n-1){ next if(($i>>$k) % 2); $ni = $i | (1<<$k); $diff = abs($des[$k]->{x} - $des[$j]->{x}) + abs($des[$k]->{y} - $des[$j]->{y}); $t = $diff * ($wgt + 100)/120 + $des[$k]->{w}; if($dp[$i][$j]->[0] + $t < $dp[$ni][$k]->[0]){ $dp[$ni][$k] = [$dp[$i][$j]->[0] + $t, $wgt - $des[$k]->{w}]; } } } } my $ans = $inf; my $ai = 0; for(0..$n-1){ if($dp[(1<<$n)-1][$_][0] < $ans){ $ans = $dp[(1<<$n)-1][$_][0]; $ai = $_; } } $ans += (abs($des[$ai]->{x} - $ini{x}) + abs($des[$ai]->{y} - $ini{y}))*5/6; print $ans;