Uva103-Stacking Boxes (DAG)
Question: uva103-Stacking Boxes (DAG)
N boxes are given and the dimensions of these boxes are given. The longest sequence is required, this makes sure that the following box has a dimension sequence greater than that of the preceding box. For example, A box (2 3 4) and B box (3 4 5), because there is A sequence of 2 3 4, 3 4 5 so that the values of each dimension of B are greater than, so A can be on B.
Solution: DAG. Process the directed graph on which the boxes can be processed, and determine whether the above conditions can be met. As long as the dimensions of the two boxes are sorted in ascending descending order, then compare whether the value of the corresponding position is smaller than the other one.
For example:
31 4 18 8 27 17
44 32 13 19 41 19
Sort
4 8 17 18 27 31
13 19 19 32 41 44
The number above is smaller than the number at the corresponding position below.
Code:
#include
#include
#include using namespace std;const int N = 35;const int M = 15;int n, m;int box[N][M];int G[N][N];int f[N][N];int path[N][N];bool judge (int a, int b) {for (int i = 0; i < m; i++)if (box[a][i] >= box[b][i])return false;return true;}void handle () {memset (G, 0, sizeof (G));for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {if (i == j)continue;if (judge(i, j))G[i][j] = 1;}}void init () {for (int i = 0; i <= n; i++)for (int j = 0; j <= n; j++)f[i][j] = -1;}int dp (int x, int y) {int& ans = f[x][y];int temp;if (ans != -1)return ans;for (int i = 0; i < n; i++)if (G[y][i]) {temp = dp(y, i) + 1;if (temp > ans) {ans = temp;path[x][y] = i;}}if (ans == -1) {ans = 2;path[x][y] = -1;}return ans;}void printf_ans(int x, int y) {if (path[x][y] == -1)return;printf (" %d", path[x][y] + 1);printf_ans(y, path[x][y]);}int main () {while (scanf ("%d%d", &n, &m) != EOF) {for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)scanf ("%d", &box[i][j]);for (int i = 0; i < n; i++)sort (box[i], box[i] + m);handle ();init();int ans = 1;int temp;int x, y;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)if (G[i][j]) {temp = dp(i, j);if (temp > ans) {ans = temp;x = i;y = j;}}printf ("%d\n", ans);if (ans != 1) { printf("%d %d", x + 1, y + 1);printf_ans(x, y);} elseprintf ("1");printf ("\n");}return 0;}