HDU 1435 Stable Match 稳定婚姻

思路:就是一个简单的稳定婚姻问题的模型,把距离近的,容量大的排在见面。然后就是发射方一次次就追求,接收方选择接受还是不接受。

http://acm.hdu.edu.cn/showproblem.php?pid=1435

/*********************************************
    Problem : HDU 1435
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++) //遍历
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) //反向遍历
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++) //遍历一个STL容器
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

struct Point{
    double x,y,z;
    double dist(Point & B) {
        return (x-B.x) * (x-B.x) + (y-B.y) * (y-B.y) + (z-B.z) * (z-B.z);
    }
};

struct Dist {
    double d;
    int v;
    int id;
}dist[205];

Point A[205];
Point B[205];

int Va[205];
int Vb[205];
int RankA[205][205];

void input() {
    scanf("%d",&n);
    int id;
    rep(i,1,n) {
        scanf("%d",&id);
        scanf("%d %lf %lf %lf",&Va[id],&A[id].x,&A[id].y,&A[id].z);
    }
    rep(i,1,n) {
        scanf("%d",&id);
        scanf("%d %lf %lf %lf",&Vb[id],&B[id].x,&B[id].y,&B[id].z);     
    }
}

bool cmp(Dist a1,Dist a2) {
    if(a1.d == a2.d) return a1.v > a2.v;
    return a1.d < a2.d;
}

bool compare(int v,int u1,int u2) {
    Dist a1,a2;
    a1.d = B[v].dist(A[u1]); a1.v = Va[u1];
    a2.d = B[v].dist(A[u2]); a2.v = Va[u2];
    return cmp(a1,a2);
}

int matchA[205];
int matchB[205];
int lick[205];

void debug() {
    rep(i,1,n) {
        rep(j,1,n) {
            printf("%d ",RankA[i][j]);
        }
        puts("");
    }
}

void solve() {
    rep(i,1,n) {
        rep(j,1,n) { dist[j].d = A[i].dist(B[j]); dist[j].id = j; dist[j].v = Vb[j]; }
        sort(dist+1,dist+1+n,cmp);
        rep(j,1,n) { RankA[i][j] = dist[j].id;}// printf("%f ",dist[j].d); } puts("");
    }

    //开始
    cls(matchA,0);
    cls(matchB,0);
    cls(lick,0);
    int flg = 0;
    while(1) { //可能不止n轮
        int ok = 0;
        rep(i,1,n) { //第i个发射塔
            if(!matchA[i]) {
                ok = 1;
                int u = ++lick[i];
                if(u > n) {flg = 1 ; break;}
                int v = RankA[i][u]; //追求的人
                if(!matchB[v]) {
                    matchB[v] = i;
                    matchA[i] = v;
                }
                else {
                    if(compare(v,i,matchB[v])) {
                        matchA[matchB[v]] = 0;
                        matchB[v] = i;
                        matchA[i] = v;
                    }
                }
            }
        }
        if(flg) break;
        if(!ok) break;
    }
    // debug();
    if(flg) puts("Impossible");
    else {
        rep(i,1,n) printf("%d %d\n",i,matchA[i]);
        puts("");
    }
}

int main(void) {
    //freopen("a.in","r",stdin);
    scanf("%d",&T); while(T--) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值