HDU 2851.Lode Runner【DP動態規劃】【5月11】


Lode Runner

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 796    Accepted Submission(s): 394


Problem Description
Lode Runner is a famous game, I think you played in your childhood.


Ok, now, I simple the problem. In the game, it has N horizontal roads, the ladders always stay at right side vertically, and the ladders are extending towards up and down with unlimited length. If ladder near or cross a road, little WisKey will walk on the road by climbed the ladder. Two roads were covered by each other in the X-axis; it will be considered can be passing through. Each road has an integer W means the dangerous level.

Little WisKey must start at No.1 road, and he can’t go back, he always go ahead. WisKey want to go some roads, so he needs to know how minimum sum of dangerous will happen. You can finish the game, yes?
 

Input
The first integer C represents the number of cases, And C cases followed.
  
Each test case contains a single integer N roads (1<=N<= 2000) and M destinations (1<=M<=500). The next N line contains three integers Si, Ei, Wi, meaning the road build from Si to Ei, and the Wi dangerous level (0 <= Si <= Ei <= 1000, 1 <= Wi <= 1000). And the roads sorted by Ei increasing yet. The last M line contains integers mean little WisKey’s destinations.
 

Output
  
For each questions, output the minimum sum of dangerous. If can’t reach the goal, please print -1.
 

Sample Input
3 10 4 1 4 7 5 6 3 3 7 5 2 9 8 10 13 8 12 14 11 11 15 13 16 18 5 17 19 6 8 20 9 1 2 3 10 5 5 1 4 5 3 6 10 5 8 20 2 9 1 7 10 2 1 2 3 4 5 4 4 1 5 1 2 7 20 2 7 3 7 9 4 1 2 3 4
 

Sample Output
7 -1 12 24 5 15 35 6 8 1 21 4 8
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 2005;
int N, M, C, x, MIN;
struct ss
{
    int x, y, v;
    int dp;
}L[MAX];
int main()
{
    scanf("%d", &C);
    while(C--)
    {
        scanf("%d %d", &N, &M);
        scanf("%d %d %d", &L[0].x, &L[0].y, &L[0].v);
        L[0].dp = L[0].v;
        for(int i = 1;i < N; ++i)
        {
            MIN = INF;
            scanf("%d %d %d", &L[i].x, &L[i].y, &L[i].v);
            for(int j = 0;j < i; ++j)
            {
                if(L[j].y >= L[i].x && L[j].y <= L[i].y) MIN = min(MIN, L[j].dp+L[i].v);
            }
            L[i].dp = MIN;
        }
        while(M--)
        {
            scanf("%d", &x);
            if(L[x-1].dp == INF) cout << -1 << endl;
            else cout << L[x-1].dp << endl;
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章