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

USACO Section 2.1: Healthy Holsteins

时间:2014-09-22 13:02:22      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   div   sp   

长久没写了,连个dfs都写不好了

 1 /*
 2 ID: yingzho2
 3 PROG: holstein
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <fstream>
 8 #include <string>
 9 #include <map>
10 #include <vector>
11 #include <set>
12 #include <algorithm>
13 #include <queue>
14 #include <cmath>
15 #include <list>
16 #include <cstring>
17 #include <cstdlib>
18 #include <limits>
19 #include <stack>
20 
21 using namespace std;
22 
23 ofstream fout ("holstein.out");
24 ifstream fin ("holstein.in");
25 
26 int V, G;
27 int target[30], input[30][30], cur[30], ans[30], tmp[30], size = 30;
28 
29 
30 bool check() {
31     for (int i = 0; i < V; ++i)
32         if (cur[i] < target[i]) return false;
33     return true;
34 }
35 
36 void dfs(int x, int sum) {
37     if (x > G) return;
38     for (int i = 0; i < V; ++i) cur[i] += input[x][i];
39     tmp[sum] = x;
40     if (check()) {
41         if (sum+1 < size) {
42             size = sum + 1;
43             for (int i = 0; i < V; ++i) ans[i] = tmp[i];
44         }
45     }
46     else dfs(x+1, sum+1);
47     for (int i = 0; i < V; ++i) cur[i] -= input[x][i];
48     dfs(x+1, sum);
49 }
50 
51 
52 int main()
53 {
54     fin >> V;
55     for (int i = 0; i < V; ++i) fin >> target[i];
56     fin >> G;
57     for (int i = 0; i < G; ++i) {
58         for (int j = 0; j < V; ++j) {
59             fin >> input[i][j];
60         }
61     }
62     dfs(0, 0);
63     fout << size << " ";
64     for (int i = 0; i < size-1; ++i) fout << ans[i]+1 << " ";
65     fout << ans[size-1]+1 << endl;
66     return 0;
67 }

 

USACO Section 2.1: Healthy Holsteins

标签:style   blog   color   io   os   ar   for   div   sp   

原文地址:http://www.cnblogs.com/yingzhongwen/p/3985580.html

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