标签:
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1303 Accepted Submission(s): 583
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<cmath> 6 using namespace std; 7 8 #define INF 99999999 9 int g[1001][1001]; 10 int mk[1001]; 11 int cy[1001]; 12 int t; 13 int n; 14 int inmax; 15 int inmin; 16 int mmmin; 17 int mmmax; 18 19 void init() 20 { 21 memset(g,0,sizeof(g)); 22 memset(mk,0,sizeof(mk)); 23 memset(cy,0xff,sizeof(cy)); 24 } 25 26 int path(int u) 27 { 28 int v; 29 for(v=1;v<=n;v++) 30 { 31 if(g[u][v]>=mmmin&&g[u][v]<=mmmax&&!mk[v]) 32 { 33 mk[v]=1; 34 if(cy[v]==-1||path(cy[v])) 35 { 36 cy[v]=u; 37 return true; 38 } 39 } 40 } 41 return false; 42 } 43 44 int maxmatch() 45 { 46 int i; 47 memset(cy,0xff,sizeof(cy)); 48 for(i=1;i<=n;i++) 49 { 50 memset(mk,0,sizeof(mk)); 51 if(!path(i)) 52 { 53 return false; 54 } 55 } 56 return true; 57 } 58 59 int main() 60 { 61 scanf("%d",&t); 62 while(t--) 63 { 64 init(); 65 scanf("%d",&n); 66 int i,j; 67 inmax=-INF,inmin=INF; 68 for(i=1;i<=n;i++) 69 { 70 for(j=1;j<=n;j++) 71 { 72 scanf("%d",&g[i][j]); 73 if(g[i][j]>inmax)inmax=g[i][j]; 74 if(g[i][j]<inmin)inmin=g[i][j]; 75 } 76 } 77 //二分差值 78 int rmax=inmax-inmin; 79 int rmin=0; 80 while(rmin<=rmax) 81 { 82 int mid=(rmin+rmax)>>1; 83 for(i=0;i<=inmax;i++) 84 { 85 mmmin=i; 86 mmmax=i+mid; 87 if(maxmatch()) 88 break; 89 } 90 if(maxmatch()) //表示这个区间段在mmmin在mmmax之间 91 rmax=mid-1; //区间缩小 92 else 93 rmin=mid+1; //不满足扩大区间 94 } 95 printf("%d\n",rmax+1); 96 } 97 return 0; 98 }
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4732407.html