#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 100000;
const int MAX = 50;
int p[MAX][MAX]; //男运动员竞赛优势矩阵
int q[MAX][MAX]; //女运动用竞赛优势矩阵
int bestx[MAX]; //解向量
int n; //男女运动员人数
class Node
{
public:
int dep; //当前层
int win; //竞赛优势
int *x; //解向量
Node(int d, int w)
{
x = new int[n+1];
dep = d;
win = w;
}
int getWin() //计算竞赛优势
{
int temp = 0;
for(int i=1; i<=dep; i++)
temp += p[i][x[i]] * q[x[i]][i];
return temp;
}
//竞赛优势大的先出队列
bool operator < (const Node &node) const
{
return win < node.win;
}
};
int search()
{
priority_queue<Node> q;
Node enode(0, 0);
for(int j=1; j<=n; j++)
enode.x[j] = j;
int best = 0;
while(true)
{
if(enode.dep == n)
{
if(enode.win > best)
{
best = enode.win;
copy(enode.x, enode.x+n+1, bestx);
break;
}
}
else
{
for(int i=enode.dep+1; i<=n; i++)
{
Node now(enode.dep+1, 0);
copy(enode.x, enode.x+n+1, now.x);
now.x[now.dep] = enode.x[i];
now.x[i] = enode.x[now.dep];
now.win = now.getWin();
q.push(now);
}
}
if(q.empty())
break;
else
{
enode = q.top();
q.pop();
}
}
return best;
}
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;
}
cout << "\n男女双方竞赛优势总和最大值为:" << search();
cout << endl;
cout << endl;
fin.close();
return 0;
}
运动员最佳匹配问题
最新推荐文章于 2023-03-11 11:02:23 发布