- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有N个城市。每个城市都有1到10(含)类型的传送点。给你一个大小为 N 的数组,用整数表示每个城市的传送类型。对于前。 1 3 5 3 8 9
。目标是根据以下规则找到从数组中的第一个条目到最后一个条目所需的最少小时数:
3
。此操作花费给定的 R 小时数。我已经实现了一个动态编程解决方案,当最快的方法只是向前移动时,它可以完美地工作。但在某些情况下,返回几个城市然后向前传送会更快,从而最大限度地减少花费的时间。
这是我的算法失败的例子:例如。 2 1 3 4 6 8 5 7 4 2 3 5 2 1 3 4 3 5 6 7
和 R = 2
正确答案:索引 0(时间 = 0)-> 索引 9(时间 = 2)-> 索引 8(时间 = 3)-> 索引 7(时间 = 4)-> 索引 19(时间 = 6)。我的算法会找到仅向前移动的最快方法,显然,正确的最快方法还涉及向后移动。
到目前为止,这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int dp[200] = {};
short ti[200] = {};
int numCities, R;
cin >> numCities >> R;
for (int i = 0; i < numCities; i++)
{
short temp;
cin >> temp;
ti[i] = temp;
}
for (int i = 1; i < numCities; i++)
{
dp[i] = dp[i - 1] + 1;
for (int x = i - 1; x >= 0; x--)
{
if (ti[x] == ti[i])
{
if (R + dp[x] < dp[i])
dp[i] = R + dp[x];
}
}
}
cout << dp[numCities - 1];
return 0;
}
我如何让我的算法适用于这种情况,其中较低的状态取决于较大的状态?
编辑:我按以下方式使用动态规划:对于每个城市,我计算在给定起始状态 dp[0] = 0
的情况下到达它们的最快方式。则递归关系为:dp[i] = min(dp[i - 1] + 1, dp[每隔一个具有相同传送类型的点] + R)
最佳答案
动态规划适用于问题具有最优子结构的情况。也就是说,您必须找到一种方法来分割问题,以便分割的最佳解决方案可以用作构建 block 来找到整个问题的最佳解决方案。
上面,我看你说要用动态规划。我看到了代码。我没有看到对您正在考虑的子问题的明确解释。也就是说:对解决方案的概念性理解是正确进行动态编程的关键,而这正是您没有提供的。
我的直觉是,在这种情况下动态规划不是一个好的方法,因为:
您可以使用嵌套循环来解决这些问题。这为您提供了 O(n^2) 时间解决方案。
但是,将此问题视为加权图遍历的一个实例,您可以使用 Dijkstra's algorithm 来解决它。在 O(n log n + m) 时间内(O(n) 遍历足以建立每个节点的邻居),其中 m 是考虑的边缘数量(这里可以通过识别每个传送器类型将仅使用一次来将其限制为 Θ(m) 的值)。为什么不这样做呢?
您可以尝试使用 A* 来改善运行时间,尽管我不相信这会在一个维度上提供很大的改进。
完成此操作的代码可能如下所示:
#include <iostream>
#include <queue>
#include <unordered_set>
#include <unordered_map>
typedef std::unordered_map<int, std::vector<int> > tele_network_t;
int Dijkstra(const std::vector<int> &graph, const tele_network_t &tn, const int R){
//This whole mess makes the smallest elements pop off the priority queue first
std::priority_queue<
std::pair<int, int>,
std::vector< std::pair<int, int> >,
std::greater< std::pair<int, int> >
> pq; //<distance, index>
//Keeping track of the teleporters used allows us to speed up the algorithm by
//making use of the theorem that each teleporter type will be used only once.
std::unordered_set<int> teleporters_used;
//Keep track of the path
std::vector<int> parent(graph.size(),-1); //Parent==-1 indicates an unvisited node
//At 0 distance, place the 0th node
pq.emplace(0,0);
parent[0] = 0; //The only node whose parent is itself should be node 0
while(!pq.empty()){
const auto c = pq.top();
pq.pop();
//We've reached the goal node
if(c.second==graph.size()-1){
std::cout<<"Dist = "<<c.first<<std::endl;
break;
}
//Insert neighbours
if(c.second!=0 && parent[c.second-1]==-1){ //Left neighbour
parent[c.second-1] = c.second;
pq.emplace(c.first+1,c.second-1);
}
if(parent[c.second+1]==-1){ //Right neighbour: can't overflow because goal is the rightmost node
parent[c.second+1] = c.second;
pq.emplace(c.first+1,c.second+1);
}
//Inner loop is executed once per teleporter type
if(teleporters_used.count(graph[c.second])==0)
for(const auto i: tn.at(graph[c.second])){
if(parent[i]==-1){
pq.emplace(c.first+R,i);
parent[i] = c.second;
}
}
teleporters_used.insert(graph[c.second]);
}
//Trace our steps backwards to recover the path. Path will be reversed, but a
//stack could be used to fit this.
int p = graph.size()-1;
while(parent[p]!=p){
std::cout<<p<<std::endl;
p = parent[p];
}
std::cout<<0<<std::endl;
}
int main(){
tele_network_t tele_network;
const int R = 2;
std::vector<int> graph = {{2,1,3,4,6,8,5,7,4,2,3,5,2,1,3,4,3,5,6,7}};
//Determine network of teleporters
for(int i=0;i<graph.size();i++)
tele_network[graph[i]].push_back(i);
Dijkstra(graph, tele_network, 2);
}
关于algorithm - 动态规划依赖于更大的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42655404/
我正在创建我的第一个 WAR 文件。我一直在试验 ant buildfile 语法,我的 buildfile 的第一部分从我的 Eclipse 项目中获取内容并将其放入 /dist 文件夹中,然后将其
我是一名学习 SQL 和 PHP 的学生,我接到了一项任务,要使用 PHP 和 mySQLi 创建学生反馈表,我真的一直在思考如何为项目设计数据库! 我正在创建一个系统,用户可以在其中登录网页,如果用
这个问题在这里已经有了答案: Is it possbile to test for expected errors when the testee exits with failure using
我目前正在设计和开发一个 Web 应用程序,该应用程序有可能快速增长。我将提供一些一般信息,然后继续我的问题。我会说我是一名中级网络程序员。 以下是一些规范:MySQL - 数据库后端PHP - 用于
我不知何故无法在我的日志解析器应用程序中实现报告功能。 这是我目前所做的: 我正在编写一个应用程序,它读取日志文件并在字符串中搜索可以在用户配置文件中定义的多个正则表达式。对于从配置中解析的每个所谓的
我有兴趣学习如何在多开发团队场景中设计/规划 Web 应用程序开发。 假设“项目经理/负责人”的角色: 成功的 Web 应用程序开发需要哪些“文档”? 需要什么 UML 图,需要什么程度? 在设计/计
table a (t_a): id name last first email state country 0 sklass klass steve
我们建立了一个广泛使用 JQuery UI 的 AJAX 网站。我们有 30 多个自制的 JQuery UI 小部件(动态加载)。我们到处都使用 JQuery native 小部件:对话框、 slid
我是一名优秀的程序员,十分优秀!