hdu 1224 简单dp +恶心的输出

没啥好说的,除了输出恶心,要明确起点和终点是固定的即可

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define MAX 107

using namespace std;

int t,n,m,u,v;
int a[MAX];
int dp[MAX];
int pre[MAX];
int mp[MAX][MAX];

void print ( int u )
{
    if ( u == -1 ) return;
    print ( pre[u] );
    printf ( "%d->" , u );
}


int main ( )
{
    int c = 1;
    scanf ( "%d" , &t );
    while ( t-- )
    {
        scanf ( "%d" , &n );
        for ( int i = 1 ; i <= n ; i++ ) scanf ( "%d" , &a[i] );
        scanf ( "%d" , &m );
        memset ( mp , 0 , sizeof ( mp ) );
        for ( int i = 1 ; i <= m ; i++ )
        {
            scanf ( "%d%d" , &u , &v );
            mp[u][v] = 1;
        }
        a[n+1] = 0;
        memset ( dp , -0x3f , sizeof ( dp ) );
        dp[1] = 0;
        memset ( pre , -1 , sizeof ( pre ) );
        for ( int i = 2 ; i <= n+1 ; i++ )
            for ( int j = 1 ; j < i ; j++ )
                if ( mp[j][i] )
                    if ( dp[i] <= dp[j] + a[i] )
                    {
                        dp[i] = dp[j] + a[i];
                        pre[i] = j;
                    }
        printf ( "CASE %d#\n" , c++ );
        printf ( "points : %d\n" , dp[n+1] );
        printf ( "circuit : " );
        print ( pre[n+1] );
        printf ( "1\n" );
        if ( t ) printf ( "\n" );
    }
}


你可能感兴趣的:(C++,动态规划)