题目大意:
(N) 个城市,由 (M) 条单向道路连接,每次去城市 (i) 都能赚 (m_i) 元,但是每去一个城市都会花费一天,旅行 (T) 天就要花费 (Ccdot T^{2}) 元,求最多能赚到多少钱。
正文:
考虑用动态规划解决本题,设 (f_{i,j}) 表示第 (i) 天在城市 (j) 时赚到的钱。那么目标状态显然是 ((f_{T,1}-Ccdot T^{2}))。
动态转移方程是:
[f_{i,j}=max{f_{i-1,k}+m_j}
]
因为是单向边,在用邻接表存图的时候最好是倒着存,以辅助动态转移,那么方程式中的 (k) 代表上一个城市,也就是代码(见下文)中的 (e[i].to)。
代码:
memset(f, -1, sizeof f);
f[0][1] = 0;
for (int i = 1; i <= N - 10; i++)
for (int j = 1; j <= n; j++)
for (int I = head[j]; I; I = e[I].next)
if(f[i - 1][e[I].to] != -1)
f[i][j] = max(f[i][j], f[i - 1][e[I].to] + a[j]);
for (int i = 1; i < N - 10; i++)
ans = max(f[i][1] - c * i * i, ans);