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. POJ 1723 SOLDIERS (中位数)

    题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...

  2. 6、Khala的登录生命周期管理

    khala能够对设备进行生命周期管理,并提供了与生命周期相关的接口,用户只需在具体的设备类型实现类中重写这些生命周期接口,即可享受khala对于生命周期管理的同时定制与业务相关的操作.具体接口解释如下 ...

  3. JQuery validator扩展

    //validator 扩展 jQuery.validator.addMethod("mail", function(value, element, messages) { ret ...

  4. windows下配置Nginx+Mysql+Php7

    环境:Windows10 mysql-5.6.24-win32解压缩版    nginx-1.8.0    php7 1.Mysql安装 下载压缩文件之后解压缩至相应目录(我的目录是G:\wnmp\m ...

  5. 在eclipse中首次新建项目的时候,出现Project interpreter not specified

    在eclipse中首次新建项目的时候,出现Project interpreter not specified(大致可以理解为:没有执行的项目编译者) 原因就是安装了pydev之后,我们的python还 ...

  6. StreamReader与StreamWriter

    StreamReader实现了抽象基类TextReader类,而StreamWriter实现了抽象基类TextWriter.分别用于对流的读取与写入. 先从StreamReader说起 一.构造方法 ...

  7. Windows 7下可以使用的各个命令语句+C#打开

    Windows 7下可以使用的各个命令语句:   control.exe /name microsoft.folderoptions 启动资源管理器的 文件夹属性 选项卡 control.exe /n ...

  8. 【Xamarin挖墙脚系列:IOS现有的设备SDK /OS/硬件一览】

    附件下载: http://pan.baidu.com/s/1o7rsrUE

  9. 数据可视化的优秀入门书籍有哪些,D3.js 学习资源汇总

    习·D3.js 学习资源汇总 除了D3.js自身以外,许多可视化工具包都是基于D3开发的,所以对D3的学习就显得很重要了,当然如果已经有了Javascript的经验,学起来也会不费力些. Github ...

  10. XMPP通讯开发-1

    有关XMPP的相关知识这里就不讲解了,网上有很多,这里我使用的NetBeans+Openire+smack搭建一个以XMPP协议的通讯工具,对于这部分知识我也不是很了解,也是初识吧,可能有些概念会混淆 ...