Pagini recente » Cod sursa (job #2931620) | Cod sursa (job #3170269) | Cod sursa (job #2834956) | Cod sursa (job #1015961) | Cod sursa (job #2939894)
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("cast.in" , "r" , stdin) , freopen("cast.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < ll , ll >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'
namespace FastRead
{
char __buff[5000];ll __lg = 0 , __p = 0;
char nc()
{
if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
return __buff[__p++];
}
template<class T>void read(T&__x)
{
T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
__x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
}
}
using namespace FastRead;
using namespace std;
const ll N = 12;
const ll M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
int c[N][N] , dp[1 << N][N];
void Test()
{
int i , j;
cin >> n;
for(i = 0 ; i < n ; i++)
for(j = 0 ; j < n ; j++)
cin >> c[i][j];
for(int msk = 0 ; msk < (1 << n) ; msk++)
for(int i = 0 ; i < n ; i++)
dp[msk][i] = INT_MAX;
for(int i = 0 ; i < n ; i++)
dp[(1 << i)][i] = 0;
for(int msk = 1 ; msk < (1 << n) ; msk++)
{
if(msk & (msk - 1));
else continue;
for(int submsk = (msk - 1) & msk ; submsk ; submsk = (submsk - 1) & msk)
{
for(int root1 = 0 ; root1 < n ; root1++)
for(int root2 = 0 ; root2 < n ; root2++)
{
if(root1 != root2 && (msk >> root1 & 1) && (submsk >> root2 & 1) && (submsk & (1 << root1)) == 0)
{
dp[msk][root1] = min(dp[msk][root1] , max(dp[submsk][root2] + c[root1][root2] , c[root1][root2] + dp[msk ^ submsk][root1]));
}
}
}
}
cout << dp[(1 << n) - 1][0] << '\n';
}
signed main()
{
#ifndef ONLINE_JUDGE
FastIO , FILES;
#endif
ll q; cin >> q;
while(q--) Test();
return 0;
}