ZOJ 3735 Josephina and RPG (dp)

这题应该是动态规划吧,乱猜的。

dp[i][j]表示 开始的时候选择第i个人。后来经过战斗&交换变成第j个人。的最优值。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define LL long long
#define DB double
#define SI(a) scanf("%d",&a)
#define SD(a) scanf("%lf",&a)
#define SS(a) scanf("%s",a)
#define PF printf
#define MM(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int i=a;ib;i--)
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define bug puts("bug")
using namespace std;
#define N 129
DB re[N][N];
DB dp[N][N];
int n,m;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    int a;
    while(~SI(n)) {
        n = n*(n-1)*(n-2)/6;
        REP(i,0,n) 
            REP(j,0,n) SD(re[i][j]);
        SI(m);
        REP(i,0,n)
            REP(j,0,n) dp[i][j] = -1;
        REP(i,0,n) dp[i][i] = 1;
        while(m--) {
            SI(a);
            DB tmp = -1;
            REP(i,0,n) {
                tmp = -1;
                REP(j,0,n)
                if(dp[i][j]>=0) {
                    tmp = max(tmp,dp[i][j]=dp[i][j]*re[j][a]);
                }
                dp[i][a] = tmp;
            }
        }
        DB ans = -1;
        REP(i,0,n) ans = max(ans,dp[i][a]);
        PF("%.7lf\n",ans);
    }
    return 0;
}


你可能感兴趣的:(模拟,动态规划,动态规划)