HDU - 1522 Marriage is Stable && HDU - 1914 The Stable Marriage Problem 稳定婚姻问题,可做模板

题意:裸的稳定婚姻问题,不过这个题的输入是字符串,要处理一下。

链接:hdu - 1522

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 2000 + 5;

int B_g[maxn][maxn], G_b_score[maxn][maxn];//男生追求的顺序 && 女生对男生的评价
int bg[maxn], gb[maxn];//   bg:boy -> girl  &&  gb:girl -> boy
bool mark[maxn][maxn];//男生是否追求过这个女生
int n;

void stable_marry() {
    memset(mark, false, sizeof(mark));
    memset(bg, -1, sizeof(bg));
    memset(gb, -1, sizeof(gb));
    queue<int> q;
    while(!q.empty())
        q.pop();
    for(int i = 1; i <= n; i++) {
        q.push(i);
    }
    int head, nxt;
    while(!q.empty()) {
        head = q.front();
        q.pop();
        for(int i = 1; i <= n; i++) {
            int nxt = B_g[head][i];
            if(mark[head][nxt])
                //男生追求过这个女生
                continue;
            mark[head][nxt] = 1;
            if(gb[nxt] == -1) {
                //女生还没有男朋友
                gb[nxt] = head;
                bg[head] = nxt;
                break;
            }
            else if(G_b_score[nxt][gb[nxt]] < G_b_score[nxt][head]) {
                //女生对追求者的评价比现男友高
                q.push(gb[nxt]);
                gb[nxt] = head;
                bg[head] = nxt;
                break;
            }
        }
    }
}



map<string, int > bmp;
map<string, int > gmp;
string bo[maxn], gi[maxn];


int main()
{
    while(~scanf("%d", &n)) {
        bmp.clear();
        gmp.clear();
        int kk = 0;
        for(int i = 1; i <= n; i++) {
            string str;
            cin >> str;
            bmp[str] = i;
            bo[i] = str;
            for(int j = 1; j <= n; j++) {
                string s;
                cin >> s;
                if(!gmp[s]) {
                    gmp[s] = ++kk;
                    gi[kk] = s;
                }
                B_g[i][j] = gmp[s];
            }
        }
        for(int i = 1; i <= n; i++) {
            string str;
            cin >> str;
            for(int j = 1; j <= n; j++) {
                string s;
                cin >> s;
                G_b_score[gmp[str]][bmp[s]] = n - j + 1;
            }
        }
        stable_marry();
        for(int i = 1; i <= n; i++) {
            cout << bo[i] << ' ' << gi[bg[i]] << endl;
        }
        printf("\n");
    }
    return 0;
}

不知这份代码为啥错了,还没明白,贴这儿有空再看(有空?再说吧):

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 2000 + 5;

int B_g[maxn][maxn], G_b_score[maxn][maxn];//男生追求的顺序 && 女生对男生的评价
int bg[maxn], gb[maxn];//   bg:boy -> girl  &&  gb:girl -> boy
bool mark[maxn][maxn];//男生是否追求过这个女生
int n;

void stable_marry() {
    memset(mark, false, sizeof(mark));
    memset(bg, -1, sizeof(bg));
    memset(gb, -1, sizeof(gb));
    queue<int> q;
    while(!q.empty())
        q.pop();
    for(int i = 1; i <= n; i++) {
        q.push(i);
    }
    int head, nxt;
    while(!q.empty()) {
        head = q.front();
        q.pop();
        for(int i = 1; i <= n; i++) {
            int nxt = B_g[head][i];
            if(mark[head][nxt])
                //男生追求过这个女生
                continue;
            mark[head][nxt] = 1;
            if(gb[nxt] == -1) {
                //女生还没有男朋友
                gb[nxt] = head;
                bg[head] = nxt;
                break;
            }
            else if(G_b_score[nxt][gb[nxt]] < G_b_score[nxt][head]) {
                //女生对追求者的评价比现男友高
                q.push(gb[nxt]);
                gb[nxt] = head;
                bg[head] = nxt;
                break;
            }
        }
    }
}

map<string, int > mp;
map<int, string > mpp;

int main()
{
    while(~scanf("%d", &n)) {
        mp.clear();
        mpp.clear();
        int kk = n;
        for(int i = 1; i <= n; i++) {
            string str;
            cin >> str;
            mp[str] = i;
            mpp[i] = str;
            for(int j = 1; j <= n; j++) {
                string s;
                cin >> s;
                if(!mp[s]) {
                    mp[s] = ++kk;
                    mpp[kk] = s;
                }
                B_g[i][j] = mp[s];
            }
        }
        for(int i = 1; i <= n; i++) {
            string str;
            cin >> str;
            for(int j = 1; j <= n; j++) {
                string s;
                cin >> s;
                G_b_score[mp[str]][mp[s]] = n - j + 1;
            }
        }
        stable_marry();
        for(int i = 1; i <= n; i++) {
            cout << mpp[i] << ' ' << mpp[bg[i]] << endl;
        }
        printf("\n");
    }
    return 0;
}

同样模板题,输入处理,输出格式。

链接:hdu - 1914

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 200 + 5;

int B_g[maxn][maxn], G_b_score[maxn][maxn];//男生追求的顺序 && 女生对男生的评价
int bg[maxn], gb[maxn];//   bg:boy -> girl  &&  gb:girl -> boy
bool mark[maxn][maxn];//男生是否追求过这个女生
int n;

void stable_marry() {
    memset(mark, false, sizeof(mark));
    memset(bg, -1, sizeof(bg));
    memset(gb, -1, sizeof(gb));
    queue<int> q;
    while(!q.empty())
        q.pop();
    for(int i = 1; i <= n; i++) {
        q.push(i);
    }
    int head, nxt;
    while(!q.empty()) {
        head = q.front();
        q.pop();
        for(int i = 1; i <= n; i++) {
            int nxt = B_g[head][i];
            if(mark[head][nxt])
                //男生追求过这个女生
                continue;
            mark[head][nxt] = 1;
            if(gb[nxt] == -1) {
                //女生还没有男朋友
                gb[nxt] = head;
                bg[head] = nxt;
                break;
            }
            else if(G_b_score[nxt][gb[nxt]] < G_b_score[nxt][head]) {
                //女生对追求者的评价比现男友高
                q.push(gb[nxt]);
                gb[nxt] = head;
                bg[head] = nxt;
                break;
            }
        }
    }
}

int main()
{
    int T;
    cin >> T;
    while(T--) {
        scanf("%d", &n);
        char ch;
        for(int i = 0; i < n; i++) {
            cin >> ch;
        }
        for(int i = 0; i < n; i++) {
            cin >> ch;
        }
        string s;
        for(int i = 0; i < n; i++) {
            cin >> s;
            int x = s[0] - 'a' + 1;
            for(int j = 2; j < s.length(); j++) {
                int y = s[j] - 'A' + 1;
                B_g[x][j - 1] = y;
            }
        }
        for(int i = 0; i < n; i++) {
            cin >> s;
            int x = s[0] - 'A' + 1;
            for(int j = 2; j < s.length(); j++) {
                int y = s[j] - 'a' + 1;
                G_b_score[x][y] = n - j + 3;
            }
        }
        stable_marry();
        for(int i = 1; i <= n; i++) {
            printf("%c %c\n", i + 'a' - 1, bg[i] + 'A' - 1);
        }
        if(T) printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c_cqq/article/details/81268199
今日推荐