nyoj304 Energy Saving

Source: Internet
Author: User

Energy saving time limit: Ms | Memory limit: 65535 KB Difficulty: 5
Describe

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.

Input
flag with multiple sets of test data, with EOF as input end
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)
Output
outputs an integer, which 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.


AC Code:

1#include <algorithm>2#include <cstdio>3#include <iostream>4#include <cstring>5 using namespacestd;6 Const intN =1010;7 intdp[n][n][2],dw[n][n],st[n],co[n];8 intMain ()9 {Ten     intn,s,v,sum; One      while(SCANF ("%d", &n)! =EOF) A     { -memset (DW,0,sizeof(DW)); -sum =0; thescanf"%d",&v); -          for(inti =1; i<=n; i++) -         { -scanf"%d%d",&st[i],&co[i]); +Sum =sum+Co[i]; -         } +          for(intI=1; i<=n;i++) A              for(intj=i;j<=n;j++) atDW[I][J] = dw[i][j-1]+Co[j]; -          for(inti = V1; I>0; i--) -         { -dp[i][v][0] = dp[i+1][v][0]+ (sum-dw[i+1][V]) * (st[i+1]-st[i]); -dp[i][v][1] = dp[i][v][0] + (Sum-dw[i][v]) * (st[v]-st[i]); -         } in          for(intJ =v+1; j<=n; J + +) -         { todp[v][j][1] =dp[v][j-1][1]+ (sum-dw[v][j-1]) * (st[j]-st[j-1]); +dp[v][j][0] =dp[v][j][1]+ (Sum-dw[v][j]) * (st[j]-st[v]); -         } the  *          for(intI =v-1;i>0; i--) $         {Panax Notoginseng              for(intJ =v+1; j<=n; J + +) -             { thedp[i][j][0] = min (dp[i+1][j][0]+ (sum-dw[i+1][J]) * (st[i+1]-St[i]), +dp[i+1][j][1]+ (sum-dw[i+1][J]) * (st[j]-st[i])); Adp[i][j][1] = min (dp[i][j-1][0]+ (sum-dw[i][j-1]) * (st[j]-St[i]), thedp[i][j-1][1]+ (sum-dw[i][j-1]) * (st[j]-st[j-1])); +             } -         } $  $printf"%d\n", Min (dp[1][n][0], dp[1][n][1])); -     } -     return 0; the}

nyoj304 Energy Saving

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.