洛谷 P1460 Healthy Holsteins DFS

P1460 Healthy Holsteins

Insert picture description here

code

/*Siberian Squirrel*/
/*Cute JinFish*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1), eps = 1e-8;
/*const int MOD = 998244353, r = 119, k = 23, g = 3;
const int MOD = 1004535809, r = 479, k = 21, g = 3;*/
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int M = 3e2 + 10, N = 5e6 + 10;
int sgn(double x) {
    
    
    if(fabs(x) < eps) return 0;
    return x < 0? -1: 1;
}
//inline int rnd(){static int seed=2333;return seed=(((seed*666666ll+20050818)%998244353)^1000000007)%1004535809;}
//double Rand() {return (double)rand() / RAND_MAX;}

int n, k, v[M], t[M], last[M];
int a[M][M], up = M;

ll quick_pow(ll ans, ll p, ll res = 1) {
    
    
    for(; p; p >>= 1, ans = ans * ans % MOD)
        if(p & 1) res = res * ans % MOD;
    return res % MOD;
}

void init() {
    
    }

bool check(int x) {
    
    
    for(int i = 1; i <= n; ++ i) {
    
    
        int ans = 0;
        for(int j = 1; j <= x; ++ j) {
    
    
            ans += a[t[j]][i];
        }
        if(ans < v[i]) return false;
    }
    return true;
}

void dfs(int pos, int m) {
    
    
    if(pos > k) {
    
    
        if(check(m) && m < up) {
    
    
            up = m;
            for(int i = 1; i <= up; ++ i) {
    
    
                last[i] = t[i];
            }
        }
        return;
    }
    t[m + 1] = pos;
    dfs(pos + 1, m + 1);
    dfs(pos + 1, m);
}

void solve(ll res = 0) {
    
    
    dfs(1, 0);
    cout << up << ' ';
    for(int i = 1; i <= up; ++ i) {
    
    
        cout << last[i] << ' ';
    }
    cout << endl;
}


int main() {
    
    
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(nullptr);
// srand(time(0));
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    init();
    int o = 1;
//	cin >> o;
    while(o --) {
    
    
        cin >> n;
        for(int i = 1; i <= n; ++ i) cin >> v[i];
        cin >> k;
        for(int i = 1; i <= k; ++ i) {
    
    
            for(int j = 1; j <= n; ++ j)
            cin >> a[i][j];
        }
        solve();
    }
    return 0;
}

Title description

Farmer John takes pride in having the healthiest cows in the world. He knows what is the minimum amount of vitamins required for cattle contained in each feed. Please help the farmer to feed his cattle to keep them healthy and to minimize the number of feeds for the cattle.

Give the minimum amount of vitamins required by the cow, output what kind of feed is needed to feed the cow, and the minimum amount of feed required.

The amount of vitamins is expressed in integers, and each feed can only be used for cattle at most once, and the data guarantees that there is a solution.
Input format

An integer vvv in the first line indicates the number of vitamins needed.
The vvv integers in the second line represent the minimum amount of each vitamin that the cow needs every day.

In the third line, an integer ggg indicates the number of feeds that can be used to feed cattle.
In the ggg line below, the nnn line indicates the amount of various vitamins contained in the feed numbered nnn.
Output format

The output file has only one line, including ppp, which is the minimum number of feeds necessary for cattle; the number of ppps is followed by the number of feeds selected (arranged in ascending order).

If there are multiple solutions, output the feed with the smallest serial number (that is, the smallest lexicographical order).

Guess you like

Origin blog.csdn.net/qq_46173805/article/details/115251244