UVA103 Stacking Boxes

[brief description of title]

Here are \ (k \) boxes of \ (n \) dimensions. Please load up to several boxes layer by layer

[Title Analysis]

The data range of this problem is small. Many people may think of \ (dfs \) or state compression \ (dp \) at first sight. In fact, this problem does not need exponential algorithm.

The first step is obviously to preprocess the nested relationship first. Someone here may search one by one with \ (dfs \), but it is better to use a greedy idea. Let's start with a question:

UVA11292

Yes n Items and m Two boxes, each with a volume a And volume b,Each box has a weight x,Request n Put two items separately m One box (each box can hold only one item) to minimize the total weight of the box.

This problem is an example of "algorithm competition introduction classic - training guide". For a box with large capacity, it is obviously necessary to let it contain a large volume of items so that it will not be wasted, so it can be sorted from small to large according to the volume of the box, from small to large according to the volume of the items, and then corresponding one by one.

In retrospect, the box nesting relationship of this question is actually a special case of the previous question. Then we can also sort each dimension of each box from small to large, so that we can preprocess the nested relationship between each box.

What to do after dealing with the size relationship. The topic requires an output path. Obviously, it needs to be handled in a way that can record the path. Here I use topological sorting because it has simple idea and high efficiency.

The specific methods are as follows:

  1. Enter and sort each dimension of each box from small to large.
  2. Enumerate boxes to handle the nested relationship between two. Connect a directed edge from the small box to the large box, and the penetration of the large box \ (+ \).
  3. Topology sorting calculation \ (dp \) and the next path.
  4. Statistical answers.
  5. Use \ (dfs \) to find the path and output.

See code for implementation details:

#include<bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f3f
using namespace std;
int read(){
	int w=0,h=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')h=-h;ch=getchar();}
	while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
	return w*h;
}
int k,n;
int box[35][15];
struct node{
	int next,to;
}edge[200010];
int head[35],num;
int in[35],dp[35],from[35];//dp[i] indicates the maximum number of boxes that can be nested with I as the outermost layer. from[i] records which box is contained in I when the i-th box is nested most
queue<int>q;
void add(int u,int v){
	edge[++num].next=head[u];
	edge[num].to=v;
	head[u]=num;
}
void topo(){
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=k;i++)
		if(!in[i])q.push(i),dp[i]=1,from[i]=-1;
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].to;
			in[v]--;
//			dp[v]=max(dp[v],dp[u]+1);
			if(dp[u]+1>dp[v]){
				dp[v]=dp[u]+1;
				from[v]=u;
			}
			if(!in[v])q.push(v);
		}
	}
}
void dfs(int u){
	if(from[u]==-1){
		printf("%lld ",u);
		return;
	}
	dfs(from[u]);
	printf("%lld ",u);//Pay attention to iterating first and then outputting. After iterating to the end, output and then return
}
signed main(){
	while(scanf("%lld%lld",&k,&n)!=EOF){
//		k=read();n=read();
		memset(in,0,sizeof(in));
		memset(box,0,sizeof(box));
		memset(from,0,sizeof(from));
		memset(head,0,sizeof(head));//Multiple inputs require initialization
		num=0;
		for(int i=1;i<=k;i++){
			for(int j=1;j<=n;j++)
				box[i][j]=read();
			sort(box[i]+1,box[i]+n+1);//Sort each box from small to large
		}
		for(int i=1;i<=k;i++)
			for(int j=1;j<=k;j++){
				bool flag=1;
				for(int l=1;l<=n;l++)
					if(box[i][l]<=box[j][l])flag=0;
				if(flag){
					add(j,i);//Jianbian
					in[i]++;
//					printf("%lld can put into %lld\n",j,i);
				}
			}
		topo();//Topological sorting
		int ans=0,id;
		for(int i=1;i<=k;i++){
//			ans=max(ans,dp[i]);
			if(dp[i]>ans){
				ans=dp[i];//Statistical answer
				id=i;
			}
		}
		printf("%lld\n",ans);
		dfs(id);puts("");
	}
	return 0;
}

Posted by sam_h on Thu, 04 Nov 2021 04:42:10 -0700