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之本地推送(前台模式与后台模式)

    #import "AppDelegate.h" #import "GlobalDefine.h" @interface AppDelegate () @end ...

  2. java基础知识1

    58.线程的基本概念.线程的基本状态以及状态之间的关系线程指在程序执行过程中,能够执行程序代码的一个执行单位,每个程序至少都有一个线程,也就是程序本身.Java中的线程有四种状态分别是:运行.就绪.挂 ...

  3. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...

  4. Lucene学习总结之六:Lucene打分公式的数学推导

    在进行Lucene的搜索过程解析之前,有必要单独的一张把Lucene score公式的推导,各部分的意义阐述一下.因为Lucene的搜索过程,很重要的一个步骤就是逐步的计算各部分的分数. Lucene ...

  5. 精简jQuery Tabs

    闲来无事,周末用jQuery写了一个比较精简的Tabs,个别地方可以用到. 截图及代码如下: <!DOCTYPE html> <html> <head lang=&quo ...

  6. https://github.com/aptana/studio3/releases aptana

    https://github.com/aptana/studio3/releases  aptana

  7. python运维开发(十六)----Dom&&jQuery

    内容目录: Dom 查找 操作 事件 jQuery 查找 筛选 操作 事件 扩展 Dom 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它 ...

  8. DIRECTORY_SEPARATOR的作用

    IRECTORY_SEPARATOR是php的内部常量,用于显示系统分隔符的命令,不需要任何定义与包含即可直接使用. 在windows下路径分隔符是/(当然/在部分系统上也是可以正常运行的),在lin ...

  9. C语言函数入门

    由于采用了函数模块式的结构,C语言易于实现结构化程序设计.使程序的层次结构清晰,便于程序的编写.阅读.调试. main 函数是主函数,它可以调用其它函数,而不允许被其它函数调用.因此,C程序的执行总是 ...

  10. mysql trigger 权限的说明

    普通用户在创建trigger时会遇到的问题: 1.如果开启了二进制日志,但是用户没有supper 权限:那么他在创建trigger 时会提示设置log_bin_trust_function_creat ...