最小路径覆盖

Dolls
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 19, Accepted users: 16
Problem 12499 : No special judgement
Problem description
Do you remember the box of Matryoshka dolls last week? Adam just got another box of dolls from Matryona. This time, the dolls have different shapes and sizes: some are skinny, some are fat, and some look as though they were attened. Specifically, doll i can be represented by three numbers wi, li, and hi, denoting its width, length, and height. Doll i can fit inside another doll j if and only if wi < wj , li < lj , and hi < hj .
That is, the dolls cannot be rotated when fitting one inside another. Of course, each doll may contain at most one doll right inside it. Your goal is to fit dolls inside each other so that you minimize the number of outermost dolls.
Input
The input consists of multiple test cases. Each test case begins with a line with a single integer N, 1 ≤ N ≤ 500, denoting the number of Matryoshka dolls. Then follow N lines, each with three space-separated integers wi, li, and hi (1 ≤ wi; li; hi ≤ 10,000) denoting the size of the ith doll. Input is followed by a single line with N = 0, which should not be processed.
Output
For each test case, print out a single line with an integer denoting the minimum number of outermost dolls that can be obtained by optimally nesting the given dolls.
Sample Input
3
5 4 8
27 10 10
100 32 523
3
1 2 1
2 1 1
1 1 2
4
1 1 1
2 3 2
3 2 2
4 4 4
0
Sample Output
1
3
2

在构图的时候已经意识到了用最少的路径将所有盒子都包括进去。

而我用了一个拓扑贪心方法,外加正向与逆向临邻接表。

每次贪心嵌套层数最多的盒子,然后把在这条路径上的盒子节点全都取出,此时盒子数++,如此循环。

直到没有盒子为止。

这确实是我写的比较复杂的程序啦= =....差不多将我会的东西全都用上了。可惜RE了....

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;

struct node
{
 	   int x,y,z;
}rec[555];

struct Edge
{
 	   int v,next;
}edge[2222];
int ptr[555],cnt[555],rptr[555];
int top[555],out[555];
int edgenum;

bool judge( node a,node b )
{
 	 if( a.x<b.x && a.y<b.y && a.z<b.z ) return true;
 	 if( a.y<b.x && a.x<b.y && a.z<b.z ) return true;
 	 if( a.x<b.x && a.z<b.y && a.x<b.z ) return true;
 	 if( a.z<b.x && a.x<b.y && a.y<b.z ) return true;
 	 if( a.z<b.x && a.y<b.y && a.x<b.z ) return true;
 	 if( a.y<b.x && a.z<b.y && a.x<b.z ) return true;
 	 return false;
}

void addEdge( int u,int v )
{
 	 edge[edgenum].v=v;
 	 edge[edgenum].next=ptr[u];
 	 ptr[u]=edgenum++;
 	 
 	 edge[edgenum].v=u;
 	 edge[edgenum].next=rptr[v];
 	 rptr[v]=edgenum++;
}


