Employees assigned to work (depth-first)

Meaning of the title: the company assigned to work items N N employees, each employee can only be given to a work, a different person each time processing operations. Seeking to complete all the work required minimum time.

Input: The first line of input integer N, N represents employees, employee number from 1 to N (1 <= N <= 10)

    Now enter a two-dimensional N * N matrix task [N] [N], wherein the task [i] [j] (0 <= task [i] [j] <= 1000) working on behalf of the i-th item number j staff time required to complete.

Output: an integer representing the minimum time required to complete the work of all the work.

Example:

Input:

6
10 11 12 11 9 11
11 9 10 13 11 12
12 10 11 10 13 9
9 14 9 10 10 11
10 10 9 11 12 11
10 7 10 10 10 8

Output:

54

dfs search violence deep (depth-first)

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int task[15][15]; //
 5 bool used[15];
 6 int ans;
 7 int n;
 8 void dfs(int x,int t){
 9     if(x==n){
10         if(t<ans){
11             ans=t;
12         }
13         return;
14     }
 15      for ( int I = 0 ; I <n-; I ++ ) {
 16          // determines whether an employee is used 
. 17          IF (! {Used [I])
 18 is              Used [I] = to true ;
 . 19              DFS (X + . 1 , T + Task [X] [I]);
 20 is              Used [I] = to false ;
 21 is          }
 22 is      }
 23 is  }
 24  int main ()
 25  {
 26 is      CIN >> n-;
 27      for ( int I =0 ; I <n-; I ++ ) {
 28          for ( int J = 0 ; J <n-; J ++ ) {
 29             CIN >> Task [I] [J];
 30          }
 31 is      }
 32      ANS = 200000 ;
 33 is      // from 0 began searching line, it takes time for the beginning of 0 
34 is      DFS ( 0 , 0 );
 35      COUT << ANS;
 36      return  0 ;
 37 [ }

 

Guess you like

Origin www.cnblogs.com/mld-code-life/p/12234839.html