hdu 1069

动态规划的题我弱爆了哎。。。。。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node {
 int x,y,z;
}a[3003];
int cmp(const void *b,const void *c) {
 if((*(struct node *)b).x!=(*(struct node *)c).x)
  return (*(struct node *)b).x<(*(struct node *)c).x?1:-1;
 return (*(struct node *)b).y<(*(struct node *)c).y?1:-1;
}
int main() {
 int i,j,k=0,n,m,f[3001],b,c,d,max;
 while(scanf("%d",&n),n) {
  m=0;
  for(i=0;i<n;i++) {
   scanf("%d%d%d",&b,&c,&d);
   a[m].x=b;
   a[m].y=c;a[m++].z=d;
   a[m].x=b;a[m].y=d;a[m++].z=c;
   a[m].x=c;a[m].y=d;a[m++].z=b;
  }
  for(i=0;i<m;i++)
   if(a[i].x<a[i].y) {
    d=a[i].x;
    a[i].x=a[i].y;
    a[i].y=d;
   }
   qsort(a,m,sizeof(a[0]),cmp);
   memset(f,0,sizeof(f));
   f[0]=a[0].z;
   for(i=1;i<m;i++) {//每次以当前位置为终止位置来求从0开始的最优解
    max=0;
    for(j=i-1;j>=0;j--) {
     if(a[j].x>a[i].x&&a[j].y>a[i].y||a[j].x>a[i].y&&a[j].y>a[i].x) {
      if(f[j]>max)
      max=f[j];
     }
    }
    f[i]=max+a[i].z;
   }
   max=0;
   for(i=0;i<m;i++)
    if(max<f[i])
     max=f[i];
    printf("Case %d: maximum height = %d ",++k,max);
  }
  return 0;
 }
  

原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410968.html