HDU 1435 Stable Match (稳定婚姻匹配)

题意:建立一个匹配表,使得一个发射点对应一个接收点,对于某一个发射点来说,它的接收点离它越近那么就会更稳定,同样对于接收点也是一样的情况。
匹配的目标是使得整个网络变得稳定。
对于某2个匹配,比如,( a ---- 1) ,(b----2) ,如果发射点a 离接收点2 比 1要近,而且2 也离 发射点a要比 b 近,那么 a 就很有可能把信号发到 2中,我们就说这个搭配是不稳定的。
而且每个点都有一个容量值,如果对于一个发射点到2个接收点的距离一样的话,它将首先选择容量大的那个。

题解:稳定婚姻匹配
让发射点一个个去匹配接收点即可。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int maxn = 205;
int t, n;
struct node {
	int id, w;
	double x, y, z;
}s[maxn], r[maxn];  //send  receive

double dis(node e1, node e2) { return sqrt((e1.x - e2.x) * (e1.x - e2.x) + (e1.y - e2.y) * (e1.y - e2.y) + (e1.z - e2.z) * (e1.z - e2.z)); }
struct fav {
    int id, w;
    double val;
    //fav(int a, int w, double b) :id(a), w(w), val(b) {}
}lover[maxn];
bool cmp(fav x, fav y) { return x.val == y.val ? x.w > y.w : x.val < y.val; }
int boy_love_who[maxn][maxn], girl_love_fir[maxn][maxn];
int boy[maxn], girl[maxn], kth[maxn];
inline void G_S() {
    for (int i = 1; i <= n; i++) boy[i] = girl[i] = kth[i] = 0;
    bool flag, no = 0;
    while (true) {
        flag = false;
        for (int i = 1; i <= n; i++) {
            if (!boy[i]) {
                int g = boy_love_who[i][++kth[i]];
                if (kth[i] > n) {
                    no = 1;
                    break;
                }
                if (!girl[g]) {
                    girl[g] = i;
                    boy[i] = g;
                    continue;
                }
                else if (girl_love_fir[g][i] < girl_love_fir[g][girl[g]]) {
                	boy[girl[g]] = 0;
                    girl[g] = i;
                    boy[i] = g;
                }
                flag = true;
            }
        }
        if (no) break;
        if (!flag) break;
    }
    if (no) puts("Impossible");
    else for (int i = 1; i <= n; i++) printf("%d %d\n", girl[i], i);
}
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) scanf("%d%d%lf%lf%lf", &s[i].id, &s[i].w, &s[i].x, &s[i].y, &s[i].z);
		for (int i = 1; i <= n; i++) scanf("%d%d%lf%lf%lf", &r[i].id, &r[i].w, &r[i].x, &r[i].y, &r[i].z);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                lover[j] = { r[j].id, r[j].w, dis(s[i], r[j]) };
            }
            sort(lover + 1, lover + n + 1, cmp);
            for (int j = 1; j <= n; j++) {
                boy_love_who[i][j] = lover[j].id;
            }
            for (int j = 1; j <= n; j++) {
                lover[j] = { s[j].id, s[j].w, dis(s[j], r[i]) };
            }
            sort(lover + 1, lover + n + 1, cmp);
            for (int j = 1; j <= n; j++) {
                girl_love_fir[i][lover[j].id] = j;
            }
        }
        G_S();
		puts("");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值