http://acm.hdu.edu.cn/showproblem.php?pid=4711
真的是坑吐血了,wa了5次 最后实在无语 把long long double 改成long double 就过了
#include <map> #include <set> #include <list> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <bitset> #include <vector> #include <cstdio> #include <cctype> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> #define PI 3.1415926535897932384626433832795 using namespace std; const int N = 110; const double INF = 1e50; const double EPS = 1e-14; int T, n, m, w; int ord[1010]; double pm[N][N], pw[N][N]; long double dp[1010][N]; int fa[1010][N]; int main() { scanf("%d", &T); while(T--) { scanf("%d%d%d", &n, &m, &w); for(int i = 0; i < n; ++i) scanf("%d", ord + i); for(int i = 0; i < m; ++i) for(int j = 0; j < m; ++j) { scanf("%lf", &pm[i][j]); if(pm[i][j] > EPS) pm[i][j] = log(pm[i][j]); else pm[i][j] = -INF; } for(int i = 0; i < m; ++i) for(int j = 0; j < w; ++j) { scanf("%lf", &pw[i][j]); if(pw[i][j] > EPS) pw[i][j] = log(pw[i][j]); else pw[i][j] = -INF; } for(int i = 0; i < m; ++i) dp[n-1][i] = pw[i][ord[n-1]]; for(int i = n - 2; i >= 0; --i) { for(int j = 0; j < m; ++j) dp[i][j] = -INF; for(int j = 0; j < m; ++j) { for(int k = 0; k < m; ++k) { if(dp[i][j] < dp[i+1][k] + pm[j][k] + pw[j][ord[i]]) { dp[i][j] = dp[i+1][k] + pm[j][k] + pw[j][ord[i]]; fa[i][j] = k; } } } } int pos = 0; long double best = -INF; for(int i = 0; i < m; ++i) if(best < dp[0][i] + pm[0][i]) { best = dp[0][i] + pm[0][i], pos = i; } printf("%d", pos); for(int i = 0; i < n-1; ++i) printf(" %d", pos = fa[i][pos]); printf(" "); } return 0; }