题意:给出n个宝物,m个寻宝人。

每个寻宝人对n个宝物都有一个估价。

从这些估价中挑出某些,是的价值差最小。

dfs。。没什么好说的,暴力枚举!

 /*
搜索+枚举
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std; const int Max = 0x7fffffff;
struct Node{
int val[ ];
}a[ ];
struct Node2{
int num[ ];
int cnt ,sum ;
}ans[ ],dfs_ans[ ];
int vis[ ];
int diff;
bool flag[ ]; void init(){
diff = Max;
memset( flag,false,sizeof( flag ) );
//memset( vis,0,sizeof( vis ) );
for( int i=;i<;i++ ){
for( int j=;j<;j++ ){
//a[ i ].val[ j ] = 0;
ans[ i ].num[ j ] = dfs_ans[ i ].num[ j ] = ;
}
ans[ i ].cnt = ans[ i ].sum = dfs_ans[ i ].cnt = dfs_ans[ i ].sum = ;
}
} void init_vis( int n ){
int N = (<<n);
for( int i=;i<N;i++ ){
for( int j=;j<n;j++ ){
if( i&(<<j) ) vis[ j ] = ;
else vis[ j ] = ;
//printf("%d",vis[j]);
}
//printf("\n");
}
} void dfs( int cur_hunter,int cur_treasure,int num_treasure,int hunter ){
if( cur_hunter==hunter&&cur_treasure==num_treasure ){
int t_max = dfs_ans[].sum;
int t_min = dfs_ans[].sum;
for( int i=;i<hunter;i++ ){
t_max = max( t_max,dfs_ans[i].sum );
t_min = min( t_min,dfs_ans[i].sum );
}
if( t_max-t_min<diff ){
diff = t_max-t_min;
for( int i=;i<hunter;i++ )
ans[i] = dfs_ans[i];
}
return ;
}
if( cur_hunter>=hunter ) return ;
int N = (<<num_treasure);
for( int i=;i<N;i++ ){
int t_cnt = ;
bool p_flag = true;
for( int j=;j<num_treasure;j++ ){
if( i&(<<j) ) vis[ j ] = ;
else {
vis[ j ] = ;
t_cnt++;
if( flag[j]==true ) {
p_flag = false;
break;
}
}
}
if( /*t_cnt!=0&&*/p_flag==true&&( t_cnt+cur_treasure+hunter-cur_hunter- )<=num_treasure ){
int tt = ;
dfs_ans[ cur_hunter ].sum = ;
dfs_ans[ cur_hunter ].cnt = t_cnt;
for( int j=;j<num_treasure;j++ ){
if( vis[j] == ){
dfs_ans[ cur_hunter ].num[ tt++ ] = j;
flag[ j ] = true;
dfs_ans[ cur_hunter ].sum += a[ cur_hunter ].val[j];
}
}
dfs( cur_hunter+,cur_treasure+t_cnt,num_treasure,hunter);
for( int j=;j<tt;j++ ){
flag[ dfs_ans[ cur_hunter ].num[j] ] = false;
}
}
}
return ;
} int main(){
int num_treasure,hunter;
char s[ ];
int ca = ;
//freopen("out.txt","w",stdout);
while( scanf("%s",s)!=EOF ){
init();
scanf("%d%d",&num_treasure,&hunter);//宝藏,寻宝人
for( int i=;i<hunter;i++ ){
for( int j=;j<num_treasure;j++ ){
scanf("%d",&a[i].val[j]);
}
}
scanf("%s",s);
if( num_treasure== ){
int m_min = a[].val[];
int m_num = ;
for( int i=;i<hunter;i++ ){
if( m_min>a[i].val[] ){
m_min = a[ i ].val[];
m_num = i;
}
}
if( ca!= ) printf("\n");
ca++;
for( int i=;i<hunter;i++ ){
if( i==m_num ){
printf("1 %d\n",m_min);
}
else
printf("0\n");
}
continue;
}
dfs( ,,num_treasure,hunter );
if( ca!= ) printf("\n");
ca++;
for( int i=;i<hunter;i++ ){
for( int j=;j<ans[i].cnt;j++ ){
if( j== )
printf("%d",ans[i].num[j]+);
else
printf(" %d",ans[i].num[j]+);
}
if( ans[i].cnt> ) printf(" %d\n",ans[i].sum);
else printf("%d\n",ans[i].sum);
}
}
return ;
}
04-25 04:19