Topic Link: Click to open the link
The longest road on a DAG,
Test instructions: Gives a box of N, (each infinite), the maximum height required to be able to stack. The edge is large and even small.
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 10000002#define _ll __int64# Define ll long long#define INF 0x3f3f3f3f#define Mod 10000007#define pp pair<int,int> #define ull unsigned long Longu Sing namespace Std;int n,m,dp[110],x[110],y[110],z[110];bool ma[110][110];void input () {m=n*3; for (int i=1;i<=m-2;i+=3) {scanf ("%d%d%d", x+i,y+i,z+i); X[i+1]=y[i];y[i+1]=z[i];z[i+1]=x[i]; X[I+2]=Y[I+1];Y[I+2]=Z[I+1];Z[I+2]=X[I+1]; }}void build () {memset (ma,0,sizeof (MA)); Memset (Dp,0,sizeof (DP)); for (int i=1;i<=m;i++) for (int j=i+1;j<=m;j++) {if ((x[i]>x[j]&&y[i]>y[j)) | | (X[i]>y[j]&&y[i]>x[j])) Ma[i][j]=1; else if ((X[J]>X[I]&&AMp;y[j]>y[i]) | | (X[j]>y[i]&&y[j]>x[i])) Ma[j][i]=1; }}int dfs (int x) {int& ans=dp[x]; if (ans) return ans; ANS=Z[X]; for (int i=1;i<=m;i++) {if (Ma[x][i]) Ans=max (Ans,dfs (i) +z[x]); } return ans; int main () {int cas=1;while (scanf ("%d", &n) &&n) {input (); Build (); int ans=-inf; for (int i=1;i<=m;i++) Ans=max (Ans,dfs (i)); printf ("Case%d:maximum height =%d\n", Cas++,ans);} return 0;}
Uva 437-the Tower of Babylon (DP)