DFS与最大匹配
其实就是个DFS。
题目:http://acm.swust.edu.cn/oj/problem/0827/
View Code
#include "iostream"
#include "string"
#include "cstring"
#include "algorithm"
using namespace std;
#define maxn 105
#define INF 0xFFFFFFF
int map[maxn][maxn], mid, k, N, M;
int Link[maxn]; //记录行的状态
int used[maxn]; //记录列的状态
bool dfs(int u)//行
{
for(int v=1; v<=M; v++) //列++
{
if(map[u][v]>=k && map[u][v]<=k+mid && !used[v])
{
used[v] = 1;
if(Link[v]==-1 || dfs(Link[v]))
{
Link[v] = u;
return true;
}
}
}
return false;
}
bool OK()
{
memset(Link, -1, sizeof(Link));
for(int i=1; i<=N; i++) //行++
{
memset(used, 0, sizeof(used));
if(!dfs(i)) return false;
}
return true;
}
int main()
{
while(cin>>N>>M)
{
int Max = 0;
int Min = INF;
for(int i=1; i<=N; i++)
for(int j=1; j<=M; j++)
{
cin>>map[i][j];
if(map[i][j]>Max) Max = map[i][j];
if(map[i][j]<Min) Min = map[i][j];
}
int left=0, right=Max-Min, ans;
while(left <= right)
{
mid = (left+right)/2;
int flag = 0;
for(k=Min; k+mid<=Max; k++)
{
if(OK())
{
ans = mid;
flag = 1;
break;
}
}
if(flag) right = mid-1;
else left = mid+1;
}
cout<<ans<<endl;
}
}
posted on 2012-03-22 19:28 More study needed. 阅读(208) 评论(0) 编辑 收藏 举报