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. iOS-OC-基础-NSNumber常用方法

    /*===========================NSNumber数值对象=========================*/ // 将基本数据类型保存为NSNumber 对象类型 NSNu ...

  2. [转载]一个小例子介绍Obj-C的函数命名方式

    原文链接:http://www.cnblogs.com/liufan9/archive/2013/04/02/2995626.html 对于以前做C#或者JAVA开发的朋友而言,初次接触iOS开发,O ...

  3. (转) 学习C++ -> 向量(vector)

      vector是向量类型,它是一种对象实体,具有值,所以可以看作是变量. 它可以容纳许多其他类型的相同实体,如若干个整数,所以称其为容器.   vector类与一般的Array类的区别在于:   1 ...

  4. EcStore中的App是什么东西?

    Ecstore中的App不是手机上的App,它是一个Web应用包,集合了Web应用的后端PHP程序.Mysql数据表定义,以及前端HTML+CSS+JS展现,通常是实现某个业务功能,如购物车.促销.支 ...

  5. CI框架微信开发-自定义菜单

    在CI框架下面实现了自定义菜单功能.写了一个model,一个类库.顺便附带access_token的实现方式 <?php class Makemenu{ public $menustr; pub ...

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

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

  7. YY语音从4.0版本开始是基于Qt的开发过程,以及碰到的问题

    作者:姚冬链接:http://www.zhihu.com/question/21359230/answer/20127715来源:知乎著作权归作者所有,转载请联系作者获得授权. YY语音从4.0版本开 ...

  8. C# 客户端发送http请求代码 (c/s)

    public class RestClient { private string BaseUri; public RestClient(string baseUri) { this.BaseUri = ...

  9. scheme递归

    主要参考: http://www.shido.info/lisp/scheme7_e.html Function fact that calculates factorials. (define (f ...

  10. 绘图工具graphviz学习使用

    画图工具: http://www.tuicool.com/articles/r2iAfa http://www.tuicool.com/articles/RjQfey 绘图工具graphviz学习使用 ...