Moving to New Office
DP
#include <iostream>
#define INF 99999999
using namespace std;
int main()
{
int t, x, y, n;
int m[100];
long dp[101][101];
cin >> t;
while(t--)
{
cin >> x >> y;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> m[i];
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
dp[i][j] = INF;
}
for(int l = 2; l <= n; l++)
{
for(int i = 0; i <= n - l; i++)
{
int j = i + l - 1;
if(l == 2)
{
dp[i][j] = 0;
//cout << "i = " << i << " j = " << j << " Val = " << dp[i][j] << endl;
}
else
{
for(int k = i + 1; k < j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + x * (m[k] - m[i]) + y * (m[j] - m[k]));
//cout << "i = " << i << " j = " << j << " Val = " << dp[i][j] << endl;
}
}
}
}
cout << dp[0][n - 1] << endl;
}
return 0;
}
Comments
Post a Comment