POJ 1163 Digital Triangles

Source: Internet
Author: User
Tags cmath

portal:http://poj.org/problem?id=1163

DP Classic, IOI94 questions, on the major OJ have

1#include <iostream>2#include <algorithm>3#include <Set>4#include <cstdio>5#include <cstdlib>6#include <cmath>7 using namespacestd;8 #definefor (i,j,k) for (int i=j;i<=k;i++)9 #defineFORD (i,j,k) for (int i=j;i>=k;i--)Ten #defineLL Long Long One #defineMAXN 1010 A intCOST[MAXN][MAXN],DP[MAXN][MAXN]; - intT,n; - intMain () the { -Cin>>N; -for (I,1, N) -For (J,1, i) +Cin>>Cost[i][j]; -FORD (I,n,1) +For (J,1, i) ADp[i][j]=cost[i][j]+max (dp[i+1][j],dp[i+1][j+1]); atcout<<dp[1][1]<<Endl; - return 0; -}
the wording of water and water

The problem also has a reinforced version, which increases the data size and number of data groups, and then eggs such as UESTC 1011

1#include <iostream>2#include <algorithm>3#include <Set>4#include <cstdio>5#include <cstdlib>6#include <cmath>7 using namespacestd;8 #definefor (i,j,k) for (int i=j;i<=k;i++)9 #defineFORD (i,j,k) for (int i=j;i>=k;i--)Ten #defineLL Long Long One #defineMAXN 110 A intCOST[MAXN][MAXN],DP[MAXN][MAXN]; - intT,n; - intMain () the { -Cin>>T; -for (I,1, T) - { +Cin>>N; -for (I,1, N) +For (J,1, i) ACin>>Cost[i][j]; atFORD (I,n,1) -For (J,1, i) -Dp[i][j]=cost[i][j]+max (dp[i+1][j],dp[i+1][j+1]); -cout<<dp[1][1]<<Endl; -for (I,1, N) -For (J,1, N) in{cost[i][j]=0;DP [i][j]=0;} - } to return 0; +}
still the wording of water

As far as I understand it: DP is based on the optimal substructure of the independent and sub-problems of each sub-problem.

It is important to note that the DP of this question is written in two ways

DP[I][J] can represent a path length maximum of "1" (I,J) as a starting point

You can also indicate a path length maximum of "2" with (I,J) as the end point

Of course, they're the biggest. Daze

If the problem is to output any one of the longest path, use "1" for DP Better

This is because the direct non-backtracking output of the path is by the top-bottom, so it requires a definite rear drive for each passing point, so use "1"

Of course, with "2" words to write a simple retrospective to take the solution, a bit annoying ah ...

The question of the DP decision tree seems to be a DAG, said Dag to meet a variety of DP conditions Ah, it is not as long as the DAG will be able to get DP chaos? ふっふっと

POJ 1163 Digital Triangles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.