Topic link Please poke here
Thinking of solving problems
The maximum weight and the path on the DAG with the right.
You can sort the three dimensions of each block first to facilitate subsequent processing.
(Thought from Purple book)
DP[I][J] represents the maximum height at which the first block of wood is the base, and when the J=0/1/2 is high at length/width/height, it can be composed.
Code
#include <stdio.h>#include<string.h>#include<algorithm>#defineN 40using namespacestd;intblo[n][3];intdp[n][3];intN;intDintXinty) { int&ans =Dp[x][y]; intu, v; if(Ans >0)returnans; //y decide who is high if(Y = =0) {ans = blo[x][0]; U = blo[x][1]; v = blo[x][2]; } Else if(Y = =1) {ans = blo[x][1]; U = blo[x][0]; v = blo[x][2]; } Else{ans = blo[x][2]; U = blo[x][0]; v = blo[x][1]; } //because the dimensions are sorted beforehand, you can reduce the judgment for(intj =1; J <= N; J + +) for(intK =0; K <=2; k++) { if(k = =0&& blo[j][1] < U && blo[j][2] <v) Ans= Max (ans, D (j, K) +Blo[x][y]); Else if(k = =1&& blo[j][0] <u && blo[j][2] <v) Ans= Max (ans, D (j, K) +Blo[x][y]); Else if(k = =2&& blo[j][0] <u && blo[j][1] <v) Ans= Max (ans, D (j, K) +Blo[x][y]); } returnans;}intMain () {intt =0; while(SCANF ("%d", &n)! = EOF &&N) {memset (DP,0,sizeof(DP)); //sort three dimensions now for(inti =1; I <= N; i++) {scanf ("%d%d%d", &blo[i][0], &blo[i][1], &blo[i][2]); Sort (Blo[i], Blo[i]+3); } for(inti =1; I <= N; i++) for(intK =0; K <=2; k++) Dp[i][k] =d (i, k); intMAXN =0; for(inti =1; I <= N; i++) for(intK =0; K <=2; k++) MAXN=Max (MAXN, dp[i][k]); printf ("Case %d:maximum height =", ++t); printf ("%d\n", MAXN); } return 0;}
Uva437-the Tower of Babylon