int main()
{
 	int N;
 	while( scanf("%d",&N)!=EOF,N )
 	{
	 	   edgenum=0;
	 	   memset( ptr,-1,sizeof(ptr) );
	 	   memset( rptr,-1,sizeof(rptr) );
	 	   memset( out,0,sizeof(out) );
	 	   for( int i=0;i<N;i++ )
		   	   	scanf( "%d %d %d",&rec[i].x,&rec[i].y,&rec[i].z );
  		   int ans=0;
		   for( int i=0;i<N;i++ )
  		  	    for( int j=0;j<N;j++ )
  		  	    {
			   		 if( judge(rec[i],rec[j]) )
					 	 addEdge( i,j );
   			   }
   		   queue<int> queue;
   		   int outnum=0;
	 	   while( outnum!=N )
	 	   {
		   		  memset( top,0,sizeof(top) );
		   		  memset( cnt,0,sizeof(cnt) );
		   		  while( !queue.empty() ) queue.pop();
		   		  for( int i=0;i<N;i++ )
		   		  {
				   	   if( out[i]==false )
				   	   {
		   		  	   	   for( int j=ptr[i];j!=-1;j=edge[j].next )
		   		  	   	   		if( out[edge[j].v]==false )
		   		  	   				top[edge[j].v]++;
					   }
				  }
		   		  for( int i=0;i<N;i++ )
		   		  	   if( top[i]==0&&out[i]==false )
		   		  	   	   queue.push(i);
		   		  int cur;
		   		  while( !queue.empty() )
		   		  {
				   		 cur=queue.front();
				   		 queue.pop();
				   		 for( int i=ptr[cur];i!=-1;i=edge[i].next )
				   		 {
						  	  if( out[edge[i].v]==false )
						  	  {
				   		 	   	  top[edge[i].v]--;
				   		 	  	  cnt[edge[i].v]=max( cnt[cur]+1,cnt[edge[i].v] );
				   		 	  	  if( top[edge[i].v]==0 )
				   		 	  	  	  queue.push(edge[i].v);
							  }
				         }
   		 		  }
   		 		  out[cur]=true;
  				  outnum++;
   		 		  A:
   		 		  for( int i=rptr[cur];i!=-1;i=edge[i].next )
	   		 	  	   if( cnt[edge[i].v]+1==cnt[cur]&& out[edge[i].v]==false )
	   		 	  	   {
					   	   out[edge[i].v]=true;
 				  		   outnum++;
				  	   	   cur=edge[i].v;
				  	   	   goto A;
				  	   }
				  ans++;
  		   }
   		   printf( "%d\n",ans );
  	}
 	return 0;
}

后来网上搜题解,发现是个二分图的题?

完全不懂为啥.. 网上还说是最小顶点覆盖。

其实今天重温了一下二分图的一些概念,确定是最小路径覆盖了。

而且最小路径覆盖的图不一定是二分图,只是可以用Vnum-Maxmatch这个公式来表达罢了。

扎实学习二分图!

以下是AC的代码:

#include<iostream>
using namespace std;

struct EDGE
{
 	   int v,next;
}E[555555];

struct RECT
{
 	   int x,y,z;
}rect[555];

int Edgenum,ptr[1111];

void addEdge( int u, int v )
{
 	 E[Edgenum].v=v;
 	 E[Edgenum].next=ptr[u];
 	 ptr[u]=Edgenum++;
}

bool judge( RECT a,RECT b )
{
 	 if( a.x<b.x && a.y<b.y && a.z<b.z ) return true;
 	 return false;
}

bool vis[1111];
int match[1111];

bool Match( int cur )
{
 	 for( int i=ptr[cur];i!=-1;i=E[i].next )
 	 {
	  	  if( !vis[E[i].v] )
	  	  {
		   	  vis[E[i].v]=true;
		   	  if( match[E[i].v]==-1 || Match( match[E[i].v] ) )
		   	  {
			   	  match[E[i].v]=cur;
			   	  return true;
			  }
   		  }
	 }
	 return false;
}

int main()
{
 	int N;
 	while( scanf("%d",&N)!=EOF,N )
 	{
	 	   memset( ptr,-1,sizeof(ptr) );
	 	   memset( match,-1,sizeof(match) );
	 	   Edgenum=0;
	 	   for( int i=0;i<N;i++ )
	 	   		scanf( "%d %d %d",&rect[i].x,&rect[i].y,&rect[i].z );
	 	   for( int i=0;i<N;i++ )
	 	   for( int j=0;j<N;j++ )
	 	   		if( judge(rect[i],rect[j]) )
	 	   		{
	 	   			addEdge( i,j );
	 	   			//addEdge( j<<1|1,j<<1 );
				}
		   int ans=0;
	 	   for( int i=0;i<N;i++ )
	 	   {
		   		memset( vis,0,sizeof(vis) );
		   		if( Match(i) )
		   			ans++;
		   }
	 	   printf( "%d\n",N-ans );
  	}
 	return 0;
}



你可能感兴趣的:(struct,Integer,ini,input,each,Shapes)