hdu6024(dp)

12695 단어 dp
Building Shops HDU’s nn classrooms are on a line ,which can be considered as a number line. Each classroom has a coordinate. Now Little Q wants to build several candy shops in these nn classrooms.
The total cost consists of two parts. Building a candy shop at classroom ii would have some cost cici. For every classroom PP without any candy shop, then the distance between PP and the rightmost classroom with a candy shop on PP’s left side would be included in the cost too. Obviously, if there is a classroom without any candy shop, there must be a candy shop on its left side.
Now Little Q wants to know how to build the candy shops with the minimal cost. Please write a program to help him.
Input The input contains several test cases, no more than 10 test cases.
In each test case, the first line contains an integer n(1≤n≤3000)n(1≤n≤3000), denoting the number of the classrooms.
In the following nn lines, each line contains two integers xi,ci(−109≤xi,ci≤109)xi,ci(−109≤xi,ci≤109), denoting the coordinate of the ii-th classroom and the cost of building a candy shop in it.
There are no two classrooms having same coordinate.
Output For each test case, print a single line containing an integer, denoting the minimal cost.
Sample Input 3 1 2 2 3 3 4 4 1 7 3 1 5 10 6 1
Sample Output 5 11
dp[i][1]는 i번째 교실에서 슈퍼마켓을 열었다는 뜻이고, dp[i][1]=min(dp[i-1][0], dp[i-1][1])+cost[i]dp[i][0]는 i번째 교실에서 슈퍼마켓을 열지 않는다는 뜻이다. 이때 i앞의 모든 교실을 두루 돌아다니며 거리를 계산하고 dp[i][0]의 값을 업데이트한다. 좌표에 따라 순서를 정하는 것을 잊지 마라.
#include 
using namespace std;
const long long inf=1e12;
struct hh
{
    long long x,c;
    bool operator < (hh&a)
    {
        return this->x<a.x;
    }
    hh():x(0),c(0){}
}s[3005];
long long dp[3005][2];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lld%lld",&s[i].x,&s[i].c);
        }
        sort(s,s+n);
        memset(dp,0,sizeof(dp));
        dp[0][0]=inf;
        dp[0][1]=s[0].c;
        for(int i=1;i<n;i++)
        {
            dp[i][1]=min(dp[i-1][0],dp[i-1][1])+s[i].c;
            dp[i][0]=inf;
            long long t=0;
            for(int j=i-1;j>=0;j--)
            {
                t+=(i-j)*(s[j+1].x-s[j].x);
                dp[i][0]=min(dp[i][0],dp[j][1]+t);
            }
        }
        cout<<min(dp[n-1][0],dp[n-1][1])<<'
'
; } return 0; }

좋은 웹페이지 즐겨찾기