码迷,mamicode.com
首页 > 其他好文 > 详细

任务安排(搜索)

时间:2017-05-24 19:22:16      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:输入   div   struct   continue   任务   include   use   main   bool   

给定N(<=30)个人和N台机器,以及每个人操作某台机器的收益。请计算所有人都工作的情况下,最大的收益和。
输入:
第一行两个数分别表示人和机器的数量(两数相等)
接下来若干行,每行三个数,a,b,c表示a 操作机器b的收益为c.
输出:
两个数。(第一个数表示参加工作的人数(此数为N),第二个数表示最大收益和。)
样例:
输入:
3 3
1 2 10
1 1 9
1 3 3
2 1 10
2 2 9
2 3 3
3 1 8
3 2 9
3 3 8
输出:
3 28

简单搜索,暴力剪枝。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std ;
 7 
 8 int cnt, jv[31][31][2], sum[31], ans ;  
 9 bool used[31] ; 
10 struct id { int sze, chu ; }; id lie[31] ;
11 
12 int cmp ( id a, id b ) {
13     return  a.chu < b.chu ;
14 }
15 
16 
17 void dfs ( int sze, int jia ) {
18     if( sze> cnt ) {
19         if(ans < jia ) ans = jia ;
20         return ;
21     }
22     int now = lie[sze].sze ;
23     
24     if( sum[cnt] - sum[sze-1] + jia <= ans ) return ;
25     for(int x = jv[now][0][0]; x>=1; x--) {
26         int to = jv[now][x][0] ;
27         if( used[to] ) continue ;    
28         used[to] = 1 ;
29         dfs( sze+1, jia+jv[now][x][1] ) ;
30         used[to] = 0;
31     }
32     return ;
33 }
34 
35 
36 
37 int main ( ) {
38 //    freopen( "1.txt", "r", stdin ) ;
39     scanf( "%d", &cnt ) ; scanf( "%d", &cnt ) ;
40     int a, b, c;
41     int n[31] ; memset( n, 0, sizeof(n) ) ;
42     while( cin>>a>>b>>c ) {
43         jv[a][ ++jv[a][0][0] ][0] = b ; 
44         jv[a][ jv[a][0][0] ][1] = c ;
45         n[a] = max( n[a], c ) ;
46     }
47     for(int x = 1; x <= cnt; x++) {
48         lie[x].chu = jv[x][0][0] ;
49         lie[x].sze = x ;
50     }
51     sort(lie+1, lie+1+cnt, cmp) ;
52         
53     for( int x = 1; x <= cnt; x++) {
54         int now = lie[x].sze ;
55         sum[x] = sum[x-1] + n[now] ;    
56     }
57     dfs( 1, 0 ) ;
58     printf( "%d %d\n", cnt, ans) ;
59 }

 

任务安排(搜索)

标签:输入   div   struct   continue   任务   include   use   main   bool   

原文地址:http://www.cnblogs.com/Ateisti/p/6900320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!