「LibreOJ NOIP Round #1」旅游路线

题目链接https://loj.ac/problem/539


题解

这个题就很神奇

首先大力$dp$很好想,因为可以把一维放到状态里以取消后效性。

然后就能倍增了...因为就是个智障$dp$

我没想出来/px

代码

#include <bits/stdc++.h>

#define N 110 

#define M 1010 

#define K 30 

#define inf 0x3f3f3f3f 

using namespace std;

int g[N][N][K], p[N], c[N], a[N], b[N], w[N][N], f[N][N * N];

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
} int main() {
int n = rd(), m = rd(), C = rd(), T = rd();
memset(g, 0xef, sizeof g);
for (int i = 1; i <= n; i ++ ) {
p[i] = rd(), c[i] = rd();
c[i] = min(c[i], C);
g[i][i][0] = 0;
}
for (int i = 1; i <= m; i ++ ) {
int x = rd(), y = rd(), z = rd();
g[x][y][0] = z;
}
for (int k = 1; k <= 17; k ++ ) {
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= n; j ++ ) {
for (int x = 1; x <= n; x ++ ) {
g[i][j][k] = max(g[i][j][k], g[i][x][k - 1] + g[x][j][k - 1]);
}
}
}
}
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= n; j ++ ) {
a[j] = -inf;
}
a[i] = 0;
for (int k = 0; k <= 17; k ++ ) {
if((c[i] >> k) & 1) {
for (int j = 1; j <= n; j ++ ) {
b[j] = -inf;
for (int x = 1; x <= n; x ++ ) {
b[j] = max(b[j], a[x] + g[x][j][k]);
}
}
for (int j = 1; j <= n; j ++ ) {
a[j] = b[j];
}
}
}
for (int j = 1; j <= n; j ++ ) {
w[i][j] = a[j];
}
}
for (int q = 0; q <= n * n; q ++ ) {
for (int x = 1; x <= n; x ++ ) {
if (q >= p[x]) {
for (int y = 1; y <= n; y ++ ) {
f[x][q] = max(f[x][q], f[y][q - p[x]] + w[x][y]);
}
}
}
}
while (T -- ) {
int s = rd(), q = rd(), d = rd();
int pl = lower_bound(f[s], f[s] + n * n + 1, d) - f[s];
if (pl > q) {
puts("-1");
}
else {
printf("%d\n", q - pl);
}
}
return 0;
}

小结:如果是图上的dp,我们可以考虑直接加上一个维度,使得不会出现后效性。

最新文章

  1. 【jQuery】 jQuery上下飘动效果
  2. 数据库DDL审计
  3. 使用C# WinForm制作 员工打卡项目 -- S2 2.3
  4. BibTex参考文献制作
  5. 第10章 系统级I/O
  6. Functions
  7. mac 下svn降级
  8. arm-linux-gcc 安装和测试
  9. Kotlin的扩展函数:扩展Android框架(KAD 08)
  10. Array.from()
  11. 01_Struts2概述及环境搭建
  12. 四、fgets与fputs
  13. vlookup使用
  14. win10系统中如何解决cmd中的路径和现在电脑的用户名不一致
  15. Yahoo Programming Contest 2019 D - Ears
  16. echo变量失败,提示:ECHO 处于关闭状态
  17. oozie 常用命令
  18. 单调栈(G - Sliding Window POJ - 2823 )
  19. hdu5542 The Battle of Chibi【树状数组】【离散化】
  20. django.db.utils.OperationalError: (1071, &#39;Specified key was too long; max key length is 767 bytes&#39;);

热门文章

  1. [转]vue解决刷新页面vuex数据、params参数消失的问题
  2. Postman集合/文件夹/请求中脚本的执行顺序
  3. jQuery属性操作之html属性操作
  4. Hadoop元数据备份与恢复方案
  5. AT3576 Popping Balls
  6. CSS子元素在父元素中水平垂直居中的几种方法
  7. OSX 改变PHP安装路径环境变量
  8. HTML5 烟花系统
  9. Celery分布式队列学习
  10. 前端知识点回顾之重点篇——CORS