HDU 1069 Monkey and Banana

来源:互联网 发布:印度 种姓 知乎 编辑:程序博客网 时间:2024/05/18 00:23

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069

解析:就是一种叠罗汉问题,可以用dp,不过这题目数据范围较小,所以可以用floyd最长路算法水过,,

//算法:箱子全部看成点,当a箱子上能放b箱子时,a->b之间连个有向边。那么问题就变成求图中最大的有向边,即floyd来求最长路(dijkstra只能求最短路)#include<stdio.h>#include<string.h>#define Max(x,y) x<y?y:x#define INF 1000000struct point{int x,y,h;}p[110];int n,map[110][110];int main(){int count = 1;while(scanf("%d",&n) != EOF && n != 0){memset(p,0,sizeof(p));memset(map,0,sizeof(map));int a,b,c,cnt = 1;for(int i = 1 ; i <= n; i ++){scanf("%d%d%d",&a,&b,&c);//每个箱子都有三种情况,衍生出三个点p[cnt].x = a;p[cnt].y = b;p[cnt++].h = c;p[cnt].x = b;p[cnt].y = c;p[cnt++].h = a;p[cnt].x = c;p[cnt].y = a;p[cnt++].h = b;}p[0].x = p[0].y = INF;//给定把所有的箱子都看成在地面上,0节点代表地面p[0].h = 0;for(int i = 0 ; i < cnt; i ++)for(int j = 0 ; j < cnt ; j ++){if((p[i].x > p[j].x && p[i].y > p[j].y)||(p[i].x > p[j].y && p[i].y > p[j].x))//如果有边,边权就是上面的箱子的高度map[i][j] = p[j].h ;elsemap[i][j] = -INF;}int cnt2 = 0;for(int k = 0 ; k < cnt ; k++)//floyd算法,注意k是在最外层的循环,不能换循环{for(int i = 0 ; i < cnt ; i ++){for(int j = 0 ;j < cnt ; j ++)if(map[i][j] < map[i][k] + map[k][j])map[i][j] = map[i][k] + map[k][j];}}int max = -INF;for(int i = 0 ; i < cnt; i ++)max = Max(max,map[0][i]);printf("Case %d: maximum height = %d\n",count++,max);}return 0;}


 

原创粉丝点击