// [解题方法]
// 记忆化搜索(递归,子问题的结果用备忘录存起来,避免重复求解)
// 二维nxt数组按照题意记录路径
// dp[x][y](即dfs(x,y))表示从(x,y)走到最右边需要的最小花费
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define LL long long
#define inf 1e16
LL dp[11][111],w[11][111], mins, res[105];
int r, c;
struct point{
int x, y;
}nxt[11][111];
LL dfs (int x, int y)
{
if (y >= c) return (dp[x][y]=w[x][y]);
if (dp[x][y] < inf)
return dp[x][y];
int i;
LL tp = inf;
for (i = x-1; i <= x+1; i++)
{
int tr = i;
if (tr == 0) tr = r;
else if (tr > r) tr = 1;
LL tmp = dfs (tr, y+1) + w[x][y];
//更新路径(花费更小+字典序)
if (tmp < tp || (tmp == tp && tr < nxt[x][y].x)) {
nxt[x][y].x = tr;
nxt[x][y].y = y+1;
tp = tmp;
}
}
return (dp[x][y]=tp);
}
int main()
{
int i, j;
while (cin >> r >> c)
{
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++)
cin >> w[i][j], dp[i][j] = inf;
mins = inf;
int v;
memset (nxt, -1, sizeof(nxt));
for (i = 1; i <= r; i++)
{
LL tp = dfs (i, 1);
if (tp < mins) {
mins = tp;
v = i;
}
}
int tc = 1, k = 0;
while (v > 0)
{
res[k++] = v;
int tv = v;
v = nxt[tv][tc].x;
tc = nxt[tv][tc].y;
}
cout << res[0];
for (i = 1; i < k; i++) cout << ' ' << res[i];
cout << endl << mins << endl;
}
return 0;
}
分享到:
相关推荐
在这个项目中,还采用了不确定重排(Unidirectional Repair,URR)策略,它是一种处理TSP约束的有效方法,能够快速修复因变异产生的非法解。 通过遗传算法求解旅行商问题,虽然不能保证找到全局最优解,但可以找到...
Minimizing AMDs in unidirectional WDM rings with grooming factor 6,徐允庆,常彦勋,In wavelength division multiplexing for unidirectional rings, traffic grooming is used to pack low rate signals ...
标题“Bifurcation analysis in a unidirectional ring of neural networks with distributed delays”表示,文章研究的焦点是对具有分布时滞的单向神经网络环进行分支分析。分支分析是动态系统理论中的一个重要领域...
### Unidirectional Edge Modes Launched by Surface Fluctuation in Magnetic Metamaterials #### 概述 本研究探讨了在磁性超材料(Magnetic Metamaterials, MM)表面波动下,通过高斯光束垂直入射所激发的单向...
UniDirectional 属性用于指定是否使用单向数据集特性。例如,在 Delphi 中,可以使用以下代码来指定是否使用单向数据集特性: ```delphi UniQuery1.UniDirectional := True; ``` 7. SpecificOptions 属性 ...
植物的单向杂交假说检验:对海桑属、木榄属和橐吾属的观察 在生物科学领域,植物杂交现象是研究物种间生殖隔离和进化机制的重要方面。植物自然杂交是一种相对常见的现象,研究其模式可以为生殖隔离机制提供深入的...
Recent experiments demonstrated that chiral symmetry breaking at an exceptional point (EP) is a viable route to achieve unidirectional laser emission in microring lasers. By a detailed semiconductor ...
A simple method for generating unidirectional surface plasmon polariton beams with arbitrary profiles
SAP ERP信息化系统学习资料文档 本文档提供了SAP ERP信息化系统学习资料文档,包括项目概况、业务流程、IT架构、集成架构、基础架构、项目路线图、预算估算、合作策略、团队结构与资源、假设与依赖关系等方面的详细...
Gaussian beam is incident to a magnetic metamaterial (MM) slab, it can be completely absorbed at a particular direction, resulting in a unidirectional perfect absorption. The unidirectionality is due ...
A simplified ring cavity for achieving a unidirectional room temperature multi-wavelength erbium-doped fiber ring laser without optical isolator is demonstrated. The fiber ring cavity is built in such...
A short distributed feedback fiber laser with a nearly unidirectional output is fabricated and tested. The short fiber laser is made of a polarization-dependent phase-shift grating fabricated with a ...
We present an experimental study on a unidirectional surface plasmon polariton (SPP) launcher based on a compact binary area-coded nanohole array, where the symmetry breaking is realized via effective...
标题和描述提到的"unidirectional-one2one-sharedprimarykey"是一个关于Hibernate映射的具体示例,涉及的是单向一对一(OneToOne)关系,并且使用了共享主键(Shared Primary Key)作为关联方式。 单向一对一关联是...
It is demonstrated that laser output of the Erbium-Ytterbium co-doped fiber under the bi-directional pumping regime is more stable than that under the unidirectional pumping one due to the relatively...
### 高效单向发射表面等离子体激元的多槽结构 #### 摘要与背景 本文探讨了一种高效单向发射表面等离子体激元(Surface Plasmon Polariton, SPP)的方法,即通过多槽结构实现。等离子体激元作为一种在金属/介电质...
### 高效单向发射表面等离子体激元的级联不对称沟槽结构 #### 研究背景与意义 本研究论文聚焦于一种新型的级联不对称沟槽结构,该结构能够高效地单向发射表面等离子体激元(Surface Plasmon Polaritons, SPPs)。...