#include<iostream>
#include<fstream>
using namespace std;
const int MAX = 50;
int n, m, k;
int p[MAX][MAX];
int min = 1000000; //最小总费用
int cur = 0; //目前费用
int r[MAX];
void compute()
{
for(int i=1; i<=n; i++)
cur += p[i][r[i]];
if(cur < min)
min = cur;
}
void backtrack(int dep)
{
if(dep >= n)
compute();
for(int i=dep+1; i<=n; i++)
{
swap(r[dep], r[i]);
backtrack(dep+1);
swap(r[dep], r[i]);
}
}
int main()
{
ifstream fin("工作分配.txt");
cout << "\n输入工作数:";
fin >> n; cout << n;
int i, j;
cout << "\n输入工作费用矩阵:\n";
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
fin >> p[i][j];
cout << p[i][j] << "\t";
}
cout << endl;
}
for(i=1; i<=n; i++)
r[i] = i;
backtrack(1);
cout << "\n最小总费用为:" << min;
cout << endl << endl;
return 0;
}
工作分配问题
最新推荐文章于 2023-12-04 21:06:30 发布