poj 2442 Sequence

来源:互联网 发布:2016淘宝买家可以贷款 编辑:程序博客网 时间:2024/06/05 00:49
Sequence
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 9222 Accepted: 3074

Description

Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's clear that we may get n ^ m this kind of sequences.
Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What we need is the smallest n sums. Could you help us?

Input

The first line is an integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains two integers m, n (0 < m <= 100, 0 < n <= 2000). 
The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.

Output

For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.

Sample Input

12 31 2 32 2 3

Sample Output

3 3 4


对读入的第一个序列建堆,接下来的每个序列按升序排列,序列中第一个数加上堆中每一个数后加入另一个堆,对该行的第2个到第n个数每个数,

和堆里面每个数相加,如果比优先队列最大的元素小,那么弹出最大元素,压入该元素,比最大元素大,则break,跳到该行的下一个数。

const  maxn=2000;type  arr=array[0..maxn] of longint;var  a,b,c:arr;  i,j,k,l,t,n,m:longint;procedure qsort(var x:arr;l,r:longint);var  i,j,k:longint;begin  if l>=r then exit;  i:=l;j:=r;  k:=x[(l+r) div 2];  repeat    while x[i]<k do inc(i);    while x[j]>k do dec(j);    if i<=j then      begin        x[0]:=x[i];x[i]:=x[j];x[j]:=x[0];        inc(i);        dec(j);      end;  until i>j;  qsort(x,i,r);qsort(x,l,j);end;procedure up(i:longint);var  j:longint;begin  if i=1 then exit;  j:=i div 2;  repeat    if c[i]>c[j] then      begin        c[0]:=c[i];        c[i]:=c[j];        c[j]:=c[0];      end    else break;    i:=j;    j:=j div 2;  until i=0;end;procedure down(i:longint);var  done:boolean;begin  done:=false;  repeat    i:=2*i;    if (i+1<=m) and (c[i]<c[i+1]) then inc(i);    if c[i div 2]<c[i] then      begin        c[0]:=c[i];        c[i]:=c[i div 2];        c[i div 2]:=c[0];      end    else done:=true;  until (2*i>m) or done;end;procedure inst(x,i:longint);begin  c[i]:=x;  up(i);end;procedure instx(x,i:longint);begin  c[i]:=x;  down(i);end;begin  readln(t);  for l:=1 to t do    begin      readln(n,m);      for i:=1 to m do        read(a[i]);      readln;      qsort(a,1,m);      for i:=1 to n-1 do        begin          for j:=1 to m do            read(b[j]);          readln;          qsort(b,1,m);          for j:=1 to m do            inst(a[j]+b[1],j);          for j:=2 to m do            for k:=1 to m do              if b[j]+a[k]<c[1] then                instx(b[j]+a[k],1)              else break;          a:=c;          qsort(a,1,m);          fillchar(c,sizeof(c),0);        end;      for i:=1 to m do        write(a[i],' ');      writeln;    end;end.
0 0
原创粉丝点击