codeup26683_分工问题

codeup26683_分工问题

时空限制    1000ms/128MB

题目描述

    设有A,B,C,D,E五人从事J1,J2,J3,J4,J5五项工作,每人只能从事一项,他们的效益如下。

    每人选择五项工作中的一项,在各种选择的组合中,找到效益最高的的一种组合输出。

输入

    无输入。

输出

    前面五行,输出五人分配的工作;

    最后一行输出:supply:最佳效益值。(参考样例输出)

样例输出

A:J5
B:J3
C:J4
D:J1
E:J2
supply:50

代码

#include<iostream>
using namespace std;
const int N = 6;
const int data[N][N] = {{ 0, 0, 0, 0, 0 ,0},
						{ 0,13,11,10, 4, 7},
						{ 0,13,10,10, 8, 5},
						{ 0, 5, 9, 7, 7, 4},
						{ 0,15,12,10,11, 5},
						{ 0,10,11, 8, 8, 4}};
int a[N],f[N],ans;
bool used[N];

void search(int i,int tot){
	if (i>5){
		if (tot>ans){
			ans = tot;	//存最大值
			for (int j=1; j<=5; j++) f[j]=a[j];	//记录最大值时工作分配
		}
		return;
	}
	for (int j=1; j<=5; j++)
		if (!used[j]){
			a[i] = j;
			used[j] = true;
			search(i+1,tot+data[i][j]);
			used[j] = false;
		}
}

int main(){
	search(1,0);	//从第一个开始,最大收益0
	for (int i=1; i<=5; i++)
		cout<<char(i+64)<<":J"<<f[i]<<endl;
	cout<<"supply:"<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/WDAJSNHC/article/details/81301972