题目大意
路面上有一些开着的灯,每个灯有功率和它的位置,人在第c个灯处,行走速度1m/s。问怎样关灯能使耗能最小,输出这个耗能。
思路
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX_N = 60, INF = 0x3f3f3f3f; int F[MAX_N][MAX_N][2], Pos[MAX_N], W[MAX_N]; int WPrefix[MAX_N]; int N, Start; void GetWPrefix() { for (int i = 1; i <= N; i++) WPrefix[i] = WPrefix[i - 1] + W[i]; } int DP() { memset(F, INF, sizeof(F)); F[Start][Start][0] = F[Start][Start][1] = 0; for (int len = 2; len <= N; len++) for (int i = 1; i <= N - len + 1; i++) { int j = i + len - 1; F[i][j][1] = min(F[i][j][1], F[i][j - 1][1] + (Pos[j] - Pos[j - 1]) * (WPrefix[i - 1] + WPrefix[N] - WPrefix[j - 1])); F[i][j][1] = min(F[i][j][1], F[i][j - 1][0] + (Pos[j] - Pos[i]) * (WPrefix[i - 1] + WPrefix[N] - WPrefix[j - 1])); F[i][j][0] = min(F[i][j][0], F[i + 1][j][0] + (Pos[i + 1] - Pos[i]) * (WPrefix[i] + WPrefix[N] - WPrefix[j])); F[i][j][0] = min(F[i][j][0], F[i + 1][j][1] + (Pos[j] - Pos[i]) * (WPrefix[i] + WPrefix[N] - WPrefix[j])); } return min(F[1][N][0], F[1][N][1]); } int main() { scanf("%d%d", &N, &Start); for (int i = 1; i <= N; i++) scanf("%d%d", Pos + i, W + i); GetWPrefix(); printf("%d ", DP()); }