回溯:运动员最佳配对问题(C++语言)

羽毛球队有男女运动员各n人. 给定2个n*n矩阵P和Q. P[i][j]是男运动员i与女运动员j配混合双打的男运动员竞赛优势; Q[i][j]是女运动员i与男运动员j配混合双打的女运动员竞赛优势. 由于技术配合和心理状态等各种因素影响, P[i][j]不一定等于Q[j][i]. 男运动员i和女运动员j配对的竞赛优势是P[i][j]*Q[j][i]. 设计一个算法, 计算男女运动员最佳配对法, 使得各组男女双方竞赛优势的总和达到最大.

数据输入:第1行有一个正整数n(1<=n<=12), 接下来2n行是P和Q

结果输出:最佳配对的各组男女双方竞赛优势总和 


样例

输入(1)

3
10 2 3
2 3 4
3 4 5
2 2 2
3 5 3
4 5 1

输出(1)

52

代码

#include <iostream>
#include <climits>

using namespace std;

int n;
int boy[21][21], girl[21][21]; // Stores the competitiveness of male and female athletes
int Max = INT_MIN; // Represents the maximum total competitiveness for both genders
int sum = 0; // Temporary sum
int data[21][21]; // data[i][] stores the competitiveness when male athlete i pairs with a female
int maxSum[21]; // Records the maximum competitiveness achievable by each male athlete when paired
int book[21]; // Used to mark whether a female athlete is already paired: book[0] for unpaired, book[1] for paired

void dfs(int t) {
    if (t >= n) {
        // When t reaches n, all athletes have been marked, and we've found the maximum competitiveness
        Max = max(Max, sum);
        return;
    }

    int ctn = 0; // Pruning function: check if sum of already matched athletes (t to n-1) and the maximum competitiveness of t to n-1 males with females is less than Max
    for (int i = t; i < n; i++) {
        ctn += maxSum[i];
    }
    if (sum + ctn < Max) {
        return; // If sum + ctn is less than Max, prune this branch and set Max to the maximum found so far
    }

    for (int i = 0; i < n; i++) {
        if (!book[i]) {
            // Female athlete i is not yet paired
            book[i] = 1; // Pair male athlete t with female athlete i
            sum += data[t][i]; // Add the competitiveness of male athlete t with female athlete i to the sum
            dfs(t + 1); // Match the next male athlete
            book[i] = 0; // If the sum for male athlete t and female athlete i doesn't exceed Max, backtrack
            sum -= data[t][i];
        }
    }
}

int main() {
    cin >> n;

    for (int i = 0; i < n; i++) {
        // Input competitiveness of male athletes
        for (int j = 0; j < n; j++)
            cin >> boy[i][j];
    }

    for (int i = 0; i < n; i++) {
        // Input competitiveness of female athletes
        for (int j = 0; j < n; j++)
            cin >> girl[i][j];
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            data[i][j] = boy[i][j] * girl[j][i]; // Calculate competitiveness for each male athlete with each female athlete
            maxSum[i] = max(maxSum[i], data[i][j]); // Record the maximum competitiveness for each male athlete after pairing
        }
    }

    dfs(0); // Start finding the maximum competitiveness
    cout << Max << endl; // Output the maximum competitiveness
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值