http://poj.org/problem?id=2442
题意:给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列,
让你求出序列和最小的前n个序列的序列和。
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N=; int main()
{
int T,m,n,arr1[N],arr2[N];
priority_queue<int,vector<int>,less<int> >q;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for (int i = ; i < n; i ++)
{
scanf("%d",&arr1[i]);
}
sort(arr1,arr1+n);
for (int i = ; i < m; i ++)
{
for (int j = ; j < n; j ++)
{
scanf("%d",&arr2[j]);
q.push(arr1[]+arr2[j]);
}
sort(arr2,arr2+n);
for (int k = ; k < n; k ++)
{
for (int p = ; p < n; p ++)
{
if (arr1[k]+arr2[p] > q.top())
break;
q.pop();
q.push(arr1[k]+arr2[p]);
}
}
for (int k = ; k < n; k ++)
{
arr1[n--k] = q.top();
q.pop();
}
}
for (int i = ; i < n; i ++)
{
if (i==n-)
printf("%d\n",arr1[i]);
else
printf("%d ",arr1[i]);
}
}
return ;
}