2016-level algorithm third time on machine-b.bamboo and chocolate Factory

Source: Internet
Author: User

B Bamboo and Chocolate Factory analysis

Three lines of the problem, is still dynamic planning, but the range of switching involved more. Relatively easy to expand to the N line of the way is the three-layer cycle, the outer layer is the K-robot, the two layers represent a switchable pipeline
Core DP statement: cost[i][k] = min (Cost[i][k], cost[j][k-1]+t[j][i]+p[i][k])
You can also make a detailed list of all possible route cuts based on the a question and then find the minimum value.

Notice that the subject is different from the meaning of T in question a.

Pseudo code given at the machine
//array starting from 0Const intmaxx=510; p[3][maxx]; t[3][3]; cost[3][maxx];//DP Bodyn=2; M is the input valuevoidResolveintNintM) {Initialize cost to infinity three lines with initial values corresponding to P-values: cost[i][0] = p[i][0]; fork=1: M-1         forI=0:2             forj =0:2Cost[i][k] = min (cost[i][k], cost[j][k-1]+t[j][i]+p[i][k]); End end end finds the minimum value for the last robot in three lines is the desired}
Code
Const intmaxx=510;intp[3][maxx];intt[3][3];intcost[3][maxx];Const intMAX = (1<< -);voidResolve (intNintm) {intI,j; for(i=0; i<n; i++) for(j =0; j<m; J + +) cost[i][j] = MAX; for(intI=0; i<n; i++) cost[i][0] = p[i][0]; for(intK =1; k<m; k++) for(inti =0; i<n; i++) for(intj =0; j<n; J + +) {Cost[i][k] = min (cost[i][k], cost[j][k-1]+t[j][i]+p[i][k]); }intAns =500000000; for(i =0; i<n; i++) {if(cost[i][m-1]<ans) ans = cost[i][m-1]; } printf ("%d\n", ans);}intMain () {intM while(SCANF ("%d", &m)!=eof) {intI,j; for(i =0; i<3; i++) for(j =0; j<m; J + +) scanf ("%d", &p[i][j]); for(i=0; i<3; i++) for(j =0; j<3; J + +) scanf ("%d", &t[i][j]); Resolve (3, m); }}

2016-level algorithm third time on machine-b.bamboo and chocolate Factory

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.