算法导论 · 蛮力法 · 任务分配问题

  • 算法说明
    对于任务分配问题,n个人n项任务,代价不同,蛮力法排列组合求解
  • 源代码
#include <cstdio>
#include <algorithm>
using namespace std;

#define n 4
#define max(a, b) (a) > (b) ? (a) : (b)

struct it {
	int w, v;
}; 

it goods[n + 1];

int main() {
	//input 
	int c[n][n] = { //代价矩阵, c[0][2]表示第一个人完成第三个任务所花费的代价 
		{9, 2, 7, 8},
		{6, 4, 3, 7},
		{5, 8, 1, 8},
		{7, 6, 9, 4} 
	};
	int a[n] = {0, 1, 2, 3};
	int minv = 1 << 29;

	do {
		int people = 0;
		int value = 0;
		for(int i = 0; i < n; i++) {
//			printf("%d ", a[i]);
			value += c[people++][a[i]];
		}
//		printf("%d\n", value);
		minv = min(minv, value); 
	} while(next_permutation(a, a + n)); //排列组合 
	
	//output
	printf("%d", minv);
	return 0;
} 
  • 运行结果
    在这里插入图片描述
发布了77 篇原创文章 · 获赞 40 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/y_dd6011/article/details/95519682
今日推荐