by 0htoAi,写于2021.8.14

Problem Description

There are n×n cells on a grid, the top-left cell is at (1,1) while the bottom-right cell is at (n,n). You start at (1,1) and move to (n,n). At any cell (i,j), you can move to (i+1,j) or (i,j+1), provided that you don't move out of the grid. Clearly, you will make exactly 2n−2 steps.

When you are at cell (i,j), including the starting point (1,1) and the destination (n,n), you can take all the ai,j diamonds at this cell, and have a chance to raise the price of each diamond by bi,j dollars. You will sell all the diamonds you have with the final price in the end, and your goal is to choose the optimal path that will maximize your profits. Note that initially the price of each diamond is zero, and you have nothing to sell.

Input

The first line contains a single integer T (1≤T≤10), the number of test cases. For each test case:

The first line contains a single integer n (1≤n≤100), denoting the size of the grid.

Each of the following n lines contains n integers, the i-th line contains ai,1,ai,2,…,ai,n (1≤ai,j≤106), denoting the number of diamonds in each cell.

Each of the following n lines contains n integers, the i-th line contains bi,1,bi,2,…,bi,n (1≤bi,j≤106), denoting how much you can raise the price in each cell.

It is guaranteed that all the values of ai,j and bi,j are chosen uniformly at random from integers in [1,106]. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.

Output

For each test case, output a single line containing an integer: the maximum number of dollars you can earn by selling diamonds.

Sample Input

1
4
2 3 1 5
6 3 2 4
3 5 1 4
5 2 4 1
3 2 5 1
2 4 3 5
1 2 3 4
4 3 5 3

Sample Output

528

题目大意

在一个 \(n\times n\) 的网格内,每格有 \(a_{ij}\) 和 \(b_{ij}\)。从左上角 \((1,1)\) 走到右下角 \((n,n)\)​,每一步只能向下或者向右走。设路径上所有的 \(a_{ij}\) 之和为 \(sa\),\(b_{ij}\) 之和为 \(sb\),求 \(sa \times sb\) 的最大值。

算法思路

Beam Search(集束搜索)算法,设一个状态包含 \({x,y,sa,sb}\)​ ,且值为 \(sa\times sb\),记录每一个状态的前 \(k\) 大值,一直转移状态直到算出状态 \(x=N\) 且 \(y=N\)​ 的最大值。

用 vector 数组 BEAM 记录每一步的状态,状态限制数 \(MAXHASH\) 设为 \(50\)。

取每个状态的前 \(50\)​​ 大值可以用大根堆来维护。限制状态需要用到数组hash_table记录当前状态数。

现在做法就很明确了,Beam Search代码如下。

点击查看代码
inline long long BEAM_SEARCH()
{
memset(hash_table,0,sizeof(hash_table));
BEAM.clear();
BEAM.push_back(STATE(1,1,a[1][1],b[1][1],(long long)a[1][1]*b[1][1]));
hash_table[make_hash(1,1)]=1;
register long long Max=0;
priority_queue<STATE>q;
while(BEAM.size()>0)
{
for(register int i=0;i<BEAM.size();++i)
{
int x=BEAM[i].x,y=BEAM[i].y;
int sa=BEAM[i].sa,sb=BEAM[i].sb;
if(x==N&&y==N)
{
Max=max(Max,BEAM[i].val);
continue;
}
if(y+1<=N)
{
q.push(STATE(x,y+1,sa+a[x][y+1],sb+b[x][y+1],(long long)(sa+a[x][y+1])*(sb+b[x][y+1])));
}
if(x+1<=N)
{
q.push(STATE(x+1,y,sa+a[x+1][y],sb+b[x+1][y],(long long)(sa+a[x+1][y])*(sb+b[x+1][y])));
}
}
int cnt=BEAM.size();
BEAM.clear();
while((!q.empty())&&BEAM.size()<=cnt*MAXHASH)
{
if(hash_table[make_hash(q.top().x,q.top().y)]<=MAXHASH)
{
BEAM.push_back(q.top());
hash_table[make_hash(q.top().x,q.top().y)]++;
}
q.pop();
}
}
return Max;
}

