改进的匈牙利188ms水过。。。。
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN -99999999
#define MAX 99999999
#define pii pair<int ,int>
#define bug cout<<"here!!"<<endl
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define eps 1e-8
#define N 233
char str[101];
int match[505][1005];//左边是组号,右边是人头号
int cnt[505];
int vis[505];
int g[505][1005];
int n,m;
int mid;
bool sear(int s){
int i,j;
for(i=0;i<m;i++){
if(g[i][s] && !vis[i]){
vis[i] = 1;
if(cnt[i] < mid){
match[i][cnt[i]++] = s;
return true;
} else {
for(j=0;j<cnt[i];j++){
if(sear(match[i][j])){
match[i][j] = s;
return true;
}
}
}
}
}
return false;
}
bool chk(int mid){
int i,j;
memset(cnt,0,sizeof(cnt));
for(i=0;i<n;i++){
memset(vis,0,sizeof(vis));
if(!sear(i))return false;
}
return true;
}
void gao(){
int i,j;
int l = 0,r = 1000;
//二分答案
while(l<r){
mid = (l+r)>>1;
if(chk(mid)){
r = mid;
} else l = mid+1;
}
printf("%d\n",l);
}
int main(){
while(scanf("%d%d",&n,&m) && (n+m)){//人数、组数
int i,j;
getchar();
memset(g,0,sizeof(g));
for(i=0;i<n;i++){
gets(str);
int len =strlen(str);
for(j=0;j<len;){
while(str[j]<'0' || str[j]>'9'){
j++;
}
int tmp = 0;
while(str[j]>='0' && str[j]<='9') {
tmp = tmp*10+str[j]-'0';
j++;
}
g[tmp][i] = 1;//每一组tmp对多个人i
}
}
gao();
}
return 0;
}

本文详细介绍了改进的匈牙利算法在解决最优匹配问题上的应用,包括算法原理、优化策略及实际案例分析。
807

被折叠的 条评论
为什么被折叠?



