zoukankan      html  css  js  c++  java
  • hdoj1435 Stable Match(稳定婚姻问题)

    简单稳定婚姻问题。

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

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

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<queue>
     6 #define maxn 205
     7 using namespace std;
     8 struct point{
     9     int num,v;
    10     double x,y,z;
    11 }Man_array[maxn],Woman_array[maxn];
    12 struct point2{
    13     int num,v;
    14     double dis;
    15 }A[maxn*maxn];
    16 int n;
    17 int nowb[maxn],nowg[maxn],V[maxn][maxn],B_g[maxn][maxn],G_b[maxn][maxn];
    18 double calc(point p,point q){
    19     double xx=p.x-q.x,yy=p.y-q.y,zz=p.z-q.z;
    20     return sqrt(xx*xx+yy*yy+zz*zz);
    21 }
    22 int comp(point2 p,point2 q){
    23     return p.dis<q.dis || p.dis==q.dis && p.v>q.v;
    24 }
    25 void Stable_Marriage(){
    26     memset(V,0,sizeof(V));memset(nowb,-1,sizeof(nowb));memset(nowg,-1,sizeof(nowg));
    27     queue<int> Q;
    28     for (int i=0;i<n;i++) Q.push(i);
    29     while (!Q.empty()){
    30         int pre=Q.front(),res;
    31         Q.pop();
    32         for (int i=0;i<n;i++){
    33             int res=B_g[pre][i];
    34             if (V[pre][res]) continue;
    35             V[pre][res]=1;
    36             if (nowg[res]==-1){
    37                 nowg[res]=pre;
    38                 nowb[pre]=res;
    39                 break;
    40             }
    41             else if (G_b[res][nowg[res]]<G_b[res][pre]){
    42                 Q.push(nowg[res]);
    43                 nowg[res]=pre;
    44                 nowb[pre]=res;
    45                 break;
    46             }
    47         }
    48     }
    49 }
    50 int main(){
    51     ios::sync_with_stdio(false);
    52     cin.tie(0);cout.tie(0);
    53     int t;
    54     cin >> t;
    55     while (t--){
    56         cin >> n;
    57         for (int i=0;i<n;i++){
    58             cin >> Man_array[i].num >> Man_array[i].v >> Man_array[i].x >> Man_array[i].y >> Man_array[i].z;
    59         }
    60         for (int i=0;i<n;i++){
    61             cin >> Woman_array[i].num >> Woman_array[i].v >> Woman_array[i].x >> Woman_array[i].y >> Woman_array[i].z;
    62         }
    63         //男——女 
    64         for (int i=0;i<n;i++){
    65             for (int j=0;j<n;j++){
    66                 A[j].dis=calc(Man_array[i],Woman_array[j]);
    67                 A[j].v=Woman_array[j].v;
    68                 A[j].num=j;
    69             }
    70             sort(A,A+n,comp);
    71             for (int j=0;j<n;j++) B_g[i][j]=A[j].num;
    72         }
    73         //女——男
    74         for (int i=0;i<n;i++){
    75             for (int j=0;j<n;j++){
    76                 A[j].dis=calc(Woman_array[i],Man_array[j]);
    77                 A[j].v=Man_array[j].v;
    78                 A[j].num=j;
    79             }
    80             sort(A,A+n,comp);
    81             for (int j=0;j<n;j++) G_b[i][A[j].num]=n-j+1;
    82         } 
    83         Stable_Marriage();
    84         for (int i=0;i<n;i++){
    85             cout << Man_array[i].num << " " << Woman_array[nowb[i]].num << endl;
    86         }
    87         cout << endl;
    88     }
    89     return 0;
    90 }
  • 相关阅读:
    JMeter 关联
    JMeter MD5加密
    JMeter 时间函数
    JMeter 常用设置
    JMeter 服务器资源监控
    js制作列表滚动(有滚动条)
    js监听事件
    获取窗口大小 并自适应大小变化
    js 标签云
    js 显示数字不断增加
  • 原文地址:https://www.cnblogs.com/changer-qyz/p/8453217.html
Copyright © 2011-2022 走看看