传送门:【SGU】104. Little shop of flowers
题目分析:dp[i][j] = max ( dp[i - 1][j - 1] + w[i][j] , dp[i][j - 1] )
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> //#include <cmath> #include <queue> using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = a ; i < b ; ++ i ) #define For( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define rev( i , a , b ) for ( int i = a ; i >= b ; -- i ) #define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next ) #define clr( a , x ) memset ( a , x , sizeof a ) #define cpy( a , x ) memcpy ( a , x , sizeof a ) const int MAXN = 105 ; struct Node { int x , y ; Node () {} Node ( int x , int y ) : x ( x ) , y ( y ) {} } ; Node pre[MAXN][MAXN] ; int dp[MAXN][MAXN] ; int w[MAXN][MAXN] ; int F , V ; void print ( int x , int y ) { if ( y ) { print ( pre[x][y].x , pre[x][y].y ) ; if ( pre[x][y].x != x ) printf ( "%d%c" , y , x < F ? ' ' : '\n' ) ; } } void solve () { pre[0][0] = Node ( 0 , 0 ) ; For ( i , 1 , F ) For ( j , 1 , V ) scanf ( "%d" , &w[i][j] ) ; For ( i , 1 , F ) { For ( j , i , V ) { dp[i][j] = dp[i - 1][j - 1] + w[i][j] ; pre[i][j] = Node ( i - 1 , j - 1 ) ; if ( j > i && dp[i][j] < dp[i][j - 1] ) { dp[i][j] = dp[i][j - 1] ; pre[i][j] = Node ( i , j - 1 ) ; } } } printf ( "%d\n" , dp[F][V] ) ; print ( F , V ) ; } int main () { while ( ~scanf ( "%d%d" , &F , &V ) ) solve () ; return 0 ; }