hdoj1435 Stable Match(稳定婚姻问题)

时间:2024-06-19 18:34:32

简单稳定婚姻问题。

题目描述不够全面,当距离相同时容量大的优先选择。

稳定婚姻问题不存在无解情况。

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define maxn 205
using namespace std;
struct point{
int num,v;
double x,y,z;
}Man_array[maxn],Woman_array[maxn];
struct point2{
int num,v;
double dis;
}A[maxn*maxn];
int n;
int nowb[maxn],nowg[maxn],V[maxn][maxn],B_g[maxn][maxn],G_b[maxn][maxn];
double calc(point p,point q){
double xx=p.x-q.x,yy=p.y-q.y,zz=p.z-q.z;
return sqrt(xx*xx+yy*yy+zz*zz);
}
int comp(point2 p,point2 q){
return p.dis<q.dis || p.dis==q.dis && p.v>q.v;
}
void Stable_Marriage(){
memset(V,,sizeof(V));memset(nowb,-,sizeof(nowb));memset(nowg,-,sizeof(nowg));
queue<int> Q;
for (int i=;i<n;i++) Q.push(i);
while (!Q.empty()){
int pre=Q.front(),res;
Q.pop();
for (int i=;i<n;i++){
int res=B_g[pre][i];
if (V[pre][res]) continue;
V[pre][res]=;
if (nowg[res]==-){
nowg[res]=pre;
nowb[pre]=res;
break;
}
else if (G_b[res][nowg[res]]<G_b[res][pre]){
Q.push(nowg[res]);
nowg[res]=pre;
nowb[pre]=res;
break;
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie();cout.tie();
int t;
cin >> t;
while (t--){
cin >> n;
for (int i=;i<n;i++){
cin >> Man_array[i].num >> Man_array[i].v >> Man_array[i].x >> Man_array[i].y >> Man_array[i].z;
}
for (int i=;i<n;i++){
cin >> Woman_array[i].num >> Woman_array[i].v >> Woman_array[i].x >> Woman_array[i].y >> Woman_array[i].z;
}
//男——女
for (int i=;i<n;i++){
for (int j=;j<n;j++){
A[j].dis=calc(Man_array[i],Woman_array[j]);
A[j].v=Woman_array[j].v;
A[j].num=j;
}
sort(A,A+n,comp);
for (int j=;j<n;j++) B_g[i][j]=A[j].num;
}
//女——男
for (int i=;i<n;i++){
for (int j=;j<n;j++){
A[j].dis=calc(Woman_array[i],Man_array[j]);
A[j].v=Man_array[j].v;
A[j].num=j;
}
sort(A,A+n,comp);
for (int j=;j<n;j++) G_b[i][A[j].num]=n-j+;
}
Stable_Marriage();
for (int i=;i<n;i++){
cout << Man_array[i].num << " " << Woman_array[nowb[i]].num << endl;
}
cout << endl;
}
return ;
}