UVa 103
The main topic: given n boxes, each box has m dimensions,
One box can be nested in another box if and only if all dimension dimensions of the box are all smaller than the corresponding dimensions of the other box,
(Note that the box can be rotated, that is, the dimensions of the box can be interchanged), and the maximum number of boxes can be set.
The first line is entered as n,m, followed by the N-line m-dimensional box
Problem-solving ideas: The nesting relationship is a two-yuan relationship, so the problem is to do dynamic planning on the DAG,
But the two-dimensional judgment is changed to n-dimensional, the other unchanged.
Detailed examination: Nested rectangle of dynamic programming on DAG (PS: This problem can be understood as nested M-side shape)
/*UVa 103 Stacking Boxes Dynamic planning on a dag---*/#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;intN, M;//n is the node number, M is the dimension (n <=, M <=)intg[ *][ *];//DAG, G[i][j] 1 means box I can be nested in box Jintvec[ *][ the];intdp[ *];//determine if x is less than yBOOLIs_small (intXinty) { for(inti =1; I <= m; ++i) { //there is one greater than or equal to return 0 if(Vec[x][i] >=Vec[y][i]) { return 0; } }//For (i) return 1;}voidCMAX (int&x,inty) { if(Y >x) {x=y; }}intDP (inti) { int&ans =Dp[i]; if(Ans >0){ //memory Search to avoid duplication of calculations returnans; } ans=1; for(intj =1; J <= N; ++K) { if(G[i][j]) {//Recursive SolutionCMAX (ans, DP (j) +1); } }//For (j) returnans;}//Output SequencevoidPrint_ans (inti) { if(Dp[i] = =1){ //one last node.printf"%d", i); } Else{ for(intj =1; J <= N; ++j) { if(G[i][j] && Dp[j] +1==Dp[i]) {printf ("%d", i); Print_ans (j); Break; } }//For (j) }}intMain () {#ifdef _local freopen ("D:\\input.txt","R", stdin);#endif while(SCANF ("%d%d", &n, &m) = =2){ for(inti =1; I <= N; ++i) { for(intj =1; J <= M; ++j) {scanf ("%d", Vec[i] +j); }//For (j)Sort (Vec[i] +1, Vec[i] + M +1); }//For (i)//Build a DAGmemset (G,0,sizeofG); for(inti =1; I <= N; ++i) { for(intj =1; J <= N; ++j) { //G[i][j] 1 means box I can be nested in box J if(Is_small (i, J)) {G[i][j]=1; } }//For (j)}//For (i)//find the longest path intAns =0; intBest ; Memset (DP,0,sizeofDP); for(inti =1; I <= N; ++i) { if(DP (i) >ans) {ans=Dp[i]; Best=i; } }//For (i)printf"%d\n", ans); Print_ans (best); printf ("\ n"); }//While (scanf) return 0;}
View Code
UVa 103 Stacking Boxes Dynamic planning on a dag---