Assignments (dfs)

In 2020, he opened garlic king big company with the N employees. Every day, garlic Jun N items to be assigned jobs to his staff, however, due to the ability of different abilities, each person vary for the same processing time needed for the work.
As we all know, is a garlic Jun attaches great importance to the efficiency of the people, he wanted to know how to change the distribution of work in order to make the total time to complete the work of all the work the minimum (each employee can only be assigned to a job). But we also all know that garlic is not an ordinary king lazy, so the garlic king found you, you save about garlic king of it!

Input format
of the first line of the input integer N, there are N representative of staff, the staff numbered from 1 to N (1 <= N <= 10).
Then enter a two-dimensional matrix of N * N Task [N] [N], Task [i] j refers to the item if i work done by the j number of employees needed time.

Output Format
Output minimum sum of times an integer representing desired.

Sample input
. 6
10. 11. 11 12 is. 11. 9
. 11. 11. 9 10 13 is 12 is
12 is 13 is 10. 9 10. 11
. 9 14. 11. 9 10 10
10 12 is. 11. 11. 9 10
10 10. 8 10. 7 10
sample output
54
Analysis: Title search key, our job is to get a job assignment, starting from the first job, but we have worked employees can not be assigned, so you need an array of tags used [15].

for(int i=0;i<n;i++){//每份工作分配给六个员工
		if(!used[i]){//前提这个员工没有工作才可以接下此工作
			used[i]=true;//标记当前员工得到了工作
			dfs(x+1,t+task[x][i]);//前去下一个工作选人
			used[i]=false;//回溯,解除标记
		}
	}

C language code:

#include<cstdio>
int task[15][15];
bool used[15];
int n;
int min=99999999;
void dfs(int x,int t){	
	if(x==n){//不能是n-1,因为到这里是代表要分配这份工作,是n才代表所有的工作都分配完成 
		if(t<min){
			min=t;//记录花费最短的时间 
		}
		return ;
	}
	for(int i=0;i<n;i++){//每份工作分配给六个员工
		if(!used[i]){//前提这个员工没有工作才可以接下此工作
			used[i]=true;//标记当前员工得到了工作
			dfs(x+1,t+task[x][i]);//前去下一个工作选人
			used[i]=false;//回溯,解除标记
		}
	}

	
} 
int main(){

	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			scanf("%d",&task[i][j]);
		}
	}
	dfs(0,0) ;//第一份工作和第一份工作花的时间 
	printf("%d",min);
	return 0;
} 

Here Insert Picture Description

Published 104 original articles · won praise 7 · views 5098

Guess you like

Origin blog.csdn.net/Anterior_condyle/article/details/104557257
dfs