题意:给出n个宝物,m个寻宝人。
每个寻宝人对n个宝物都有一个估价。
从这些估价中挑出某些,是的价值差最小。
dfs。。没什么好说的,暴力枚举!
1 /* 2 搜索+枚举 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 using namespace std; 9 10 const int Max = 0x7fffffff; 11 struct Node{ 12 int val[ 10 ]; 13 }a[ 10 ]; 14 struct Node2{ 15 int num[ 10 ]; 16 int cnt ,sum ; 17 }ans[ 10 ],dfs_ans[ 10 ]; 18 int vis[ 10 ]; 19 int diff; 20 bool flag[ 10 ]; 21 22 void init(){ 23 diff = Max; 24 memset( flag,false,sizeof( flag ) ); 25 //memset( vis,0,sizeof( vis ) ); 26 for( int i=0;i<10;i++ ){ 27 for( int j=0;j<10;j++ ){ 28 //a[ i ].val[ j ] = 0; 29 ans[ i ].num[ j ] = dfs_ans[ i ].num[ j ] = 0; 30 } 31 ans[ i ].cnt = ans[ i ].sum = dfs_ans[ i ].cnt = dfs_ans[ i ].sum = 0; 32 } 33 } 34 35 void init_vis( int n ){ 36 int N = (1<<n); 37 for( int i=0;i<N;i++ ){ 38 for( int j=0;j<n;j++ ){ 39 if( i&(1<<j) ) vis[ j ] = 0; 40 else vis[ j ] = 1; 41 //printf("%d",vis[j]); 42 } 43 //printf(" "); 44 } 45 } 46 47 void dfs( int cur_hunter,int cur_treasure,int num_treasure,int hunter ){ 48 if( cur_hunter==hunter&&cur_treasure==num_treasure ){ 49 int t_max = dfs_ans[0].sum; 50 int t_min = dfs_ans[0].sum; 51 for( int i=1;i<hunter;i++ ){ 52 t_max = max( t_max,dfs_ans[i].sum ); 53 t_min = min( t_min,dfs_ans[i].sum ); 54 } 55 if( t_max-t_min<diff ){ 56 diff = t_max-t_min; 57 for( int i=0;i<hunter;i++ ) 58 ans[i] = dfs_ans[i]; 59 } 60 return ; 61 } 62 if( cur_hunter>=hunter ) return ; 63 int N = (1<<num_treasure); 64 for( int i=0;i<N;i++ ){ 65 int t_cnt = 0; 66 bool p_flag = true; 67 for( int j=0;j<num_treasure;j++ ){ 68 if( i&(1<<j) ) vis[ j ] = 0; 69 else { 70 vis[ j ] = 1; 71 t_cnt++; 72 if( flag[j]==true ) { 73 p_flag = false; 74 break; 75 } 76 } 77 } 78 if( /*t_cnt!=0&&*/p_flag==true&&( t_cnt+cur_treasure+hunter-cur_hunter-1 )<=num_treasure ){ 79 int tt = 0; 80 dfs_ans[ cur_hunter ].sum = 0; 81 dfs_ans[ cur_hunter ].cnt = t_cnt; 82 for( int j=0;j<num_treasure;j++ ){ 83 if( vis[j] == 1 ){ 84 dfs_ans[ cur_hunter ].num[ tt++ ] = j; 85 flag[ j ] = true; 86 dfs_ans[ cur_hunter ].sum += a[ cur_hunter ].val[j]; 87 } 88 } 89 dfs( cur_hunter+1,cur_treasure+t_cnt,num_treasure,hunter); 90 for( int j=0;j<tt;j++ ){ 91 flag[ dfs_ans[ cur_hunter ].num[j] ] = false; 92 } 93 } 94 } 95 return ; 96 } 97 98 int main(){ 99 int num_treasure,hunter; 100 char s[ 12 ]; 101 int ca = 1; 102 //freopen("out.txt","w",stdout); 103 while( scanf("%s",s)!=EOF ){ 104 init(); 105 scanf("%d%d",&num_treasure,&hunter);//宝藏,寻宝人 106 for( int i=0;i<hunter;i++ ){ 107 for( int j=0;j<num_treasure;j++ ){ 108 scanf("%d",&a[i].val[j]); 109 } 110 } 111 scanf("%s",s); 112 if( num_treasure==1 ){ 113 int m_min = a[0].val[0]; 114 int m_num = 0; 115 for( int i=1;i<hunter;i++ ){ 116 if( m_min>a[i].val[0] ){ 117 m_min = a[ i ].val[0]; 118 m_num = i; 119 } 120 } 121 if( ca!=1 ) printf(" "); 122 ca++; 123 for( int i=0;i<hunter;i++ ){ 124 if( i==m_num ){ 125 printf("1 %d ",m_min); 126 } 127 else 128 printf("0 "); 129 } 130 continue; 131 } 132 dfs( 0,0,num_treasure,hunter ); 133 if( ca!=1 ) printf(" "); 134 ca++; 135 for( int i=0;i<hunter;i++ ){ 136 for( int j=0;j<ans[i].cnt;j++ ){ 137 if( j==0 ) 138 printf("%d",ans[i].num[j]+1); 139 else 140 printf(" %d",ans[i].num[j]+1); 141 } 142 if( ans[i].cnt>0 ) printf(" %d ",ans[i].sum); 143 else printf("%d ",ans[i].sum); 144 } 145 } 146 return 0; 147 }