DP 状态转移方程很简单。。。。但是输出坑了我无数次。。。太坑了。。。我开始还以为状态转移方程错了。。。。
AC代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
int index[1100];
struct straight{
int x, y;
};
void out (int i)
{
if (i==1)
{
printf("1");
return ;
}
out(index[i]);
printf("->%d",i);
}
int main(){
struct straight str[100000];
int v[1100], dp[1100];
int t, n, m, Case = 1;
cin >> t;
while( t-- ){
if( Case != 1 ){
cout << endl;
}
cin >> n;
for( int i = 1; i <= n; i++ ){
cin >> v[i];
}
cin >> m;
for( int i = 1; i <= m; i++ ){
cin >> str[i].x >> str[i].y;
}
memset( dp, 0, sizeof( dp ) );
memset( index, 0, sizeof( index ) );
v[n+1] = 0;
for( int i = 1; i <= n + 1; i++ ){
int max = 0;
int in = 0;
for( int j = 1; j <= m; j++ ){
if( str[j].y == i ){
if( dp[str[j].x] + v[i] > max ){
max = dp[str[j].x] + v[i];
in = str[j].x;
}
}
}
dp[i] = max;
index[i] = in;
}
printf( "CASE %d#\n", Case++ );
cout << "points : " <<dp[n+1] << endl;
cout << "circuit : ";
out(index[n+1]);
cout << "->" << 1 << endl;
}
return 0;
}