标签:ini guarantee 存在 work 为我 c++ style 序号 his
今天在刷 USACO 2.1 PROB Healthy Holsteins
这题需要以这种方式搜索 1 ~ n
以n = 4 为例:
搜索顺序为:
(1),(2),(3),(4),
(1,2),(1,3),(1,4) ,(2,3),(2,4),(3,4)
(1,2,3),(1,2,4) , (1,3,4)
(1,2,3,4)
——
因为每次搜索的数字位数不同,所以需要用到递归。
递归在这里是决定循环层数的(循环和递归决定的维度不同)
但是这种dfs一直不会写。
今天看了别人的代码,死啃很久,Step one by one 好久。
大概能理解了。
把递归一方面看成是一个整体,高度抽象出来。只关注每次递归的意思
比如 dfs(t+1) 表示的在确定第t位后,确定第t+1是否拿,拿到第几个?
——
我以为我理解了dfs,突然发现自己对他的理解还是很浅薄。
讲不出来什么。
——
代码如下:
1 /* 2 ID:xiekeyi1 3 PROG:holstein 4 LANG:C++ 5 */ 6 #include<bits/stdc++.h> 7 using namespace std ; 8 const int maxn = 30 ; 9 int v , vv[maxn] , g , gg[maxn][maxn]; 10 11 void init() 12 { 13 cin >> v ; 14 for( int i = 1 ; i <= v ; i++) 15 cin >> vv[i]; 16 cin >> g ; 17 for( int i = 1 ; i <= g ; i++) 18 for( int j = 1 ; j <= v ; j++) 19 cin >> gg[i][j]; 20 } 21 22 bool flag = false ; 23 int num[maxn]; 24 int cnt = 0 ; 25 int k = 1 ; 26 void work() 27 { 28 int a[30] = {0}; 29 for( int i = 1 ; i <= cnt ; i++) 30 for( int j = 1 ; j <= v ; j++) 31 a[j] += gg[ num[i] ][j] ; 32 for( int i = 1 ; i <= v ; i++) 33 if( a[i] < vv[i] ) 34 return ; 35 36 flag = true ; 37 cout << cnt ; 38 for( int i = 1 ; i <= cnt ; i++) 39 cout << ‘ ‘ << num[i] ; 40 cout << endl ; 41 } 42 43 void dfs( int t ) 44 { 45 if( flag || t > g ) return ; 46 if( cnt >= k ) 47 { 48 work() ; 49 return ; 50 } 51 52 cnt++; 53 num[cnt] = t + 1 ; 54 dfs( t + 1 ) ; 55 cnt--; 56 dfs( t + 1 ) ; 57 return ; 58 } 59 60 61 int main() 62 { 63 freopen("holstein.in","r",stdin); 64 freopen("holstein.out","w",stdout); 65 init() ; 66 while( !flag ) 67 { 68 dfs(0); 69 k++; 70 } 71 return 0 ; 72 }
Farmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for the cows. Help Farmer John feed his cows so they stay healthy while minimizing the number of scoops that a cow is fed.
Given the daily requirements of each kind of vitamin that a cow needs, identify the smallest combination of scoops of feed a cow can be fed in order to meet at least the minimum vitamin requirements.
Vitamins are measured in integer units. Cows can be fed at most one scoop of any feed type. It is guaranteed that a solution exists for all contest input data.
Line 1: | integer V (1 <= V <= 25), the number of types of vitamins |
Line 2: | V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each day |
Line 3: | integer G (1 <= G <= 15), the number of types of feeds available |
Lines 4..G+3: | V integers (0 <= each one <= 1000), the amount of each vitamin that one scoop of this feed contains. The first line of these G lines describes feed #1; the second line describes feed #2; and so on. |
4 100 200 300 400 3 50 50 50 50 200 300 200 300 900 150 389 399
The output is a single line of output that contains:
If more than one set of feedtypes yield a minimum of scoops, choose the set with the smallest feedtype numbers.
2 1 3
题意:
农民JOHN以拥有世界上最健康的奶牛为傲。他知道每种饲料中所包含的牛所需的最低的维他命量是多少。请你帮助农夫喂养他的牛,以保持它们的健康,使喂给牛的饲料的种数最少。
给出牛所需的最低的维他命量,输出喂给牛需要哪些种类的饲料,且所需的饲料剂量最少。
维他命量以整数表示,每种饲料最多只能对牛使用一次,数据保证存在解
PROGRAM NAME: holstein
INPUT FORMAT:
(file holstein.in)
第1行:一个整数V(1<=V<=25),表示需要的维他命的种类数。
第2行:V个整数(1<=每个数<=1000),表示牛每天需要的每种维他命的最小量。
第3行:一个整数G(1<=G<=15),表示可用来喂牛的饲料的种数。
下面G行,第n行表示编号为n饲料包含的各种维他命的量的多少。
OUTPUT FORMAT:
(file holstein.out)
输出文件只有一行,包括
牛必需的最小的饲料种数P
后面有P个数,表示所选择的饲料编号(按从小到大排列)。
如果有多个解,输出饲料序号最小的(即字典序最小)。
(摘自NOCOW)
标签:ini guarantee 存在 work 为我 c++ style 序号 his
原文地址:http://www.cnblogs.com/xiekeyi98/p/6843206.html