12222 - Mountain Road(dp+贪心)

Problem E
Mountain Road

In the Franconian Switzerland, there is a narrow mountain road. With only a single lane, this is a bottleneck for two-way traffic. Your job is to schedule incoming cars at both ends so that the last car leaves the road as early as possible.

Each car is specified by three values: the direction in which it is going, the arrival time at the corresponding beginning of the road, and the driving time this car needs to get through, provided it is not slowed down by other cars in front. Cars cannot overtake each other on the mountain road, and reordering cars in the queues at the ends of the road is not allowed.

For safety reasons, two successive cars going in the same direction may not pass any point of the road within less than 10 seconds. This ensures that the second car will not crash into the first car if the latter brakes hard. However, if another car passes in the other direction in between, it will be clear that the road is empty, so in this case, this rule does not apply.

Input
The first line of the input consists of a single integer (1 ≤ ≤ 200), the number of test cases.

Then follow the test cases, each beginning with a single line consisting of an integer (1 ≤ ≤ 200), the number of cars you are to consider in this test case. The remainder of each test case consists of nlines, one line per car, starting with a single upper case letter (“A” or “B”), giving the direction in which the car is going. Then follow, on the same line, two integers (0 ≤ ≤ 100000) and (1 ≤ 100000), giving the arrival time at the beginning of the road and the minimum travel time, respectively, both in seconds.

Within a test case, the cars are given in order of increasing arrival time, and no two cars will arrive at the same time.

Output
For each test case, print a single line consisting of the point in time (in seconds) the last car leaves the road when the cars are scheduled optimally.

Sample InputSample Output
2   
4   
A  0  60   
B  19  10   
B  80  20   
A  85  100   
4   
A  0  100   
B  50  100   
A  100  1   
A  170  100
200   
270
 

题意:A,B两点,给定n辆车,A,B为车的目的地,t为发车时间,d为最快的路程花费的时间。由于要确保安全,要满足一下两个条件:

1、不能有反方向的车

2、相同方向的车之间时间不能相差小于10秒。

问所有汽车最小需要的时间。

思路:先是贪心的思想,方向相同放在一起,发车时间小的肯定是先发最优。题目给定的已经是升序了,所以无需排序。

然后是DP,把A和B方向车分开存,dp[i][j][d]代表的是前i辆A车,j辆B车,最后一辆方向为d,需要的最少时间。方向A为0,B为1

这样由dp[i][j][0]这个状态可以往后选x辆B,dp[i][j][1]这个状态可以往后选x辆A,起始时间为dp[i][j] dp[k][j](如果是B为dp[i][k]) = min{time};

time的求法为从起始时间开始确定,每次维护起始时间和到达时间即可,注意由于间隔要大于10,所以最后后面要+10.

代码:

#include <stdio.h>
#include <string.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define INF 0x3f3f3f3f
const int N = 205;

int t, n, an, bn, dp[N][N][2];
struct Car {
	int s, t;
} a[N], b[N];

int solve() {
	dp[0][0][0] = dp[0][0][1] = 0;
	for (int i = 0; i <= an; i++) {
		for (int j = 0; j <= bn; j++) {
			int s = dp[i][j][1], end = 0, k;
			for (k = i + 1; k <= an; k++) {
				s = max(s, a[k].s);
				end = max(s + a[k].t, end);
				dp[k][j][0] = min(dp[k][j][0], end);
				s += 10; end += 10;
			}
			s = dp[i][j][0]; end = 0;
			for (k = j + 1; k <= bn; k++) {
				s = max(s, b[k].s);
				end = max(s + b[k].t, end);
				dp[i][k][1] = min(dp[i][k][1], end);
				s += 10; end += 10;
			}
		}
	}
	return min(dp[an][bn][0], dp[an][bn][1]);
}

int main() {
	scanf("%d", &t);
	while (t--) {
		memset(dp, INF, sizeof(dp));
		an = bn = 0;
		scanf("%d", &n);
		int S, T; char C[2];
		while (n--) {
			scanf("%s%d%d", C, &S, &T);
			if (C[0] == 'A') {
				an++; a[an].s = S; a[an].t = T;
			}
			else {
				bn++; b[bn].s = S; b[bn].t = T;
			}
		}
		printf("%d\n", solve());
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值