P1004(多进程DP)
作者:
hayate
,
2024-05-13 08:24:15
,
所有人可见
,
阅读 14
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 10;
int g[N][N];
int f[N][N][N][N];
int main()
{
int n;
cin >> n;
for( ; ; )
{
int x, y, z;
cin >> x >> y >> z;
if(x == 0 && y == 0 && z == 0)
{
break;
}
else
{
g[x][y] = z;
}
}
for(int i = 1; i <= n; i ++ )
{
for(int j = 1; j <= n; j ++ )
{
for(int k = 1; k <= n; k ++ )
{
for(int o = 1; o <= n; o ++ )
{
f[i][j][k][o] = max(max(f[i - 1][j][k - 1][o], f[i - 1][j][k][o - 1]), max(f[i][j - 1][k - 1][o], f[i][j - 1][k][o - 1])) + g[i][j] + g[k][o];
if(i == k && j == o)
{
f[i][j][k][o] -= g[i][j];
}
}
}
}
}
cout << f[n][n][n][n] << endl;
return 0;
}