http://poj.org/problem?id=2442
题意:给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列,
让你求出序列和最小的前n个序列的序列和。
1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 const int N=2002; 8 9 int main() 10 { 11 int T,m,n,arr1[N],arr2[N]; 12 priority_queue<int,vector<int>,less<int> >q; 13 scanf("%d",&T); 14 while(T--) 15 { 16 scanf("%d%d",&m,&n); 17 for (int i = 0; i < n; i ++) 18 { 19 scanf("%d",&arr1[i]); 20 } 21 sort(arr1,arr1+n); 22 for (int i = 1; i < m; i ++) 23 { 24 for (int j = 0; j < n; j ++) 25 { 26 scanf("%d",&arr2[j]); 27 q.push(arr1[0]+arr2[j]); 28 } 29 sort(arr2,arr2+n); 30 for (int k = 1; k < n; k ++) 31 { 32 for (int p = 0; p < n; p ++) 33 { 34 if (arr1[k]+arr2[p] > q.top()) 35 break; 36 q.pop(); 37 q.push(arr1[k]+arr2[p]); 38 } 39 } 40 for (int k = 0; k < n; k ++) 41 { 42 arr1[n-1-k] = q.top(); 43 q.pop(); 44 } 45 } 46 for (int i = 0; i < n; i ++) 47 { 48 if (i==n-1) 49 printf("%d ",arr1[i]); 50 else 51 printf("%d ",arr1[i]); 52 } 53 } 54 return 0; 55 }