运动员最佳匹配问题

来源:互联网 发布:centos pdo mysql.so 编辑:程序博客网 时间:2024/04/28 16:50
#include <iostream>#include <fstream>using namespace std;const int INF = 100000;const int MAX = 50;int p[MAX][MAX];int q[MAX][MAX];int x[MAX];int n; //男女运动员人数int bestw = 0;int cw = 0;void compute(){    cw = 0;    for(int i=1; i<=n; i++)        cw += p[i][x[i]] * q[x[i]][i];    if(cw > bestw)        bestw = cw;}void backtrack(int i){    if(i>n)    {        compute();    }    else        for(int j=i; j<=n; j++) //女运动员        {            swap(x[i], x[j]);            backtrack(i+1);            swap(x[i], x[j]);        }}int main(){    ifstream fin("运动员最佳匹配.txt");    cout << "输入男女运动员人数:";    fin >> n; cout << n;    cout << "\n输入混合双打中男运动员竞赛优势矩阵:\n";    int i, j;    for(i=1; i<=n; i++)    {        for(j=1; j<=n; j++)        {            fin >> p[i][j];              cout << p[i][j] << " ";        }        cout << endl;    }    cout << "\n输入混合双打中女运动员竞赛优势矩阵:\n";    for(i=1; i<=n; i++)    {        for(j=1; j<=n; j++)        {            fin >> q[i][j];              cout << q[i][j] << " ";        }        cout << endl;    }    for(i=1; i<=n; i++)        x[i] = i;    backtrack(1);    cout << "\n男女双方竞赛优势总和最大值为:" << bestw;    cout << endl;    cout << endl;    fin.close();    return 0;}

这里写图片描述

0 0