UVa 103 Stacking Boxes --- DAG上的动态规划

  

  UVa 103

  题目大意:给定n个箱子,每个箱子有m个维度,

       一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度,

       (注意箱子可以旋转,即箱子维度可以互换),求最多能套几个箱子。

       第一行输入为n,m,之后是n行m维的箱子

  解题思路:嵌套关系是二元关系,因此这题即在DAG上做动态规划,

       只不过将二维的判断改成了n维,其他不变。

       详细看考:DAG上的动态规划之嵌套矩形  (ps:这题可以理解成嵌套m边形)

/* UVa 103 Stacking Boxes --- DAG上的动态规划 */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m;    //n为结点数,m为维度(n <= 30, m <= 10)
int G[35][35];    //DAG, G[i][j]为1表示 盒子i可以嵌套在盒子j中
int vec[35][15];
int dp[35];

//判断x是否小于y
bool is_small(int x, int y){
    for (int i = 1; i <= m; ++i){
        //有一个大于等于则返回0
        if (vec[x][i] >= vec[y][i]){
            return 0;
        }
    }//for(i)
    return 1;
}

void CMAX(int&x, int y){
    if (y > x){
        x = y;
    }
}

int DP(int i){
    int &ans = dp[i];
    if (ans > 0){
        //记忆化搜索,避免重复计算
        return ans;
    }
    ans = 1;
    for (int j = 1; j <= n; ++j){
        if (G[i][j]){
            //递归求解
            CMAX(ans, DP(j) + 1);
        }
    }//for(j)
    return ans;
}

//输出序列
void print_ans(int i){
    if (dp[i] == 1){
        //最后一个节点了
        printf("%d", i);
    }
    else{
        for (int j = 1; j <= n; ++j){
            if (G[i][j] && dp[j] + 1 == dp[i]){
                printf("%d ", i);
                print_ans(j);
                break;
            }
        }//for(j)
    }
}

int main()
{
#ifdef _LOCAL
    freopen("D:\\input.txt", "r", stdin);
#endif
    while (scanf("%d%d", &n, &m) == 2){
        for (int i = 1; i <= n; ++i){
            for (int j = 1; j <= m; ++j){
                scanf("%d", vec[i] + j);
            }//for(j)
            sort(vec[i] + 1, vec[i] + m + 1);
        }//for(i)

        //建DAG
        memset(G, 0, sizeof G);
        for (int i = 1; i <= n; ++i){
            for (int j = 1; j <= n; ++j){
                //G[i][j]为1表示盒子i可以嵌套在盒子j中
                if (is_small(i, j)){
                    G[i][j] = 1;
                }
            }//for(j)
        }//for(i)

        //求最长路径
        int ans = 0;
        int best;
        memset(dp, 0, sizeof dp);
        for (int i = 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;
}

       

时间: 2024-10-25 19:22:11

UVa 103 Stacking Boxes --- DAG上的动态规划的相关文章

uva 103 Stacking Boxes (DAG)

uva 103 Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and a

UVA - 103 - Stacking Boxes (动态规划)

UVA - 103 Stacking Boxes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when

uva 103 Stacking Boxes(最长上升子序列)

Description  Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions

UVa 103 - Stacking Boxes (LIS,打印路径)

链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足其中一个的所有边长 按照任意顺序都一一对应小于另一个的边长,这样的最长序列的个数,并且打印任意一个最长子串的路径, 例如:a(9,5,7,3),b(6,10,8,2),c(9,7,5,1),a和b不满足,但c和b满足 分析:首先对没组边长从小到大排序,再对各组图形按最小边排序,再求最大子串, 对于打印路径,可以逆序循环,也可递归求解 #include<cstdio> #include

UVA 103 Stacking Boxes (DP)

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

UVA 103 Stacking Boxes n维最长上升子序列

题目链接:UVA - 103 题意:现有k个箱子,每个箱子可以用n维向量表示.如果一个箱子的n维向量均比另一个箱子的n维向量大,那么它们可以套接在一起,每个箱子的n维向量可以互相交换值,如箱子(2,6)可以和箱子(7,3)套接在一起.求出套接的箱子最多的个数前提下任意一种解决方案. 算法:抛开n维不看,本题就是一个DP的最长上升子序列问题,现在加上了n维的限制,想想也不是很难吧,在DP过程中判断每一维都满足条件即可. 1 #include<iostream> 2 #include<cst

UVa - 103 - Stacking Boxes

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

uva103 - Stacking Boxes(DAG)

题目:uva103 - Stacking Boxes(DAG) 题目大意:给出N个boxes, 并且给出这些箱子的维度,要求找一个最长的序列,能够使得下面的箱子一定能够有个维度序列大于上面的那个箱子的维度序列.例如:A箱子(2 3 4),B箱子(3 4 5),因为有个序列2 3 4 , 3 4 5使得B每个维度的值都大于A,所以A可以在B上面 . 解题思路:DAG.将这些箱子哪个能在哪个上面处理出有向图出来,这里判断是否可以在上面的情况,只要将这两个箱子的维度都从小到大排下序,然后比较一下是否对

Monkey and Banana(DAG上的动态规划问题)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8122    Accepted Submission(s): 4194 Problem Description A group of researchers are designing an experiment to test the IQ of a