Untitled II Time limit:2000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1515 accepted submission (s): 688
Problem Description This is a simple game, in a n*n matrix, find the number of n so that the number of n in a different row and column and require that n number of the maximum and minimum difference is the minimum value.
Input enter an integer t to represent the T-group data.
Enter a positive integer n (1<=n<=100) for the first row of each set of data to represent the size of the matrix.
Then enter n rows, n number x (0<=x<=100) per row.
Output represents the minimum value for each set of data outputs.
Sample Input
1 4 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4-4
Sample Output
3
Author XHD
Source hdoj 2008 Summer Exercise (2)-Hold by Captain Xu: Chinese title does not explain
Idea: Come up and write a DFS, uh ... TLE
Then think of the binary difference, the lower bounds of the enumeration to see whether each row can be matched to (that is, the binary graph maximum matching, behavior x set, column y set). This is the condition of the binary transfer.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Cmath> using namespace std;
#define N #define INF 99999999 int N,ans;
int m[n][n],ma[n][n],vis[n];
int num[n],cnt,line[n]; int can (int t) {for (int i=1; i<=n; i++) {if (!vis[i]&&ma[t][i)) {Vis[i]=1
; if (line[i]==-1| |
Can (Line[i])) {line[i]=t;
return 1;
}} return 0;
} void get (int t) {for (int i=0;i<cnt;i++) if (num[i]==t) return;
num[cnt++]=t;
int main () {int T;
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", &m[
I][J]);
Get (M[i][j]);
int l=0,r=210;
Sort (num,num+cnt);
int last;while (l<=r) {int mid= (L+R) >>1,flag=0;
for (int i=0;i<cnt;i++) {memset (ma,0,sizeof (MA)); for (int k=1;k<=n;k++) for (int j=1;j<=n;j++) if (M[K][J]>=NUM[I]&&A
Mp;m[k][j]<=num[i]+mid) Ma[k][j]=1;
memset (line) (line,-1,sizeof);
int ans=0;
for (int j=1;j<=n;j++) {memset (vis,0,sizeof (VIS));
if (Can (j)) ans++;
else break;
} if (ans==n) {flag=1;
Break
} if (flag) last=mid,r=mid-1;
else l=mid+1;
printf ("%d\n", last);
return 0;
}