# hdoj 2236 无题II

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 110;

int line[maxn][maxn],used[maxn],nxt[maxn];
int mat[maxn][maxn],num[maxn],cnt;
int T,n;

bool Find(int x){
for(int i = 1;i <= n;i++){
if(line[x][i]&&!used[i]){
used[i] = 1;
if(nxt[i] == 0||Find(nxt[i])){
nxt[i] = x;
return true;
}
}
}
return false;
}

int match(){
int sum = 0;
for(int i = 1;i <= n;i++){
memset(used,0,sizeof(used));
if(Find(i)) sum++;
else break;
}
return sum;
}

void handle(int x){
for(int i = 0;i < cnt;i++){
if(num[i] == x) return ;
}
num[cnt++] = x;
}

bool judge(int mid){
for(int i = 0;i < cnt;i++){
memset(line,0,sizeof(line));
for(int j = 1;j <= n;j++){
for(int k = 1;k <= n;k++){
if(mat[j][k] >= num[i]&&mat[j][k] <= num[i]+mid)
line[j][k] = 1;
}
}
memset(nxt,0,sizeof(nxt));
int ans = match();
if(ans == n) return true;
}
return false;
}

int main(){
scanf("%d",&T);
while(T--){
cnt = 0;
scanf("%d",&n);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
scanf("%d",&mat[i][j]);
handle(mat[i][j]);
}
}
int l = 0,r = 210,ans;
sort(num,num+cnt);
while(l <= r){
int mid = (l+r)>>1;
if(judge(mid)) ans = mid,r = mid-1;
else l = mid+1;
}
printf("%d\n",ans);
}
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120