"Dynamic planning" mr351-Visa

Source: Internet
Author: User

"The main topic"

Xuzhenyi to have a visa. The office is a M-storey building, 1<=m<=100.

Each floor has n offices, numbered 1. N (1<=n<=500). There is a visa clerk in each office.

A visa needs to be stamped by a visa officer on level m to be valid.

Each visa officer must meet one of the following three conditions to seal the Xuzhenyi:

1. The visa officer is on the 1 floor.

2. Xuzhenyi's visa has been stamped on the visa officer's downstairs (the room number is the same).

3. Xuzhenyi's visa has been stamped on the visa officer's adjoining room (room number is 1, the floor is the same).

Each visa officer must charge a fee for the stamp, which does not exceed 1000000000.

Find the lowest cost seal route and make the visa effective.

Line 1th of two integers m and N.

The next m line is n integers per row, and the number of J in line I indicates the fee charged by the J Visa officer on Level I.

Output 1th behavior min= minimum cost. From line 2nd, output the number of the room you passed in sequentially, one number per line.

If there are multiple routes with the least cost, output any one.

Sample input to sample

3 4

10 10 1 10

2 2 2 10

1 10 10 10

Sample output Sample Outputs

Min=8

3

3

2

1

1

Ideas

Simple dynamic planning, initialize the first floor of the room is the room visa price, for two floors and above each room, there are three kinds of possibilities:

(1) walk up from the downstairs room, as all rooms mincost array initialization.

(2) walk from the adjoining room on the left, starting from the second room on each floor and scanning from left to right.

(3) walk from the adjoining room on the right, starting from the penultimate room on each floor and scanning from right to left.

Use the path array to record the minimum price for each room, from which room of the floor is passed, and then output in reverse order.

1#include <iostream>2#include <cstdio>3#include <cmath>4 using namespacestd;5 Const intmaxm= -+ -;6 Const intmaxn= -+ -;7 structnode8 {9     intx, y;Ten }; One intM,n; A Long LongCOST[MAXM][MAXN]; - Long LongMINCOST[MAXM][MAXN]; - node PATH[MAXM][MAXN]; the  - voidInit () - { -scanf"%d%d",&m,&n); +      for(intI=0; i<m;i++) -         for(intj=0; j<n;j++) +scanf"%d",&cost[i][j]); A } at  - voidDP () - { -      for(intI=0; i<n;i++)  -     { -mincost[0][i]=cost[0][i]; inpath[0][i].x=-1; -path[0][i].y=-1;  to     }  +      for(intI=1; i<m;i++) -     { the          for(intj=0; j<n;j++)  *         { $mincost[i][j]=mincost[i-1][j]+Cost[i][j];Panax Notoginsengpath[i][j].x=i-1; -path[i][j].y=J; the         } +          for(intj=1; j<n;j++) A             if(mincost[i][j]>mincost[i][j-1]+Cost[i][j]) the             { +mincost[i][j]=mincost[i][j-1]+Cost[i][j]; -path[i][j].x=i; $path[i][j].y=j-1; $             } -          for(intj=n-2; j>=0; j--) -             if(mincost[i][j]>mincost[i][j+1]+Cost[i][j]) the             { -mincost[i][j]=mincost[i][j+1]+Cost[i][j];Wuyipath[i][j].x=i; thepath[i][j].y=j+1; -             } Wu     } - } About  $ voidprint () - { -     intans=0; -      for(intI=1; i<n;i++)if(mincost[m-1][i]<=mincost[m-1][ans]) ans=i; Acout<<"min="<<mincost[m-1][ans]<<Endl; +     intnowx=m-1, nowy=ans; the     intanspath[maxm*maxn],t=-1; -      while(nowx!=-1) $     { thet++; theanspath[t]=Nowy; theNode next=Path[nowx][nowy]; thenowx=Next.x; -nowy=Next.y; in     } the      for(inti=t;i>=0; i--) cout<<anspath[i]+1<<Endl; the } About  the intMain () the { theFreopen ("Mr351.in5","R", stdin); +Freopen ("Mr351.ou5","W", stdout); - init (); the DP ();Bayi print (); the     return 0; the}

"Dynamic planning" mr351-Visa

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.