Go: // n×nの成長度表に現れる数字を重複を許してm-1個選んで掛け合わせる時の最大値を求める
// ->掛け算型のDP

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		growth_table := make([][]float64, n)
		for i := range growth_table {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			tmp := make([]float64, n)
			for j := range tmp {
				tmp[j], _ = strconv.ParseFloat(buf_[j], 64)
			}
			growth_table[i] = tmp
		}
		dp := make([][]float64, m)
		for i := range dp {
			dp[i] = make([]float64, n)
		}
		for i := range dp {
			dp[0][i] = 1
		}
		for k := 1; k < m; k++ {
			// k個使うとき...
			for i := 0; i < n; i++ {
				for j := 0; j < n; j++ {
					// 新たにgrowth_table[i][jを使うかどうか
					dp[k][j] = max(dp[k][j], dp[k-1][i]*growth_table[i][j])
					// check(dp)
				}
			}
		}
		ans := 0.0
		for i := 0; i < n; i++ {
			if dp[m-1][i] > ans {
				ans = dp[m-1][i]
			}
		}
		fmt.Printf("%.2f\n", ans)
	}
}

func max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

// func check(dp [][]float64) {
// 	log.Println()
// 	for i := range dp {
// 		log.Println(dp[i])
// 	}
// 	log.Println()
// }

C++: #include<bits/stdc++.h>
using namespace std;

double c[100][100];
double dp[100][100];//i??\??????j???????????¬
signed main() {
	int a, b;
	while(scanf("%d%d", &a, &b),a|b) {
		for (int d = 0; d < a; d++) {
			for (int e = 0; e < a; e++) {
				scanf("%lf", &c[d][e]);
			}
		}
		for (int d = 0; d < b; d++) {
			for (int e = 0; e < a; e++) {
				if (d == 0)dp[d][e] = 1;
				else dp[d][e] = -(1 << 15);
			}
		}
		for (int d = 0; d < a; d++)dp[0][d] = 1;
		for (int d = 1; d < b; d++) {
			for (int e = 0; e < a; e++) {//????????\
				for (int f = 0; f < a; f++) {//????????\
					dp[d][e] = max(dp[d][e], dp[d - 1][f] * c[f][e]);
				}
			}
		}
		double MAX = -(1 << 15);
		for (int d = 0; d < a; d++) {
			MAX = max(MAX, dp[b - 1][d]);
		}
		printf("%.2lf\n", MAX);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n, m;
  double g[100][100];
  double dp[101][100]; //dp[i][j]:i番目までで前に肥料jを与えたときの最大の苗木長

  while(scanf("%d %d", &n, &m), n){
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        scanf("%lf", &g[i][j]);
      }
    }

    fill_n( *dp, 101 * 100, -1);

    for(int i = 0; i < n; i++){
      dp[0][i] = 1.0;
    }

    for(int i = 0; i < m; i++){
      for(int j = 0; j < n; j++){
        if(dp[i][j] == -1) continue;
        for(int k = 0; k < n; k++){
          dp[i + 1][k] = max( dp[i + 1][k], dp[i][j] * g[j][k]);
        }
      }
    }

    int ret = 0;
    for(int i = 1; i < n; i++){
      if(dp[m][i] > ret) ret = i;
    }
    cout << fixed << setprecision(2) << dp[m][ret] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cmath>
#define EPS 1e-10
using namespace std;
typedef pair<double,int> P;
int m,n;
double total;
double dp[110][110];
int Just;
vector<double> matrix[110];
double rec(int next,int counter){
  if(dp[next][counter] > 0)return dp[next][counter];
  if(counter  == 0){
    return 1.0;
  }
  double HANMA = 0.0,test=0.0;
 
 
  for(int i=0;i<n;i++){   
    
    //HANMA = max(HANMA,rec(i,counter-1)*matrix[next][i]);
    test = rec(i,counter-1)*matrix[next][i];
    if(test > HANMA)HANMA = test;
  }  
 
  dp[Just][counter] = HANMA;
  return HANMA;
}


int main(){
  
  double In;
 
  while(cin >> n >> m && n && m){
    for(int i=0;i<=n;i++)matrix[i].clear();
    
    for(int i=m;i>=0;i--)for(int j=0;j<n;j++)dp[j][i] = -10;
    for(int i=0;i<n;i++)dp[i][0] = dp[i][1] = 1;
    double MaX[n+1];
    int Pos[n+1];
    for(int i=0;i<n;i++)MaX[i] = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cin >> In;
	matrix[i].push_back(In);
      }
    }
 

    double ans = -1;
 
  
      for(int I=0;I<n;I++){
	Just = I;
	double tes = rec(I,m);
	ans = max(ans,dp[Just][m]);
      }
    

      //for(int i=1;i<=m;i++)cout << dp[0][i] << " ";
      //cout << endl;

    printf("%.2f\n",ans);

  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
double key[100][100];
double table[100][100];

void Init(int n, int m){
	for(int i = 0; i < n; i++) for(int j = 0; j < m+1; j++) table[i][j] = 0;
}

double memo(int cur, int count, int limit, int kind){
	if(table[cur][count]) return table[cur][count];
	if(count==limit) return 1.0;
	double Max = 0;
	int prev = 0;
	for(int i = 0; i < kind; i++){
		int tmp = memo(i, count+1, limit, kind);
		if(Max <= tmp){
			if(Max*key[prev][cur] < tmp*key[i][cur]){
				Max = tmp;
				prev = i;
			}
		}
	}
	table[cur][count] = Max*key[prev][cur];
	return table[cur][count];
}

int main(){
	int n, m;
	while(cin >> n >> m && ((n!=0)||(m!=0))){
		Init(n, m);
		for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) cin >> key[i][j];
		for(int i = 0; i < n; i++) table[i][0] = memo(i, 1, m, n);
		double max = 0;
		for(int i = 0; i < n; i++) if(max < table[i][0]) max = table[i][0];
		printf("%0.2f\n", max);
	}

	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<vector>

using namespace std;

const double eps = 1.0e-10;
double eq(double a, double b){ //a==b
  return abs(b-a)<eps;
}
double gt(double a, double b){ //a>b
  return !eq(a,b) && a > b;
}

int main(){
  while(true){
    int n,m;
    double answer = -1;
    cin >> n >> m;
    if( n == 0 && m == 0 ) break;
    double table[n][n];
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
	cin >> table[i][j];
      }
    }
    double dp[n][m];
    for(int i = 0; i < n; ++i){
      dp[i][0] = 1.0;
    }
    for(int i = 1; i < m; ++i){
      double maximum = -1;
      for(int j = 0; j < n; ++j){
	for(int k = 0; k < n; ++k){
	  double t = table[k][j] * dp[k][i-1];
	  if( gt(t,maximum) ){
	    maximum=t;
	  }
	}
	dp[j][i] = maximum;
      }
    }
    
    for(int i = 0; i < n; ++i){
      if( gt( dp[i][m-1], answer ) )
	answer = dp[i][m-1];
    }
    printf("%.2lf\n",answer);

  }
  return 0;
}