Algorithmic Learning-Dynamic programming (DP problem) (c + +)

Source: Internet
Author: User

These days have been looking again, feel to understand some, first write down.

Dynamic planning

Dynamic programming is a direction of operational research, which is to decompose multistage optimization problems into a series of single-order problems. In the process of increasing, the optimal solution of the current problem is constantly calculated.

Generally divided into the following four parts:

    • Linear motion regulation: intercepting missiles, chorus formations, digging mines, building schools, swordsman duels, etc.;
    • Regional dynamic regulation: Stone merge, add two fork tree, count the number of words, artillery array, etc.
    • Tree action: Greedy Nine Dragons, two-point search tree, party joy, digital triangles, etc.
    • Knapsack problem: 01 knapsack problem, complete knapsack problem, group knapsack problem, two-dimensional backpack, boxing problem, milking milk (1132th of Tongji ACM), etc.
Automotive Production line Problems

This question is an example of the dynamic programming of the introduction to the algorithm, which I think is relatively simple and typical, so I will explain this topic:

Colonel Motor Company produces cars in factories with two assembly lines. There are N assembly stations on each assembly line,
The assembly stations in the same position on both lines have the same function, but the required time is different, and the car chassis is in two
Transfer between assembly lines takes a certain amount of time. As shown in the two production lines.

First we know that the shortest time of each stage contains the shortest time of the previous phase.

The simple thing is, we only have two cases, one is short on assembly line 1, one is short on assembly line 2.

If violent search, greed to calculate the words (that is, recursive approach). The complexity of time is 2^n, which is unacceptable.

This time our approach is to start with the first question, assembly line 1, 2 on the station. So simple, a comparison is better. When there are two station, it is from the results of the previous comparison (a line 1 shortest, a line 2 shortest) continue to add the current station values continue to compare.

So we find that each current optimal solution contains the last optimal solution.

Code

The code is the best solution for the current problem from the very beginning, and then the next one.

////Main.cpp//Dp_line////Created by Alps on 15/4/26.//Copyright (c) 2015 Chen. All rights reserved.//#include <iostream>using namespace STD;#define NUM 5intMain () {intFirst[num];the shortest path length to the 1,i of the equipment station    intSecond[num];//2,i Shortest path length to assembly station    intLinef[num];//path entered from 1    intLines[num];//path entered from 2    intA[num];//The time required to 1,i at the Assembly station    intB[num];//reassembly station 2,i the time required to stay    intM[num];//Time from Assembly station 1,I-1 to Assembly station 2,i    intN[num];//Time from Assembly station 2,I-1 to Assembly station 1,i    intLine[num];//The current shortest circuit through the assembly station    intF[num];//Current Shortest path length    intEA,EB,XA,XB;//EA for entering the Assembly station 1 time, EB for entering 2 of the time, XA for the assembly Station 1 of the time, xb for out of the Assembly station 2    scanf("%d %d%d%d", &AMP;EA,&AMP;EB,&AMP;XA,&AMP;XB); for(intI=0; i<num;++i) {scanf("%d%d", &a[i], &b[i]); } first[0] = ea + a[0]; second[0] = EB + b[0]; for(intI=0; i<num-1; ++i) {scanf("%d%d", &m[i], &n[i]); } for(intI=1; i<num;++i) {if(first[i-1] + A[i] < second[i-1] + m[i-1] + A[i]) {First[i] = first[i-1] + a[i]; Linef[i] =1; }Else{First[i] = second[i-1] + m[i-1] + a[i]; Linef[i] =2; }if(second[i-1] + B[i] < first[i-1] + n[i-1] + B[i]) {Second[i] = second[i-1] + b[i]; Lines[i] =2; }Else{Second[i] = first[i-1] + n[i-1] + b[i-1]; Lines[i] =1; }    } for(intI=0; i<num;++i) {if(First[i] + xa < second[i] + XB)            {F[i] = first[i] + xa; Line[i] =1; }Else{F[i] = Second[i] + XB; Line[i] =2; }    } for(intI=0; i<num;++i) {printf("Station%d\n", Line[i]); }printf("Distince is%d\n", f[num-1]);return 0;}

This code is relatively simple, the essence is the for loop.

The test cases are as follows:

3 2 3 44 33 66 32 35 22 32 43 44 3

If you have any suggestions, please feel free to comment. Learn from each other. Thank you.

Algorithmic Learning-Dynamic programming (DP problem) (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.