hdu2389 Rain on your Parade 二分图匹配--HK算法

You’re giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is here. It’s a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. The evening is progressing just as you had imagined. It could be the perfect end of a beautiful day.

But nothing ever is perfect. One of your guests works in weather forecasting. He suddenly yells, “I know that breeze! It means its going to rain heavily in just a few minutes!” Your guests all wear their best dresses and really would not like to get wet, hence they stand terrified when hearing the bad news.
You have prepared a few umbrellas which can protect a few of your guests. The umbrellas are small, and since your guests are all slightly snobbish, no guest will share an umbrella with other guests. The umbrellas are spread across your (gigantic) garden, just like your guests. To complicate matters even more, some of your guests can’t run as fast as the others.
Can you help your guests so that as many as possible find an umbrella before it starts to pour?

Given the positions and speeds of all your guests, the positions of the umbrellas, and the time until it starts to rain, find out how many of your guests can at most reach an umbrella. Two guests do not want to share an umbrella, however.

虽然也是二分图匹配,但是需要用HK算法,匈牙利算法会超时

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXN=;//最大点数
const int INF=0x3f3f3f3f;//距离初始值
int Map[MAXN][MAXN];//二分图
int cx[MAXN];//cx[i]表示左集合i顶点所匹配的右集合的顶点序号
int cy[MAXN];//cy[i]表示右集合i顶点所匹配的左集合的顶点序号
int n,m,dis;
int dx[MAXN],dy[MAXN];
bool vis[MAXN];
int xp[],yp[],s[];
int xu[],yu[]; bool searchpath(){
queue<int>Q;
dis=INF;
memset(dx,-,sizeof(dx));
memset(dy,-,sizeof(dy));
for(int i=;i<=n;++i){
//cx[i]表示左集合i顶点所匹配的右集合的顶点序号
if(cx[i]==-){
//将未遍历的节点入队并初始化次节点距离为0
Q.push(i);
dx[i]=;
}
}
//广度搜索增广路径
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(dx[u]>dis)break;
//取右侧节点
for(int i=;i<=m;++i){
//右侧节点的增广路径的距离
if(Map[u][i]&&dy[i]==-){
dy[i]=dx[u]+;//v对应的距离为u对应距离加1
if(cy[i]==-)dis=dy[i];
else{
dx[cy[i]]=dy[i]+;
Q.push(cy[i]);
}
}
}
}
return dis!=INF;
} //寻找路径深度搜索
int findpath(int s){
for(int i=;i<=m;++i){
//如果该点没有被遍历过并且距离为上一节点+1
if(!vis[i]&&Map[s][i]&&dy[i]==dx[s]+){
//对该点染色
vis[i]=;
if(cy[i]!=-&&dy[i]==dis)continue;
if(cy[i]==-||findpath(cy[i])){
cy[i]=s;cx[s]=i;
return ;
}
}
}
return ;
} //得到最大匹配的数目
int MaxMatch(){
int res=;
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
while(searchpath()){
memset(vis,,sizeof(vis));
for(int i=;i<=n;++i){
if(cx[i]==-)res+=findpath(i);
}
}
return res;
} int main(){
int T;
scanf("%d",&T);
for(int q=;q<=T;++q){
memset(Map,,sizeof(Map));
int t;
scanf("%d",&t);
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf("%d%d%d",&xp[i],&yp[i],&s[i]);
}
scanf("%d",&m);
for(int i=;i<=m;++i){
scanf("%d%d",&xu[i],&yu[i]);
}
for(int i=;i<=n;++i){
for(int j=;j<=m;++j){
if((xp[i]-xu[j])*(ll)(xp[i]-xu[j])+(yp[i]-yu[j])*(ll)(yp[i]-yu[j])<=t*(ll)t*s[i]*s[i]){
Map[i][j]=;
}
}
}
printf("Scenario #%d:\n%d\n\n",q,MaxMatch());
}
return ;
}
上一篇:SSRF漏洞学习


下一篇:机器学习:分类算法性能指标之ROC曲线