#include #include #include #include using namespace std; const int MAXV = 205; const int64_t INFI = 201ull*(1ull<<30); int64_t cost[MAXV][MAXV]; bool usedx[MAXV], usedy[MAXV]; int linkx[MAXV], linky[MAXV], m_prev[MAXV], que[MAXV], L, R; int64_t lx[MAXV], ly[MAXV], slack[MAXV]; void lable(int y) { while (y != -1) { int pv = linkx[m_prev[y]]; linky[y] = m_prev[y]; linkx[m_prev[y]] = y; y = pv; } } bool bfs(int n) { while (L < R) { int x = que[L++], y; for (y = 0; y < n; y++) if (!usedy[y]) { if (lx[x] + ly[y] != cost[x][y]) { int64_t ex = lx[x] + ly[y] - cost[x][y]; if (slack[y] > ex) { slack[y] = ex; m_prev[y] = x; } } else { m_prev[y] = x; if (linky[y] == -1) { lable(y); return true; } usedy[y] = true; usedx[linky[y]] = true; que[R++] = linky[y]; } } } return false; } int64_t max_match(int n) { int i, x; memset(ly, 0, sizeof (ly)); for (x = 0; x < n; x++) lx[x] = *max_element(cost[x], cost[x] + n); memset(linkx, -1, sizeof (linkx[0]) * n); memset(linky, -1, sizeof (linky[0]) * n); for (x = 0; x < n; x++) { memset(usedx, false, sizeof (usedx[0]) * n); memset(usedy, false, sizeof (usedy[0]) * n); fill(slack, slack + n, INFI); L = R = 0; usedx[x] = true; que[R++] = x; while (linkx[x] == -1 && !bfs(n)) { int64_t dec = INFI; for (i = 0; i < n; i++) if (!usedy[i]) dec = min(dec, slack[i]); for (i = 0; i < n; i++) { if (usedx[i]) lx[i] -= dec; if (usedy[i]) ly[i] += dec; slack[i] -= dec; } for (i = 0; i < n; i++) if (!usedy[i] && slack[i] == 0) { if (linky[i] == -1) { lable(i); break; } usedy[i] = true; usedx[linky[i]] = true; que[R++] = linky[i]; } } } int64_t ans = 0; for (i = 0; i < n; i++) ans += cost[i][linkx[i]]; return ans; } int main() { int cas,n,t,i,j,tmp; scanf("%d",&cas); for(t=1;t<=cas;t++) { scanf("%d",&n); for(i=0;i