algorithm ch15 FastWay

这是DP最基础的一个问题,刚开始学习这一块,实现了一下,不要黑我巨长的参数表,新手。

代码如下:

void FastWay(int l1[], int l2[], int e1, int e2, int x1, int x2, int t1[], int t2[], int n)
{
    int f1[6] ={0};
    int f2[6] ={0};
    int l[12] = {0};
    f1[0] = e1 + l1[0];
    f2[0] = e2 + l2[0];
    int lres = 0;
    int fres = 0;
    int offset = 0;

    for(int i = 1; i < n; ++i)
    {
        if((f1[i-1] + l1[i]) <= (f2[i-1] + t2[i-1] + l1[i]))
        {
            l[i] = 1;
            f1[i] = f1[i-1] + l1[i];
        }
        else
        {
            f1[i] = f2[i-1] + t2[i-1] + l1[i];
            l[i] = 2;
        }
        if(f2[i-1] + l2[i] <= f1[i-1] + t1[i-1] + l2[i])
        {
            l[i+6] = 2;
            f2[i] = f2[i-1] + l2[i];
        }
        else
        {
            l[i+6] = 1;
            f2[i] = f1[i-1] + t1[i-1] + l2[i];
        }
    }
    if(f1[n-1] + x1 <= f2[n-1] + x2)
    {
        lres = 1;
        fres = f1[n-1] + x1;

    }
    else
    {
        lres = 2;
        fres = f2[n-1] + x2;
        offset = 6;
    }
    PrintWayRecurse(l, lres, n);
}
void PrintWayRecurse(int l[], int lres, int n)
{
    if(n == 0)
        return;
    int offset = 0;

    if(lres == 2)
    {
        offset = 6;
    }
    else
    {
        offset = 0;
    }
    PrintWayRecurse(l, l[n -1 +offset], n-1 );
    cout << "line " << lres << " station " << n << endl;

}

这里是迭代输出的,当时教c语言的老湿真的是极力的反对我们用迭代,所有的迭代都可以用顺序语句实现,可是现在看算法这块好多将迭代的,感觉思维有点跟不上,困惑。

然后写了个顺序执行的:

void PrintWay(int l[], int lres, int offset, int n)
{
    if(offset == 6)
    {
        cout << "line " <<  2 << " station" << n << endl;
    }
    else
    {
        cout << "line " << 1 << " station" << n << endl;
    }
    for(int i = n-1; i> 0; --i)
    {
        cout << "line " << l[i + offset] << " station" << i << endl;
        if(l[i+offset] == 1)
            offset = 0;
        else
            offset = 6;
    }
    cout << "test" ;
}

代码有点乱,新手。。。

时间: 2024-07-31 14:32:51

algorithm ch15 FastWay的相关文章

PLA Percentron Learning Algorithm #台大 Machine learning #

Percentron Learning Algorithm 于垃圾邮件的鉴别 这里肯定会预先给定一个关于垃圾邮件词汇的集合(keyword set),然后根据四组不通过的输入样本里面垃圾词汇出现的频率来鉴别是否是垃圾邮件.系统输出+1判定为垃圾邮件,否则不是.这里答案是第二组. 拿二维数据来做例子.我们要选取一条线来划分红色的叉叉,和蓝色的圈圈样本点(线性划分).怎么做呢?这里的困难之处就在于,其实可行的解可能存在无数条直线可以划分这些样本点.很难全部求解,或许实际生活中并不需要全部求解.于是,

STL algorithm算法is_partitioned(26)

is_partitioned原型: std::is_partitioned template <class InputIterator, class UnaryPredicate> bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred); 测试范围内的元素是否是以pred为准则的一个划分.如果是,则返回true,否则返回false. 划分的意思是说,对每个元素进行pred(*it),得

支付宝支付php的demo或sdk报错 Warning: openssl_sign() [function.openssl-sign]: Unknown signature algorithm. in

最近在做支付宝支付,在本地测试一切正常,上传到服务器就遇到报错: Warning: openssl_sign() [function.openssl-sign]: Unknown signature algorithm. in 后来查了查,是我的服务器上PHP环境支持openssl_sign()但却不支持 OPENSSL_ALGO_SHA256这样的参数,问了一下大佬,才发现这个参数是在php5.4.8以上版本才支持,低版本的是使用的SHA256,于是乎试了一下,搞定! 报错原因是支付宝的dem

Berlekamp-Massey Algorithm [for Team Problem 5525]

Input: 第一行为两个正整数n,m 第二行为n个整数a1..an 最后一行为一个正整数k Output: 为一个整数,代表方案数对1000000007取模的值 Sample Input 5 3 1 1 2 0 2 2 Sample Output 3 来自毛爷爷17年论文 Berlekamp-Massey Algorithm直接开算 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const

Strassen algorithm(O(n^lg7))

Let A, B be two square matrices over a ring R. We want to calculate the matrix product C as {\displaystyle \mathbf {C} =\mathbf {A} \mathbf {B} \qquad \mathbf {A} ,\mathbf {B} ,\mathbf {C} \in R^{2^{n}\times 2^{n}}} If the matrices A, B are not of ty

LabelRank(A Stabilized Label Propagation Algorithm for Community Detection in Networks)非重叠社区发现

最近在研究基于标签传播的社区分类,LabelRank算法基于标签传播和马尔科夫随机游走思路上改装的算法,引用率较高,打算将代码实现,便于加深理解. 一.概念 相关概念不再累述,详情见前两篇文章 二.算法思路 (1)Propagation (2)Inflation (3)Cut off (4)Explicit Conditional Update (5)Stop Criterion 三.A Stabilized Label Propagation Algorithm for Community D

(转)常用算法(Algorithm)的用法介绍

2算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. 2<algorithm>是所有STL头文件中最大的一个,其中常用到的功能范围涉及到比较.交换.查找.遍历操作.复制.修改.反转.排序.合并等等. 2<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数,包括加法和乘法在序列上的一些操作. 2<functional>中则定义了一些模板类,用以声明函数对象. 2STL提供

hihocoder1198 Memory Allocating Algorithm(链表~)

题意: 小Hi和小Ho最近在研究内存分配的机制,他们写了一个比较简单的内存.内存可以表示成M个连续的存储空间,下标为0..M-1: 每当有数据写入时,内存分配程序会从下标0开始向右找一块足够存放下该数据的区域,将该数据写入.比如写入一个长度为2的数据,因为是第一个数据,我们用1来表示: 之后继续依次写入长度为3的数据和长度为2的数据,则有: 当数据足够多后,我们可能会遇到剩下的空间不足以写下新的数据.这时内存程序会从最早的数据开始进行删除.假设我们现在写到第8个数据把内存写满了: 这时我们需要写

Algorithm - Introduction

Goal: Use Computer to solve problems step by step!!! What is Computer Science? Computer Science is the study of problems, problem-solving, and the solutions that come out of the problem-solving process. What is Programming? Programming is the process