您的位置:首页 > 其它

工作分配问题

2015-11-28 21:29 344 查看
#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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: