Mountain Road UVA - 12222

中规中矩的一道动态规划的题目,dp[i][j][k]表示A地车的已经走了i辆,B地车已经走了j辆,并且目前当k=0时,表示最后的一辆车是停在B地,k=1的时候表示最后的一辆车是停在A地,如此的情况之下所花费的时间。那么 就仿照背包问题从i=0到amountA以及j=0到amountB,逐步推测并且寻找最小的值,注意更新每次从某一个地方单向发车的其实时间以及到达时间(加10),并且最终的结果再取最小值,具体实现见如下的代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;

int dp[210][210][2];//1:目前在A点  0:目前在B点 
const int Inf = 0x3f3f3f3f;

class car{
public:
	int start, dur;
};

class Solve{
public:
	int amount;
	int amountA, amountB;
	car carA[210],carB[210];

	void Init(){
		amountA = 0;
		amountB = 0;
		for (int i = 0; i < amount; i++){
			char c;
			cin >> c;
			if (c == 'B'){
				amountA++;
				cin >> carA[amountA].start >> carA[amountA].dur;
			}
			else{//c=='A'
				amountB++;
				cin >> carB[amountB].start >> carB[amountB].dur;
			}
		}
		memset(dp,Inf,sizeof(dp));
	}

	void Deal(){//amountA是A地车的数量  amountB是B地车的数量
		Init();
		dp[0][0][0] = 0;
		dp[0][0][1] = 0;
		for (int i = 0; i <= amountA; i++){
			for (int j = 0; j <= amountB; j++){
				int start = dp[i][j][0], end = 0;
				for (int k = j + 1; k <= amountB; k++){
					start = max(start, carB[k].start);
					end = max(start + carB[k].dur, end);
					dp[i][k][1] = min(dp[i][k][1], end);
					end += 10;
					start += 10;
				}
				start = dp[i][j][1], end = 0;
				for (int k = i + 1; k <= amountA; k++){
					start = max(start, carA[k].start);
					end = max(start + carA[k].dur, end);
					dp[k][j][0] = min(dp[k][j][0], end);
					start += 10;
					end += 10;
				}
			}
		}
		cout << min(dp[amountA][amountB][0], dp[amountA][amountB][1]) << endl;
	}
};

int main(){
	Solve a;
	int c;
	cin >> c;
	while (c--){
		cin >> a.amount;
		a.Deal();
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值