工作分配问题

来源:互联网 发布:电信光猫网络g灯不亮 编辑:程序博客网 时间:2024/05/21 13:07
#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;}

这里写图片描述

0 0
原创粉丝点击