http://poj.org/problem?id=2771
题意:老师带学生出去旅游,老师想要尽可能的避免学生谈恋爱,又想尽可能的带学生出去,
四种情况满足任意种都不会谈恋爱
1.身高差超过40厘米
2.性别一样
3.音乐风格不一样
4,爱好的运动一样
题解:我们这样想,先把他们能谈恋爱的全都匹配起来,然后再把他们狠心拆开也就是只带他们中的一个去,所有答案就等于总人数-最大的匹配数,这也是为什么二分图的最大独立集等于二分图的最大匹配数。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
struct node{
int h;
string style;
string hobby;
}shuaige[505],meinv[505];
int vis[505];
int match[505];
int n;
int lenm,lenf;
int ispipei(int u,int v)
{
if(abs(shuaige[u].h-meinv[v].h)<=40&&shuaige[u].style.compare(meinv[v].style)==0&&shuaige[u].hobby.compare(meinv[v].hobby)!=0)
return 1;
return 0;
}
int find(int u)
{
for(int i=1;i<=lenf;i++)
{
if(ispipei(u,i)&&vis[i]==0)
{
vis[i]=1;
if(match[i]==-1||find(match[i]))
{
match[i]=u;
return 1;
}
}
}
return 0;
}
int pipei()
{
for(int i=1;i<=lenf;i++)
match[i]=-1;
int ans=0;
for(int i=1;i<=lenm;i++)
{
memset(vis,0,sizeof(vis));
int k=find(i);
ans+=k;
}
return ans;
}
int main()
{
int t;
int h;
char sex;
char hobby[105],style[105];
cin>>t;
while(t--)
{
lenm=0;
lenf=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d %c %s %s",&h,&sex,&style,&hobby);
if(sex=='M')
{
shuaige[++lenm].h=h;
shuaige[lenm].style=style;
shuaige[lenm].hobby=hobby;
}
else
{
meinv[++lenf].h=h;
meinv[lenf].style=style;
meinv[lenf].hobby=hobby;
}
}
//cout<<lenm<<" "<<lenf<<endl;
printf("%d\n",n-pipei());
}
}

本文介绍了一个基于二分图最大匹配的旅行配对算法,旨在帮助老师在带学生旅行时,通过考虑身高、性别、音乐风格和爱好等条件,避免学生间可能发生的恋爱,同时最大化旅行的参与人数。
3924

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



