
while (<DATA>) {
  chomp;
  push @G, [split];
}

my $V = shift || 0;
my @T = prim(\@G, $V);
print "@T\n";

sub prim {
  my $G = shift;
  my $N = @$G;
  my $u;

  # step 1
  my $t = shift || 0;
  my @T;
  my %U = ( $t => 1 );
  my @E = ($t) x $N;

  while (1) {

    # step 2
    { my $minv = 1000000;
      for (0 .. $N-1) {
        next if $U{$_};
        my $l = $G->[$t][$_];
        next if $l >= $minv;
        $minv = $l;
        $u = $_;
      }
    }

    push @T, "$E[$u] - $u";     # step 3
    $U{$u} = 1;                 # step 4

    return @T if keys(%U) == $N; # step 5

    # step 6
    for (0 .. $N-1) {
      next if $U{$_};
      if ($G->[$u][$_] <  $G->[$t][$_]) {
        $G->[$t][$_] = $G->[$u][$_];
        $E[$_] = $u;
      }
    }
  }                             # step 7
}

__DATA__
0 1 4 2
1 0 8 5
4 8 0 7
2 5 7 0
