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

1
2 3
1 2 3
2 2 3

Sample Output

3 3 4

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int a[][],b[];
priority_queue<int>p;
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n ,m;
scanf("%d%d",&m,&n); //m个序列,每个含n个数字
for(int i=;i<m;i++){
for(int j=;j<n;j++)scanf("%d",&a[i][j]);
sort(a[i],a[i]+n); //每个序列输入完后先排序
}
for(int i=;i<n;i++)
p.push(a[][i]);
for(int i=;i<m;i++){
for(int j=;j<n;j++){
b[j]=p.top();
p.pop();
}
for(int j=;j<n;j++){
for(int t=n-;t>=;t--){
if(j==)p.push(a[i][]+b[t]); //将第i个序列的第一项与加之i-1序列的前n小项相加
else{
if(p.top()>b[t]+a[i][j]){
p.pop();
p.push(a[i][j]+b[t]);
}
else break;
}
}
}
}
for(int i=;i<n;i++){
b[i]=p.top();
p.pop();
}
for(int i=n-;i>=;i--){
if(i==)printf("%d\n",b[i]);
else printf("%d ",b[i]);
}
}
//system("pause");
return ;
}

U - stl 的 优先队列 Ⅰ的更多相关文章

  1. STL之优先队列

    STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...

  2. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  3. STL中优先队列的使用

    普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...

  4. STL priority_queue 优先队列 小记

    今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...

  5. STL之优先队列(1)

    优先队列用法 在优先队列中,优先级高的元素先出队列. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 优先队列的第一种用法: 也是最常用的用法 priority_queue< ...

  6. STL之优先队列(priority_queue)

    转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...

  7. W - stl 的 优先队列 Ⅲ

    Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...

  8. V - stl 的 优先队列 Ⅱ

    Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...

  9. hdu 4393 Throw nails(STL之优先队列)

    Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...

  10. hdu1716排列2(stl:next_permutation+优先队列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. int.TryParse 与 int.Parse 的区别

    int.TryParse 与 int.Parse 的区别是,int.TryParse不会产生异常,转换成功返回 true,转换失败返回 false.最后一个参数为输出值,如果转换失败,输出值为 0. ...

  2. java中的native关键字

    参照下面的链接http://blog.163.com/yueyemaitian@126/blog/static/21475796200701491621267/

  3. JavaScript权威指南阅读笔记3

    第六章 对象 1.首先是先介绍了对象直接量的格式:对象直接量就是1.由若干个名/值对组成的映射表,2名/值对中间由冒号分割,3名值对之间由逗号分割,4整个映射表由花括号括起来.这样就组成了一个对象直接 ...

  4. SqlServer排序(null值,和非空值排列顺序)

    项目中遇到一个问题,需要设置序号排序,而该字段中的默认值为空,使用普通排序,空值就会在最前边.可以使用如下语句:   其中 col 为 排序的字段名称. then 0 else 1 代表先空值,后数字 ...

  5. Backward Digit Sums(POJ 3187)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5495   Accepted: 31 ...

  6. 一个简单的网页读字符串 SpeechLib

    //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib //引用组件:Interop.SpeechLib.dll//导入空间:SpeechLib 前面设置内容引用别人 ...

  7. 手把手教你学习FPGA系列视频教程_救护车鸣笛声

    本套教程主要面对FPGA初学者,本次DIY活动不仅让初学者掌握FPGA硬件电路设计以及焊接方面的知识,更重要的是让初学者学习硬件描述语言 (VerilogHDL)描述数字电路,以及Quartus II ...

  8. usb mass storage之旅

    前面总结了usb hid keyboard,现在总结usb mass storage,在枚举阶段没什么好总结的,hid和mass storage差不多,都是同样的枚举过程,但是在他们的配置描述符.接口 ...

  9. python 递归展开嵌套的序列(生成器用法)

    任何使用yield语句的函数都称为生成器.调用生成器函数将创建一个对象,该对象通过连续调用next()方法(在python3中是__next__())生成结果序列. next()调用使生成器函数一直运 ...

  10. Linux系统编程(17)——正则表达式进阶

    C的变量和Shell脚本变量的定义和使用方法很不相同,表达能力也不相同,C的变量有各种类型,而Shell脚本变量都是字符串.同样道理,各种工具和编程语言所使用的正则表达式规范的语法并不相同,表达能力也 ...