UV problem 10092 the problem with the problem setter)

Source: Internet
Author: User
Tags dot net
// The problem with the problem setter (FAQ) // PC/Ultraviolet IDs: 111008/10092, popularity: C, success rate: average level: 3 // verdict: accepted // submission date: 2011-10-08 // ultraviolet () Run Time: 0.060 S // All Rights Reserved (c) 2011, Qiu. Metaphysis # Yeah dot net /// [Problem description] // so many students are interested in participant ipating in this year's regional // programming contest that we have decided to arrange a screening test to // identify the most promising candidates. this test may include as follows as 100 // problems drawn from as follows as 20 categories. I have been assigned the job of // setting problems for this test. /// First the job seemed to be very easy, since I was told that I wocould be // given a pool of about 1,000 problems divided into appropriate categories. // after getting the problems, however, I discovered that the original authors // often wrote down multiple category-names in the category fields. since no // problem can used in more than one category and the number of problems needed // for each category Is fixed, assigning problems for the test is not so easy. /// [input] // The input file may contain in multiple test cases, each of which begins with a // line containing two integers, NK and NP, where NK is the number of categories // and NP is the number of problems in the pool. there will be between 2 and 20 // categories and at most 1,000 problems in the pool. /// the second line contains NK positiv E integers, where the ith integer specifies // The number of problems to be encoded in category I (1 ≤ I ≤ nk) of the test. // You may assume that the sum of these NK integers will never exceed 100. the // jth (1 ≤ j ≤ np) of the next NP lines contains the category information of // the jth problem in the pool. each such problem category specification starts // with a positive integer specifying Number of categories in which this // problem can be specified ded, followed by the actual Category numbers. /// a test case containing two zeros for nk and NP terminates the input. /// [Output] // for each test case, print a line reporting whether problems can be successfully // selected from the pool under the given restrictions, with 1 for success and // 0 for failure. /// in case of successful selecti On, print NK additional lines where the ith line // contains the problem numbers that can be sorted in Category I. problem // numbers are positive integers not greater then NP and each two problem numbers // must be separated by a single space. any successful selection will be accepted. /// [sample input] // 3 15 // 3 3 4 // 2 1 2 // 1 3 // 1 3 // 1 3 // 1 3 // 1 3 // 1 3 // 1 3 // 3 1 2 3 // 2 2 3 // 2 1 3 // 1 2 // 1 2 // 1 2 // 2 1 2 // 2 1 3/2 1 2/1 1/3 1 2 3/3 15/7 3 4/2 1 2/1 1/1 2 // 1 2 // 1 3 // 3 1 2 3 // 2 2 3 // 2 3 // 2 2 3 // 1 2 // 1 2 // 1 2 // 2 2 2 3 // 2 2 3 // 2 1 2 // 1 1 // 3 1 2 3 // 0 0 /// [sample output] // 1 // 8 11 12 // 1 6 7 // 2 3 4 5 // 0 /// [problem solving method] // This question is similar to that in the format, it can be modeled as the biggest flow problem. Set the source point source. The Arc capacity between the source point and each question is 1, and the arc capacity between each question and each category is 1, the arc capacity between the question category and sink is the number of questions to be // evaluated for this category, and then whether the maximum flow of this graph can meet the question requirements. # Include <iostream> # include <cstring> # include <queue> using namespace STD; # define maxcategory 21 # define maxproblem 1001 # define maxv 1030 // maximum number of vertices. # Define unsolvable 0 // no arrangement scheme. # Define solvable 1 // there is a plan. # Define dummy (-1) // indicates that the vertex has no parent vertex. Struct edge {int vertex; // connected vertex. Int capacity; // capacity. Int flow; // traffic. Int residual; // residual traffic .}; Edge edges [maxv] [maxv]; // edge of the directed graph. Int degree [maxv]; // The degree of vertices in the directed graph. Int parents [maxv]; // traversal mark, the parent vertex of the current vertex. Bool discovered [maxv]; // indicates whether the traversal mark is found. // Use the width-first traversal to find a path with the positive traffic from the source point to the sink point. Any increasing path from the source to the sink can increase the total traffic, because // This can be used to traverse the width first. It should be noted that the total traffic can only be increased along the "can also be extended" (that is, the residual capacity is a positive number) therefore, it is necessary to determine whether the residual capacity is positive during the // traversal process, so as to help the width first traverse the separated saturated and unsaturated sides. Void breadthfirstsearch (INT source, int sink) {queue <int> vertices; vertices. Push (source); discovered [Source] = true; while (! Vertices. empty () {int v = vertices. front (); vertices. pop (); For (INT I = 0; I <degree [v]; I ++) // check whether the edge is saturated. If (edges [v] [I]. residual> 0) {If (discovered [edges [v] [I]. vertex] = false) {vertices. push (edges [v] [I]. vertex); discovered [edges [v] [I]. vertex] = true; parents [edges [v] [I]. vertex] = V;} If (edges [v] [I]. vertex = sink) return ;}}// locate the directed edge between vertex x and vertex y. Edge * findedge (int x, int y) {for (INT I = 0; I <degree [X]; I ++) if (edges [x] [I]. vertex = y) Return & edges [x] [I];} // augmented. Handle forward and reverse arcs. Void augmentpath (INT source, int sink, int volume) {If (Source = sink) return; edge * E = findedge (parents [sink], sink ); e-> flow + = volume; e-> residual-= volume; E = findedge (sink, parents [sink]); e-> residual + = volume; augmentpath (source, parents [sink], volume);} // The capacity of the compute path from sink to source based on BFs results. The augmented process converts as much residual traffic as possible to positive traffic. The capacity of the zengguang road is equal to the minimum residual capacity of the entire road, just as the speed of the traffic flow depends on the most crowded road section. Int pathvolume (INT source, int sink) {If (parents [sink] = dummy) return 0; edge * E = findedge (parents [sink], sink ); if (Source = parents [sink]) Return (e-> residual); elsereturn (min (pathvolume (source, parents [sink]), E-> residual ));} // initialize the search variable. Void initializesearch () {memset (discovered, false, sizeof (discovered); memset (parents, dummy, sizeof (parents);} // network stream solution. Find a path from the source to the sink that can increase the total traffic and use it to increase the total traffic. When no augmented path exists, the final stream of the algorithm is the maximum stream. Note that each directed edge e = (I, j) must be split into two arcs (I, j) and (J, I). // (I, j) the initial residual capacity is the capacity of E, the residual capacity of (J, I) is 0, and the initial flow of all arcs is set to 0. // In fact, any feasible stream can be used as the initial stream of the algorithm. Quick Construction of feasible flows close to the maximum stream can greatly improve the algorithm efficiency. Bool NetFlow (INT source, int sink, int ntotal) {int maxflow = 0, volume; initializesearch (); breadthfirstsearch (source, sink); volume = pathvolume (source, sink ); while (volume) {maxflow + = volume; augmentpath (source, sink, volume); initializesearch (); breadthfirstsearch (source, sink); volume = pathvolume (source, sink );} return maxflow = ntotal;} int main (int ac, char * AV []) {int ncategories, nproblems; int ntot Al, ncount, ntype; int category [maxcategory]; // number of questions of each category. Int problem [maxcategory]; // number of existing questions of each type. Int source, sink; bool solvable; while (CIN> ncategories> nproblems, ncategories | nproblems) {source = ntotal = 0; sink = ncategories + nproblems + 1; memset (degree, 0, sizeof (degree); memset (problem, 0, sizeof (problem); For (INT I = 1; I <= ncategories; I ++) {CIN> category [I]; ntotal + = category [I]; // Source Vertex to category. Edges [Source] [degree [Source] ++] = (edge) {I, category [I], 0, category [I]}; edges [I] [degree [I] ++] = (edge) {source, category [I], 0, 0 };}for (INT I = ncategories + 1; I <= (ncategories + nproblems); I ++) {CIN> ncount; For (Int J = 1; j <= ncount; j ++) {CIN> ntype; problem [ntype] ++; // category to question. Edges [ntype] [degree [ntype] ++] = (edge) {I, 1, 0, 1 }; edges [I] [degree [I] ++] = (edge) {ntype, 1, 0, 0} ;}// the question is sent to the sink. Edges [I] [degree [I] ++] = (edge) {sink, 1, 0, 1 }; edges [sink] [degree [sink] ++] = (edge) {I, 1, 0, 0};} If (ncategories = 0) {cout <solvable <"\ n"; continue;} If (nproblems = 0) {cout <unsolvable <"\ n"; continue ;} // if the number of questions of a type is less than the required total number, it cannot be solved. Solvable = true; For (INT I = 1; I <= ncategories; I ++) if (problem [I] <category [I]) {solvable = false; break ;} if (solvable = false) {cout <unsolvable <"\ n"; continue;} solvable = NetFlow (source, sink, ntotal); cout <(solvable? Solvable: unsolvable) <"\ n"; if (! Solvable) continue; For (INT I = 1; I <= ncategories; I ++) {int blank = 0; For (Int J = 0; j <degree [I]; j ++) {If (edges [I] [J]. residual = 0) {cout <(blank ++? "": ""); Cout <(edges [I] [J]. vertex-ncategories) ;}} cout <"\ n" ;}} return 0 ;}

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.