UVa Problem 10249 The Grand Dinner (豐盛的晚餐)

來源:互聯網
上載者:User
// The Grand Dinner (豐盛的晚餐)// PC/UVa IDs: 111007/10249, Popularity: C, Success rate: high Level: 4// Verdict: Accepted// Submission Date: 2011-10-08// UVa Run Time: 0.092s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// [Problem Description]// Each team participating 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 contestants, 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 multiple test cases. The first line of each test// case contains 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//// [解題方法]// 此題有兩種解法,一種是簡單的貪進法,一種是較複雜的圖論網路最大流法。//// 貪進法採用如下策略:先將人數按降序排列,從編號為 1 的桌子開始安排座位,安排完後,安排下一隊。若// 能安排完所有人,則輸出 1 和安排方案,若不能安排則輸出 0,其中明顯不能安排的,如某隊人數大於桌子// 數,參賽隊伍數或桌子數為 0 等特殊情況,可以預先處理(雖然可以得到正確答案,但是貪進法的正確性如// 何證明?可以通過網路最大流來證明嗎?)。//// 網路流解法:源點 source 和每支參賽隊伍之間邊的容量為參賽隊伍人數,每支隊伍到桌子之間邊的容量為// 1,每張桌子到匯點 sink 邊的容量為桌子的座位元,然後使用網路流演算法求最大流,如果最大流等於參賽隊// 伍總人數,則滿足條件,輸出方案,否則不滿足條件,輸出 0。可以使用寬度優先遍曆的 Ford-Fullerson// 增廣路方法,又名 Edmonds-Karp 演算法,演算法效率為 O(V*E*E),後續博文會給出。#include <iostream>#include <algorithm>#include <cstring>using namespace std;#define MAXTEAMS 70// 最大參賽隊伍數。#define MAXTABLES 50// 桌子最大數。#define UNSOLVABLE 0#define SOLVABLE 1struct team{int number;// 參賽隊伍編號。int nmembers;// 參賽隊伍人數。};// 比較函數,參數隊伍人數多的排前面。bool cmp(team x, team y){return x.nmembers > y.nmembers;}int main(int ac, char *av[]){team teams[MAXTEAMS];// 各參賽隊編號,人數。int capacity[MAXTABLES];// 桌子的座位元。int nteams, ntables, ntemp;// 隊伍數,桌子數,總人數。bool seat[MAXTABLES][MAXTEAMS];// 記錄座位安排方案。while (cin >> nteams >> ntables, nteams || ntables){// 讀入參賽隊人數並找參賽隊的最大人數。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;}// 讀入桌子座位元量。for (int i = 0; i < ntables; i++)cin >> capacity[i];// 若參賽隊伍數為 0,則直接輸出存在,但是不用輸出具體方案,因為所有桌子無人坐。if (nteams == 0){cout << SOLVABLE << "\n";continue;}// 若桌子數為 0 或者參賽隊伍中某隊人數超過桌子數,則無法安排。if (ntables == 0 || maxMembers > ntables){cout << UNSOLVABLE << "\n";continue;}// 將參賽隊伍按人數多少從大到小排列。sort(teams, teams + nteams, cmp);// 貪進法安排座位,若某隊無法安排,則無安排方案。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;}// 若經過一輪安排後,剩餘人數不為 0 則無法安排。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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.