列表

详情


NC24153. [USACO 2016 Dec P]Robotic Cow Herd

描述

Bessie is hoping to fool Farmer John by building a herd of K realistic robotic cows (1≤K≤100,000).
It turns out that building a robotic cow is somewhat complicated. There are N (1≤n≤100,000) individual locations on the robot into which microcontrollers must be connected (so a single microcontroller must be connected at each location). For each of these locations, Bessie can select from a number of different models of microcontroller, each varying in cost.

For the herd of robotic cows to look convincing to Farmer John, no two robots should behave identically. Therefore, no two robots should have exactly the same set of microcontrollers. For any pair of robots, there should be at least one location at which the two robots use a different microcontroller model. It is guaranteed that there will always be enough different microcontroller models to satisfy this constraint.

Bessie wants to make her robotic herd as cheaply as possible. Help her determine the minimum possible cost to do this!

输入描述

The first line of input contains N and K separated by a space.
The following N lines contain a description of the different microcontroller models available for each location. The ith such line starts with Mi (1≤Mi≤10), giving the number of models available for location i. This is followed by Mi space separated integers Pi,j giving the costs of these different models (1≤Pi,j≤100,000,000).

输出描述

Output a single line, giving the minimum cost to construct K robots.

示例1

输入:

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

输出:

61

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 179ms, 内存消耗: 15580K, 提交时间: 2021-06-30 20:12:49

#include<bits/stdc++.h>
#define LL long long
#define pb push_back
using namespace std;
const int N=1e5+5;
struct node{
	LL sum;int x,y;
	bool friend operator<(node a,node b){return a.sum>b.sum;} 
};
int n,k,c,id[N],val[N];LL res,ans;
priority_queue<node>q;
vector<int>vec[N];
bool cmp(int x,int y){return val[x]<val[y];}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1,x,y;i<=n;i++){
		scanf("%d",&x);
		if(x==1){scanf("%d",&y);res+=y;continue;}
		c++;id[c]=c;
		for(int j=1;j<=x;j++)scanf("%d",&y),vec[c].pb(y);
		sort(vec[c].begin(),vec[c].end());
		res+=(LL)vec[c][0];val[c]=vec[c][1]-vec[c][0];
	}
	ans=res;k--;n=c;
	sort(id+1,id+n+1,cmp);
	q.push((node){res+val[id[1]],1,1});
	for(int i=1;i<=k;i++){
		node u=q.top();q.pop();ans+=u.sum;
		if(u.y<vec[id[u.x]].size()-1)q.push((node){u.sum-vec[id[u.x]][u.y]+vec[id[u.x]][u.y+1],u.x,u.y+1});
		if(u.x<n)q.push((node){u.sum+val[id[u.x+1]],u.x+1,1});
		if(u.x<n&&u.y==1)q.push((node){u.sum-val[id[u.x]]+val[id[u.x+1]],u.x+1,1});
	}
	printf("%lld\n",ans);
	return 0;
}

上一题