Assembly Required

题目描述

Princess Lucy broke her old reading lamp, and needs a new one. The castle orders a shipment of parts from the Slick Lamp Parts Company, which produces interchangable lamp pieces.
There are m types of lamp pieces, and the shipment contained multiple pieces of each type. Making a lamp requires exactly one piece of each type. The princess likes each piece with some value, and she likes a lamp as much as the sum of how much she likes each of the pieces.
You are part of the castle staff, which has gotten fed up with the princess lately. The staff needs to propose k distinct lamp combinations to the princess (two lamp combinations are considered distinct if they differ in at least one piece). They decide to propose the k combinations she will like the least. How much will the princess like the k combinations that the staff proposes?

输入

The first line of input contains a single integer T (1 ≤ T ≤ 10), the number of test cases. The first line of each test case contains two integers m (1 ≤ m ≤ 100), the number of lamp piece types and k (1 ≤ k ≤ 100), the number of lamps combinations to propose. The next m lines each describe the lamp parts of a type;
they begin with ni (2 ≤ ni ≤ 100), the number of pieces of this type, followed by ni integers vi,1 ,... , vi,ni(1 ≤ vi,j ≤ 10,000) which represent how much the princess likes each piece. It is guaranteed that k is no greater than the product of all ni ’s.

输出

For each test case, output a single line containing k integers that represent how much the princess will like the proposed lamp combinations, in nondecreasing order.

样例输入

2
2 2
2 1 2
2 1 3
3 10
4 1 5 3 10
3 2 3 3
5 1 3 4 6 6

样例输出

2 3
4 5 5 6 6 7 7 7 7 7

提示

In the first case, there are four lamp pieces, two of each type. The worst possible lamp has value 1 + 1 = 2,
while the second worst possible lamp has value 2 + 1 = 3.

题意:

一共有n行,每行选一个,组合出k种最小的价值。从第一行开始,枚举出所有的情况,这样肯定会T,所以我们可以只维护前K个最小的,每枚举完一行,选出前K个小的和下一行去计算可能出现的组合情况,这样最后一行枚举完后数组存的就是最优的情况

AC代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<set>
#include<cmath>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define sca(x) scanf("%d", &x)
#define scas(x) scanf("%s",x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pris(x) printf("%s\n",x)
#define prl(x) printf("%lld\n",x)
//#include <bits/stdc++.h>

typedef long long ll;
const int maxn=1e6+7;
const int mod=1e9+7;
const double eps=1e-8;
// const double pi = acos(-1);

using namespace std;

int val[maxn];
int res[maxn];
int vis[105];
int m[105];

int main()
{
    int t;
    sca(t);
    while(t--)
    {
      int n,k;
      sca2(n,k);
      int len = 1;
      memset(res,0);
      rep(i,0,n)
      {
        rep(i,0,len)
        {
          val[i] = res[i];
        }
        sca(m[i]);
        int pp = 0;
        rep(j,0,m[i])
        {
          int temp;
          sca(temp);
          rep(p,0,len)
          {

            res[pp++] = val[p] + temp;
          }
        }
        sort(res,res+pp);
        // rep(x,0,pp)
        // {
        //   printf("%d%c",res[x],x == pp-1?'\n':' ');
        // }
        len = pp < k ? pp : k;
        //pri(len);
      }
      rep(i,0,k)
      {
        printf("%d%c",res[i],i == k-1?'\n':' ');
      }


      
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Prince_NYing/article/details/89194775