标签:
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1198 Accepted Submission(s): 535
#include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<string> #define INF 100000000 using namespace std; int n,mp[105][105],minn,maxx,link[105],mark[105],l,r,mid,p; bool dfs(int x) { for(int i=1;i<=n;i++) { if(mark[i]==-1&&mp[x][i]>=p&&mp[x][i]<=p+mid) { mark[i]=1; if(link[i]==-1||dfs(link[i])) { link[i]=x; return true; } } } return false; } bool check(int x) { memset(link,-1,sizeof(link)); for(int i=1;i<=n;i++) { memset(mark,-1,sizeof(mark)); if(!dfs(i)) return false; } return true; } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d",&n); maxx=0,minn=INF; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&mp[i][j]); minn=min(minn,mp[i][j]); maxx=max(maxx,mp[i][j]); } } l,r,mid; int ans=0; l=0,r=maxx-minn; while(l<=r) { mid=(l+r)>>1; bool flag=false; for(p=minn;p+mid<=maxx;p++) { if(check(mid)) { flag=true; break; } } if(flag) { ans=mid; r=mid-1; } else l=mid+1; } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/water-full/p/4459983.html