HDU 6981.Rise in Price (Beam Search 贪心)的更多相关文章

  1. Beam Search快速理解及代码解析

    目录 Beam Search快速理解及代码解析(上) Beam Search 贪心搜索 Beam Search Beam Search代码解析 准备初始输入 序列扩展 准备输出 总结 Beam Sea ...

  2. 【NLP】选择目标序列:贪心搜索和Beam search

    构建seq2seq模型,并训练完成后,我们只要将源句子输入进训练好的模型,执行一次前向传播就能得到目标句子,但是值得注意的是: seq2seq模型的decoder部分实际上相当于一个语言模型,相比于R ...

  3. 集束搜索beam search和贪心搜索greedy search

    贪心搜索(greedy search) 贪心搜索最为简单,直接选择每个输出的最大概率,直到出现终结符或最大句子长度. 集束搜索(beam search) 集束搜索可以认为是维特比算法的贪心形式,在维特 ...

  4. 关于 Image Caption 中测试时用到的 beam search算法

    关于beam search 之前组会中没讲清楚的 beam search,这里给一个案例来说明这种搜索算法. 在 Image Caption的测试阶段,为了得到输出的语句,一般会选用两种搜索方式,一种 ...

  5. Empirical Analysis of Beam Search Performance Degradation in Neural Sequence Models

    Empirical Analysis of Beam Search Performance Degradation in Neural Sequence Models  2019-06-13 10:2 ...

  6. beam search 和 greedy search

    贪心搜索(greedy search): 贪心搜索最为简单,直接选择每个输出的最大概率,直到出现终结符或最大句子长度. 集束搜索(beam search): 集束搜索可以认为是维特比算法的贪心形式,在 ...

  7. Beam Search快速理解及代码解析(下)

    Beam Search的问题 先解释一下什么要对Beam Search进行改进.因为Beam Search虽然比贪心强了不少,但还是会生成出空洞.重复.前后矛盾的文本.如果你有文本生成经验,一定对这些 ...

  8. Beam Search快速理解及代码解析(上)

    Beam Search 简单介绍一下在文本生成任务中常用的解码策略Beam Search(集束搜索). 生成式任务相比普通的分类.tagging等NLP任务会复杂不少.在生成的时候,模型的输出是一个时 ...

  9. Beam Search(集束搜索/束搜索)

    找遍百度也没有找到关于Beam Search的详细解释,只有一些比较泛泛的讲解,于是有了这篇博文. 首先给出wiki地址:http://en.wikipedia.org/wiki/Beam_searc ...

  10. 关于Beam Search

    Wiki定义:In computer science, beam search is a heuristic search algorithm that explores a graph by exp ...

随机推荐

  1. Linux的优缺点

    作为一个Archlinux用户, 断然是不会认为Linux有缺点的, 任何所谓的缺点都是自己技艺不精或者没有好好利用搜索引擎而造成的狭隘偏见. 但是假如是一位习惯于视窗系统的新手而言, 假如他上手的是 ...

  2. 【算法数据结构专题】「延时队列算法」史上手把手教你针对层级时间轮(TimingWheel)实现延时队列的开发实战落地(上)

    承接上文 承接之前的[精华推荐 |[算法数据结构专题]「延时队列算法」史上非常详细分析和介绍如何通过时间轮(TimingWheel)实现延时队列的原理指南],让我们基本上已经知道了「时间轮算法」原理和 ...

  3. [arthas] UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields)

    问题描述 [arthas@1]$ trace cn.xx.dataservice.biz.dataservice.controller.v1.CommonSearchController datase ...

  4. [WEB]对于"Refused to execute script from 'http://xx.xx.xx/yy/zz.js' because its MIME type ('') is not executable, and strict MIME type checking is enabled."问题的解决办法

    1 文由 阶段一:对WEB服务器HTTP通信的header设置了安全头(X-Content-Options:nosniff) 两个月前协助交付侧大哥处理.修复一个三级等保项目的第三方安全公司释出的安全 ...

  5. spring-boot-starter-parent not found

    项目开始的springboot版本为2.6.6 <parent> <groupId>org.springframework.boot</groupId> <a ...

  6. JAVASE和JAVAEE的区别

    JAVASE和JAVAEE的区别 JavaEE: Java Enterprise Edition,Java企业版,多用于企业级开发,包括web开发等等.企业版本帮助开发和部署可移植.健壮.可伸缩切安全 ...

  7. UE4 内存写坏导致异常崩溃问题记录

    1. 问题表现 经常出现进程崩溃,崩溃堆栈较为底层 原因基本上都是 read write memory 时触发了异常,盘查后初步怀疑是内存写坏了. 2. 排查期 UE 支持各种内存分配器: TBB A ...

  8. OpenAI ChatGPT 能取代多少程序员的工作?导致失业吗?

    阅读原文:https://bysocket.com/openai-chatgpt-vs-developer/ ChatGPT 能取代多少程序员的工作?导致我们程序员失业吗?这是一个很好的话题,我这里分 ...

  9. 【Visual Leak Detector】核心源码剖析(VLD 1.0)

    说明 使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记.本篇对 VLD 1.0 源码做内存泄漏检测的思路进行剖析.同系列文章目录可见 <内存泄漏检测工具>目录 目录 说明 1. 源码 ...

  10. [C++核心编程] 4.1、类和对象-封装

    文章目录 4 类和对象 4.1 封装 4.1.1 封装的意义 4.1.2 struct和class区别 4.1.3 成员属性设置为私有 4 类和对象 C++面向对象的三大特性为:封装.继承.多态 C+ ...