P1460 健康的荷斯坦奶牛 Healthy(DFS)

思路:这道题还是用了小小的剪枝,这里要注意的是该题有很多中构建树的顺序,但是,在这众多顺序中不一定都能保证输出的方案字典序最小。

构建搜索树:如图构建

     P1460 健康的荷斯坦奶牛 Healthy(DFS)

剪枝,emmm,看代码:

#include<iostream>
using namespace std;
int a[][];
int b[], an[], mm[], gg[];
int n, m, mins=, kk;
bool f(){
for (int i = ; i <= m;++i)
if (b[i] > an[i])return ;
return ;
}
void add_of_s(int s, int y){
for (int i = ; i <= m; ++i)
an[i] = an[i] + y*a[s][i];
} void dfs(int s, int k){
if (k > n){ return; }
if (kk >= mins){ return; } //剪枝
if (f()){ mins = kk; for (int i = ; i <= mins; ++i)gg[i] = mm[i]; }
for (int i = s+; i <= n;++i)
{
kk++; mm[kk] = i;
add_of_s(i, );
dfs(i , k + );
mm[kk] = ; kk--;
add_of_s(i, -);
}
} int main(){
cin >> m;
for (int i = ; i <= m; ++i)cin >> b[i];
cin >> n;
for (int i = ; i <= n;++i)
for (int j = ; j <= m; ++j)
cin >> a[i][j];
dfs(, );
cout << mins << " ";
for (int i = ; i <= mins; ++i)
cout << gg[i] << " \n"[i == mins]; }
上一篇:关于spring的一些注解


下一篇:Selenium with Python 009 - WebDriver API