Pagini recente » Cod sursa (job #1566048) | Cod sursa (job #1473373) | Cod sursa (job #1856168) | Cod sursa (job #952947) | Cod sursa (job #605008)
Cod sursa(job #605008)
#include <iostream>
#define NMax 13
#define Inf 2000000000
using namespace std;
int T, N, Cost[NMax][NMax], Best[(1<<NMax)][NMax];
bool Computed[NMax][(1<<NMax)][NMax];
inline int Min (int a, int b)
{
if (a<b)
{
return a;
}
return b;
}
inline int Max (int a, int b)
{
if (a>b)
{
return a;
}
return b;
}
int Memo (int Conf, int X)
{
if (Computed[T][Conf][X])
{
return Best[Conf][X];
}
int Computers[NMax];
Computers[0]=0;
for (int i=0; i<N; ++i)
{
if (Conf&(1<<i))
{
Computers[++Computers[0]]=i;
}
}
if (Computers[0]==1)
{
Best[Conf][X]=0;
Computed[T][Conf][X]=true;
return 0;
}
Best[Conf][X]=Inf;
int NConf=(1<<Computers[0]);
for (int C=1; C<NConf; ++C)
{
int Conf1=0, Conf2=0;
for (int i=0; i<Computers[0]; ++i)
{
if (C&(1<<i))
{
Conf1+=(1<<Computers[i+1]);
}
}
if (Conf1&(1<<X))
{
Conf1-=(1<<X);
}
Conf2=Conf-Conf1;
for (int Comp=0; Comp<N; ++Comp)
{
if ((Conf1&(1<<Comp)) and (Comp!=X))
{
Best[Conf][X]=Min (Best[Conf][X], Max (Memo (Conf1, Comp), Memo (Conf2, X))+Cost[X][Comp]);
}
}
}
Computed[T][Conf][X]=true;
return Best[Conf][X];
}
int main()
{
freopen ("cast.in", "r", stdin);
freopen ("cast.out", "w", stdout);
scanf ("%d", &T);
for (; T>0; --T)
{
scanf ("%d", &N);
for (int i=0; i<N; ++i)
{
for (int j=0; j<N; ++j)
{
scanf ("%d", &Cost[i][j]);
}
}
int n=(1<<N);
//printf ("%d\n", Memo (n-1, 0));
for (int i=1; i<n; ++i)
{
for (int j=0; j<N; ++j)
{
if (i&(1<<j))
{
Memo (i, j);
}
}
}
printf ("%d\n", Best[n-1][0]);
}
return 0;
}