HDU 1435 Stable Match 稳定婚姻

来源:互联网 发布:u盘怎么安装mac系统 编辑:程序博客网 时间:2024/05/23 16:54

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

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-8using 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 0
原创粉丝点击