-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler107.pl
More file actions
executable file
·56 lines (49 loc) · 923 Bytes
/
euler107.pl
File metadata and controls
executable file
·56 lines (49 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/perl -w
use strict;
my @network;
my @vertices;
my %tree;
sub sum_network
{
my ($m, $n);
my $l = 0;
foreach (1..39) {
$m = $_;
foreach ($m+1..40) {
$n = $network[$m][$_];
$l += ($n eq '-')?0:$n;
}
}
$l;
}
sub find_next_v
{
my ($i, $m);
my $min = 100000000;
foreach (@vertices) {
$m = $_;
foreach (1..40) {
next if (exists($tree{$_}) || $network[$m][$_] eq '-');
if ($network[$m][$_]<$min) {
$min = $network[$m][$_];
$i = $_;
}
}
}
($i, $min);
}
push @network, [0];
while (<>) {
chomp;
push @network, [0, split /,/];
}
my $length = sum_network();
push @vertices, 1;
$tree{1} = 1;
while (@vertices<40) {
my ($a, $b) = find_next_v();
push @vertices, $a;
$tree{$a} = 1;
$length -= $b;
}
print "$length\n";