UV problem 10249 the grand dinner (full dinner)

Source: Internet
Author: User
Tags dot net
// The grand dinner (full dinner) // PC/Ultraviolet IDs: 111007/10249, popularity: C, success rate: high level: 4 // verdict: accepted // submission date: 2011-10-08 // UV Run Time: 0.092 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // [Problem description] // each team participation in this year's ACM world finals is expected to attend // The Grand Banquet arranged for after the award ceremony. to maximize the amount // of interaction between members of different teams, no two members of the same // team will be allowed to sit at the same table. /// given the number of members on each team (including CO Ntestants, coaches, // reserves, and guests) and the seating capacity of each table, determine whether // it is possible for the teams to sit as described. if such an arrangement is // possible, output one such seating assignment. if there are multiple possible // arrangements, any one is acceptable. /// [input] // The input file may contain in multiple test cases. the first line of each test // case conta INS two integers, 1 ≤ m ≤ 70 and 1 ≤ n ≤ 50, denoting the number of // teams and tables, respectively. the second line of each test case contains M // integers, where the ith integer mi indicates the number of members of Team I. // There are at most 100 members of any team. the third line contains N integers, // Where the jth integer NJ, 2 ≤ NJ ≤ 100, indicates the seating capacity of // table J. //// A test case containing two zeros for M and N terminates the input. // [Output] // for each test case, print a line containing either 1 or 0, denoting whether // there exists a valid seating arrangement of the team members. in case of a // successful arrangement, print M additional lines where the ith line contains // a table number (from 1 to n) for each of the members of Team I. /// [sample input] // 4 5 // 4 5 3 5 // 3 5 2 6 4 // 4 5 // 4 5 3 5 // 3 5 2 6 3 // 0 ////[ sample output] // 1 // 1 2 4 5 // 1 2 3 4 5 // 2 4 5 // 1 2 3 4 5 /// [solution]/ /There are two solutions for this question, one is a simple greedy method and the other is a complicated Maximum Flow Method of graph theory networks. /// The greedy method adopts the following strategy: first arrange the number of people in descending order, arrange the seats from the table numbered 1, and then arrange the next team. If the owner can be arranged, output 1 and arrangement scheme. If no arrangement is made, output 0, which obviously cannot be arranged. For example, if the number of a team is larger than the number of tables, in special cases such as the number of participating teams or the number of tables is 0, you can handle them in advance (although you can get the correct answer, what is the correctness of the greedy method? Can it be proved by the maximum network stream ?). //// Network stream solution: the size of the edge between the source and each participating team is the number of participants, and the size of the edge between each team and the table is // 1, the capacity of the sink side from each table to the sink is the number of seats in the table, and then the network flow algorithm is used to find the maximum flow. If the maximum flow is equal to the total number of queues/queues, the conditions are met and the output scheme is provided, otherwise, the output value is 0. You can use the Ford-fullerson Method for Width-first traversal, also known as the Edmonds-Karp Algorithm. The algorithm efficiency is O (V * E. # Include <iostream> # include <algorithm> # include <cstring> using namespace STD; # define maxteams 70 // maximum number of participating teams. # Define maxtables 50 // maximum number of tables. # Define unsolvable 0 # define solvable 1 struct team {int number; // contestant ID. Int nmembers; // number of participating teams .}; // Comparison function. This parameter is the front of a large number of members. Bool CMP (Team X, Team y) {return X. nmembers> Y. nmembers;} int main (int ac, char * AV []) {team teams [maxteams]; // number and number of members of each queue. Int capacity [maxtables]; // number of seats at the table. Int nteams, ntables, ntemp; // number of teams, number of tables, total number of students. Bool seat [maxtables] [maxteams]; // records the seating arrangement scheme. While (CIN> nteams> ntables, nteams | ntables) {// read the maximum number of members of the Primary Team and find the maximum number of members of the Primary Team. Int maxmembers = 0; For (INT I = 0; I <nteams; I ++) {CIN> ntemp; If (maxmembers <ntemp) maxmembers = ntemp; teams [I]. number = I; teams [I]. nmembers = ntemp;} // number of seats in the table. For (INT I = 0; I <ntables; I ++) CIN> capacity [I]; // if the number of participating teams is 0, the output directly exists, but there is no need to output a specific solution because no one is sitting on all the tables. If (nteams = 0) {cout <solvable <"\ n"; continue;} // if the number of tables is 0 or the number of participating teams exceeds the number of tables, it cannot be arranged. If (ntables = 0 | maxmembers> ntables) {cout <unsolvable <"\ n"; continue ;} // sort the number of participating teams from large to small. Sort (teams, teams + nteams, CMP); // greedy law arranges seats. If a team cannot arrange the seats, no plan is arranged. Bool sitting = true; memset (seat, false, sizeof (seat); For (INT I = 0; I <nteams; I ++) {ntemp = teams [I]. nmembers; For (Int J = 0; j <ntables & ntemp; j ++) if (capacity [J]) {ntemp --; capacity [J] --; seat [J] [teams [I]. number] = true;} // if the number of remaining persons is not 0 after a round of arrangement, it cannot be arranged. If (ntemp) {sitting = false; break ;}} cout <(sitting? Solvable: unsolvable) <"\ n"; if (sitting) {for (INT I = 0; I <nteams; I ++) {int blank = 0; for (Int J = 0; j <ntables; j ++) if (seat [J] [I]) cout <(blank ++? "": "") <(J + 1); 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.