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 <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int main(){
  int n,m; 
  double data[101][101],tree[101][101];
  while ( cin >> n >> m ,n+m ) {
    for ( int i = 1; i <= n; i++ ) {
      for ( int j = 1; j <= n; j++ ) cin >> tree[i][j];
    }

    for ( int i = 1; i <= n; i++ ) {
      for ( int j = 1; j <= m; j++ ) data[i][j] = -1.0;
    }


    for ( int i = 1; i <= n; i++ ) data[i][1] = 1.0;
    
    int tmp;
    int cnt = 0;
    for( int i = 1; i <= n; i++ ) {
      cnt++;
      tmp = cnt;
      int tmp2;
      for( int j = 1; j < m; j++ ) {
	for( int k = 1; k <= n; k++ ) {
	  if ( data[i][j+1] < data[i][j] * tree[tmp][k] ) {
	    data[i][j+1] = data[i][j] * tree[tmp][k];
	    tmp2 = k;
	  }
	}
	tmp = tmp2;
      }
    }

    //for ( int i = 1; i <=n; i++ ) {
    //  for( int j = 1; j <= n; j++ ) printf("%.2f ",data[i][j]);
    // cout << endl;
    // }
    double maxv = -1.0;
    for( int i = 1; i <= n; i++ ) maxv = max( data[i][n], maxv );

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

  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int n, m;
double map[100][100], Max;

void search( int prev, double point, int count )
{
	if(count == m) {
		if(Max < point) {
			Max = point;
		}
		return;
	}
	for(int i=0; i < n; ++i)
		serach(i, point*map[prev][i], count + 1);
}

int main( void )
{
	while(cin >> n >> m, n | m) {
		Max = 0;
		for(int i=0; i < n; ++i)
			for(int j=0; j < n; ++j) {
				cin >> map[i][j];
			}
		for(int i=0; i < n; ++i)
			search(i, 1.0, 1);
		printf("%.2f\n", Max);
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cassert>
#include<cmath>
#define EPS 1e-10
using namespace std;

int m,n;
double total;
double dp[110][110];
int Just;
vector<double> matrix[110];
void rec(int next,int counter,double sam){
 if(sam >= dp[Just][m])return;
  if(counter >= m){
    dp[Just][m] = max(dp[Just][m],sam);
    return;
  }
  for(int i=0;i<n;i++){
   
    rec(i,counter+1,sam*matrix[next][i]);
  }  
}


int main(){
  
  double In;
 
  while(cin >> n >> m && n && m){
    for(int i=0;i<=n;i++)matrix[i].clear();
  
    for(int i=109;i>=0;i--)for(int j=0;j<110;j++)dp[j][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;
	rec(I,1,1.0);
	ans = max(ans,dp[Just][m]);
      }
    
    printf("%.2f\n",ans);

  }
  return 0;
}

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

using namespace std;

#define EPS pow(10, -5)

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector< vector<long double> > hyou(n, vector<long double>(n));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> hyou[i][j];
			}
		}
		vector< vector<long double> > nae(m, vector<long double>(n, 1.0));
		for (int i = 1; i < m; i++) {
			for (int j = 0; j < n; j++) {
				nae[i][j] = nae[i-1][0]*hyou[0][j];
				for (int k = 1; k < n; k++) {
					nae[i][j] = max(nae[i][j], nae[i-1][k]*hyou[k][j]);
				}
			}
		}
		long double MAX = 0.0;
		for (int i = 0; i < n; i++) {
			MAX = max(MAX, nae[m-1][i]);
		}
		MAX += EPS;
		MAX *= 100.0;
		if (MAX-(long double)((int)MAX) >= 0.5) {
			MAX += 1.0;
		} 
		MAX /= 100.0;
		printf("%.2Lf\n", MAX);
	}
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int n, m;
double g[100][100];
double dp[101][100];

double rec(int a, int b){ //a回目，前に与えた肥料b
	if(dp[a][b]>0) return dp[a][b];
	
	double ret=1.0;

	if(a==m-1) ret=1.0;
	else{
		double t=-1;
		for(int i=0; i<n; ++i){
			double s=g[b][i]*rec(a+1, i);	
			//printf("s=%lf\n", s);
			if(s>t) t=s;
		}
		ret*=t;
		//printf("t=%lf\n", t);
	}
	
	return dp[a][b]=ret;
}

int main(){
	while(1){
		scanf(" %d %d", &n, &m);
		if(n==0) break;
		for(int i=0; i<n; ++i)
		for(int j=0; j<n; ++j)
		scanf(" %lf", &g[i][j]);
		
		for(int i=0; i<n; ++i)
		for(int j=0; j<n; ++j)
		dp[i][j]=-1.0;		
		
		double ans=0;
		for(int i=0; i<n; ++i){
			double tmp=rec(0, i);
			if(tmp>ans) ans=tmp;	
		}
		
		
		for(int i=0; i<n; ++i){
			for(int j=0; j<n; ++j){
				printf("%.4lf ", dp[i][j]);		
			}
			printf("\n");
		}
		
		
		ans*=100;
		ans=round(ans);
		ans/=100;
		printf("%.2lf\n", ans);
	}
}

C++: #include <iostream>
#include <iomanip>
#include <cmath>
#include <cfloat>
using namespace std;

const int NMAX = 100;

int main() {
  int n, m;
  double dp[2][NMAX];
  double g[NMAX][NMAX];
  double lg[NMAX][NMAX];

  while (1) {
    cin >> n >> m;
    if (!n && !m) {
      break;
    }

    for (int i=0; i<n; ++i) {
      for (int j=0; j<n; ++j) {
        cin >> g[i][j];
        lg[i][j] = log(g[i][j]);
      }
    }
    for (int i=0; i<n; ++i) {
      dp[0][i] = 0.0;
    }

    for (int i=0; i<m-1; ++i) {
      for (int j=0; j<n; ++j) {
        dp[!(i&1)][j] = dp[i&1][0] + lg[0][j];
        for (int k=0; k<n; ++k) {
          dp[!(i&1)][j] = max(dp[!(i&1)][j], dp[i&1][k] + lg[k][j]);
        }
      }
    }

    double lans = -DBL_MAX;
    for (int i=0; i<n; ++i) {
      lans = max(lans, dp[!(m&1)][i]);
    }
    cout << fixed << setprecision(2) << round(exp(lans) * 100) / 100. << endl;
  }
  return 0;
}