现在的位置: 首页 > 综合 > 正文

杭电2851 简单dp

2013年10月27日 ⁄ 综合 ⁄ 共 2145字 ⁄ 字号 评论关闭

         这道题说白了就是一道水题,就是题意不太好理解。题意:给你多个区间,每个区间有一个危险值,接下来有多次询问,求从第一个区间到第n个区间的最小危险值。

就是一道简单的dp,题目:

Lode Runner

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


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
 

ac代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <climits>
using namespace std;
struct road{
  int beginpos;
  int endpos;
  int danlev;
}ss[2012];
int main(){
	//freopen("1.txt","r",stdin);
  int ncase,dp[2012];;
  scanf("%d",&ncase);
  while(ncase--){
    int numroad,numask;
	memset(dp,0,sizeof(dp));
	scanf("%d%d",&numroad,&numask);
	for(int i=1;i<=numroad;++i)
		scanf("%d%d%d",&ss[i].beginpos,&ss[i].endpos,&ss[i].danlev);
	dp[1]=ss[1].danlev;
	for(int i=2;i<=numroad;++i){
		int min=INT_MAX;
		for(int j=1;j<i;++j){
			if(ss[i].beginpos<=ss[j].endpos&&dp[j]!=-1&&min>dp[j])
				min=dp[j];
		}
		if(min<INT_MAX)
		   dp[i]=min+ss[i].danlev;
		else
			dp[i]=-1;
	}
	int ask;
	while(numask--){
	  scanf("%d",&ask);
	  //if(dp[ask]==0)
		 // printf("-1\n");
	  //else
	     printf("%d\n",dp[ask]);
	}
  }
  return 0;
}

抱歉!评论已关闭.