(DS "Algorithmic Competition Primer Classic") UVA 11997 K Smallest Sums

Source: Internet
Author: User

The main topic: There are K groups, each array to select a number, composed of k^k number. Select the smallest number of top K in this k^k number


Problem Solving Ideas:

1, if only k array, then the last number of the smallest first k should be obtained from the first two arrays of the smallest number of K and a third array

It is obtained by the rule arithmetic.


2. If there are only 3 numbers per array. Then the first two arrays (A: (A0,A1,A2) B: (both B0,b1,b2,a and B arrays are ordered) have the following result matrix:

A0+b0,a0+b1,a0+b2

A1+b0,a1+b1,a1+b2

A2+b0,a2+b1,a2+b2

In this matrix, the a0+b0 is certainly the smallest. If only 3 minimum numbers are maintained in a queue, the next occurrence of the minimum number is

A0+b1. Analysis is as follows:

By a1+b1>a1+b0, it is not possible to produce the next minimum number in A1+B1 and beyond. This logic indicates where the next possible minimum

It's a0+b1.


3, in the following of this solution, he did not work out k^k results, it is the essence of n+n may produce a minimum number of results


Problem KK Smallest Sums

You ' re given k-arrays, each with a k integers. There is KK ways to pick exactly one element in each array and calculate the sum of the integers. Your task is to find the K smallest sums among them.

Input

There'll be several test cases. The first line of each case is contains an integer k (2<=k<=750). Each of the following K lines contains k positive integers in each array. Each of the these integers does not exceed 1,000,000. The input is terminated by End-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each test case, print the k smallest sums, in ascending order.

Sample Input
31 8 59 2 510 7 621 11 2
Output for the Sample Input
9 10 122 2
Rujia Liu ' s Present 3: A Data Structure Contest celebrating the 100th anniversary of Tsinghua University
Special thanks:yiming Li
Note:please Make sure to test your program with the gift I/O files before submitting!



The code is as follows:

/* * uva_11997.cpp * * Created on:2014 December 30 * author:administrator * * #include <iostream> #include <cstdi o> #include <queue> #include <algorithm>using namespace std;const int maxn = 758;int a[maxn][maxn];struct Item{int S,b;item (int s,int b): s (s), B (b) {}bool operator < (const item& B) Const{return s > b.s;}; void merge (int* a,int* b,int* c,int N) {priority_queue<item> q;int i;for (i = 0; i < n; ++i) {Q.push (Item (a[i] + B [0],0)];} for (i = 0; i < n; ++i) {Item item = Q.top (); Q.pop (); C[i] = item.s;int B = item.b;if (B+1 < N) {Q.push (item (ITEM.S-B[B] + b[b+1],b+1)}}} int main () {int n;while (scanf ("%d", &n) = = 1) {int I;int j;for (i = 0; i < n; ++i) {for (j = 0; J < N; ++j) {scanf ( "%d", &a[i][j]);} Sort (a[i],a[i]+n);} for (i = 1; i < n; ++i) {merge (a[0],a[i],a[0],n);} printf ("%d", a[0][0]); for (i = 1; i < n; ++i) {printf ("%d", A[0][i]);} printf ("\ n");} return 0;}




(DS "Algorithmic Competition Primer Classic") UVA 11997 K Smallest Sums

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.