NYOJ 304 Energy Saving [DP Good question]

Source: Internet
Author: User
Tags min time limit
Energy Saving

Click to open the topic


Time limit: Ms | Memory limit: 65535 KB Difficulty: 5 description

Dr.kong designed robots are getting smarter. Recently, the municipal company handed over a task to the city, starting 5:00 every day, it was responsible for shutting down all the street lights on the right side of ZK Avenue.

Every 5:00, he will be next to a street lamp on ZK Avenue, then he starts to turn off the light. Each lamp has a certain power, the robot has a conscious energy-saving consciousness, it hopes that during the turn off the lights, the right side of the Zk road, the total amount of electricity consumption is the least.

The robot is walking at 1m/s speed. Let's say it doesn't take extra time to turn off the light, because it turns the light off when it passes through a lamp.

Please write a program that calculates the minimum energy consumed by all lights on the ZK Avenue during the time of the closing of the lights, given the lamp setting, the lamp power and the starting position of the robot. Enter a flag with multiple sets of test data, ending with EOF as input
First line of test data per group: N indicates the number of street lights on the ZK Avenue (2≤n≤1000)
The second line: V means the street lamp number where the robot is starting to turn off the lights. (1≤v≤n)
In the next n rows, each row contains two integers d and w separated by spaces to describe the parameters of each lamp

D indicates the distance between the street lamp and the beginning of ZK Avenue (expressed in meters),
W represents the power of the bulb, which is the amount of energy consumed by the bulb per second. Streetlights are given in order.
(0≤d≤1000, 0≤w≤1000) outputs an integer that is the minimum value of the sum of energy consumed. Note results are less than 200,000,000 sample input

4 32 25 86 18 7
Sample output
56
Source

Fourth session of Henan Province Program design Competition

Problem Solving Ideas:

The subject is a dynamic programming topic, the robot turn off the lights either to the left to turn off the lights, or to the right to turn off the lights, that is, to close the next street light is either from the left side of the closed section of the past, or from the closed section of the right side of the past, definition:

Dp[i][j][0] indicates that I to J street lights have been closed, the robot in the position of the street lamp I, at this time has been consumed by the minimum energy

DP[I][J][1] said that I to J street lights have been closed, the robot in the position of the street lamp J, at this time has been consumed by the minimum energy

Then state-shifted:

Dp[i][j][0] = min (dp[i+1][j][0]+[i+1,j] Road outside of the street lights are not closed in the robot from the i+1 I spent energy, DP[I+1][J][1]+[I+1,J] The electric energy consumed by the robot from J to I during the road is not closed.

Dp[i][j][1] = min (dp[i][j-1][0]+[i,j-1) Road not closed the electric energy consumed by the robot from I walk to J, Dp[i][j-1][1]+[i,j-1] The electric energy consumed by the robot from J-1 to J after the road is not closed.

(Note: The I,j interval in DP is not the distance, but the number of lights, such as the first I light to the first J lamp)


AC Code:

#include <bits/stdc++.h> using namespace std;
const int MAX=1E3+10;

const int inf=0x3f3f3f3f; struct {int d,w;}

Node[max]; int dp[max][max][2];//dp[i][j][0] means I to J street lights are closed, the robot in the position of the street light I, at this time has been consumed by the minimum energy//dp[i][j][1] said I to J street lights are closed, the robot in the position of the street light J,
	The minimum energy int bw[max][max];//that has been consumed at this time stores the total power consumption of street lamps from I to J; int main () {int n;
		while (cin>>n) {int V;
		cin>>v;
			int tw=0;//Record The power of all lights for (int i=1;i<=n;i++) {scanf ("%d%d", &AMP;NODE[I].D,&AMP;NODE[I].W);
		TW+=NODE[I].W;
		
		} memset (Bw,0,sizeof (BW));
			Initializes the for (int i=1;i<=n;i++) {for (int j=i;j<=n;j++) {BW[I][J]=BW[I][J-1]+NODE[J].W;
			}} for (int i=v-1;i>0;i--) {dp[i][v][0]=dp[i+1][v][0]+ (Tw-bw[i+1][v]) * (NODE[I+1].D-NODE[I].D);
		dp[i][v][1]=dp[i][v][0]+ (Tw-bw[i][v]) * (NODE[V].D-NODE[I].D);
			} for (int i=v+1;i<=n;i++) {dp[v][i][1]=dp[v][i-1][1]+ (tw-bw[v][i-1]) * (NODE[I].D-NODE[I-1].D);
		dp[v][i][0]=dp[v][i][1]+ (Tw-bw[v][i]) * (NODE[I].D-NODE[V].D); }//DP State Conversion for (int i=v-1;i>0;i--) {for (int j=v+1;j<=n;j++) {dp[i][j][0]=min (dp[i+1][j][0]+ (Tw-bw[i+1][j]) * (NODE[I+1].D-NODE[I].D),
				dp[i+1][j][1]+ (Tw-bw[i+1][j]) * (NODE[J].D-NODE[I].D)); Dp[i][j][1]=min (dp[i][j-1][1]+ (tw-bw[i][j-1]) * (NODE[J].D-NODE[J-1].D), dp[i][j-1][0]+ (Tw-bw[i][j-1]) * (Node[j].
			D-NODE[I].D));
	}} cout<<min (Dp[1][n][0],dp[1][n][1]) <<endl;
} return 0;  }



Related Article

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.