Usaco 2.14 healthy Holsteins (BFS)

Source: Internet
Author: User

John, a farmer, is proud of having the healthiest cows in the world. He knows the minimum amount of vitamins required for each type of feed. Please help the farmer to feed his ox to keep them healthy, so that the number of feeds fed to the ox is minimal.

The minimum amount of vitamins required by the cattle is given, the type of feed required for the cattle is output, and the minimum dose is required.

The amount of vitamins is expressed as an integer. Each type of feed can only be used once for cattle, and the data is resolved.

Output:

The output file contains only one row, including

Minimum feed count required for cattle (P)

There is a p number next to it, indicating the selected feed number (in ascending order ).

If there are multiple solutions, the output feed sequence number is the smallest (that is, the Lexicographic Order is the smallest ).

Analysis: there are a total of 15 types of feed. To find the minimum number that meets the conditions, it is equivalent to a combination that requires different lengths, find the minimum Lexicographic Order that meets the conditions.

The key to using BFS is to record the status. Each status must record several quantities.

Struct Node
{
Int state; // records the path by bit, that is, the type of feed used.
Int ver [26]; // Save the status value,
Short cur; // feed with the largest number currently selected
Short CNT; // number of selected types
};

Each time a status is taken out, the selected feed is added after the current maximum number of the feed, and the selected feed number is recorded. In this way, the first one meets the conditions and the question is asked.

Take the test example as an example:

The entire search process is as follows:

First, press these three statuses into the peer column (1) (2) (3)

After (1) is retrieved)

After extracting (2), the (2, 3)

After removing (3 ......

After (1, 2) is retrieved, (1, 2, 3)

After (1, 3) is retrieved, if the conditions are met, exit;

Then the output is relatively simple;

/* ID: nanke691lang: C ++ task: Holstein */# include <iostream> # include <fstream> # include <string. h >#include <queue >#include <algorithm> using namespace STD; struct node {int state; // records the path by bit, that is, the type of feed used: int ver [26]; // save status value, short cur; // The short CNT with the largest number selected currently; // number of selected types}; int Hol [20] [30]; int V, G, aim [30], Len, ans; queue <node> q; bool compare (int * A) // determine whether the condition is met {for (INT I = 1; I <= V; I ++) if (A [I] <aim [I]) return false; return true;} Vo Id BFS () {node S, F; For (INT I = 1; I <= g; I ++) {for (Int J = 1; j <= V; j ++) s. ver [J] = Hol [I] [J]; S. state = (1 <(I-1); S. cur = I; S. CNT = 1; q. push (s);} Len = 100000000; while (! Q. empty () {f = Q. front (); q. pop (); If (F. CNT <= Len & compare (F. ver) {Len = f. CNT; ans = f. state; break;} For (INT I = f. cur + 1; I <= g; I ++) {for (Int J = 1; j <= V; j ++) s. ver [J] = f. ver [J] + Hol [I] [J]; S. CNT = f. CNT + 1; S. cur = I; S. state = (F. state | (1 <(I-1); // record the currently selected number Q. push (s) ;}}int main () {freopen ("Holstein. in "," r ", stdin); freopen (" Holstein. out "," W ", stdout); scanf (" % d ", & V); For (INT I = 1; I <= V; I ++) scanf ("% d", & AIM [I]); scanf ("% d", & G); (INT I = 1; I <= g; I ++) for (Int J = 1; j <= V; j ++) scanf ("% d ", & Hol [I] [J]); BFS (); cout <Len <''; int flag = 0; For (INT I = 1; I <= g; I ++) {If (ANS & (1 <(I-1 )))! = 0) {If (! Flag) cout <I, flag = 1; else cout <''<I ;}} cout <Endl ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.