工作分配问题
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
设有n件工作分配给n个人。将工作i分配给第j个人所需的费用为 cij。试设计一个算法,为每一个人都分配1 件不同的工作,并使总费用达到最小。
设计一个算法,对于给定的工作费用,计算最佳工作分配方案,使总费用达到最小。
Input
输入数据的第一行有1 个正整数n (1≤n≤20)。接下来的n行,每行n个数,表示工作费用。
Output
将计算出的最小总费用输出。
Sample Input
3
10 2 3
2 3 4
3 4 5
Sample Output
9
#include<iostream>
using namespace std;
int n,cost=0;
int x[100],c[100][100];
void work(int i,int count){
if(i>n){
cost = min(cost,count);
}
if(count<cost)
for(int j=1;j<=n;j++)
if(x[j] == 0){
x[j] = 1;
work(i+1,count+c[i][j]);
x[j] = 0;
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>c[i][j];
x[j] = 0;
}
cost+=c[i][i];
}
work(1,0);
cout<<cost<<endl;
system("pause");
return 0;
}