#include <bits/stdc++.h>
using namespace std;
#define maxn 1000
int n;
int cc;//当前费用
int bestc=0x3f3f3f;//最少费用
int c[maxn][maxn];//第件工作第j个人需要的费用
int x[maxn];//解空间
void BackTrack(int i)
{
if(i==n)
{
if(cc+c[i][x[i]]<bestc)
{
bestc=cc+c[i][x[i]];
}
}
else
{
for(int j=i;j<=n;j++)
{
if(cc+c[i][x[j]]<bestc)
{
swap(x[i],x[j]);
cc+=c[i][x[i]];
BackTrack(i+1);
cc-=c[i][x[i]];
swap(x[i],x[j]);
}
}
}
}
/*
3
10 2 3
2 3 4
3 4 5
*/
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
x[i]=i;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>c[i][j];
}
}
BackTrack(1);
cout<<bestc;
}
工作分配问题
最新推荐文章于 2024-01-17 11:34:18 发布