poj1040

参考:http://blog.csdn.net/dreamvyps/article/details/6069844

菜鸟,一直对DFS很困惑。。。。

测试数据:http://poj.org/showmessage?message_id=17837

不要忘了处理订单数为0的情况

 

//Problem: 1040 
//Memory: 224K  Time: 438MS 
//Language: C++  Result: Accepted 

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

const unsigned MaxOrder = 22;
const unsigned MaxStop = 7;

typedef struct _ORDER
{
	int from;
	int to;
	int passengerCnt;
	int value;//该order可以得到的价值
}Order;

vector<Order> order;
int stop[MaxStop];
int capacity,station,ordernum;
int maxV;

inline bool cmp(const Order &a, const Order &b)//按照每个order的value进行降序排序
{
	return a.value>b.value;
}
void Dfs(int s, int curMaxV)
{
	if(curMaxV>maxV)
		maxV = curMaxV;
	if(s>=order.size())
		return;
	int i, j;
	for(i=s; i<order.size(); ++i)
	{
		for(j=order[i].from; j<order[i].to; ++j)
		{
			stop[j] += order[i].passengerCnt;
			if(stop[j]>capacity)
				break;
		}
		if(j==order[i].to)//还有空余座位
		{
			Dfs(i+1,curMaxV+order[i].value);
			--j;
		}

		//回溯
		for(int k=j; k>=order[i].from; --k)
			stop[k] -= order[i].passengerCnt;
	}
}
int main()
{
	Order tmp;
	int i;

	cin>>capacity>>station>>ordernum;
	while(capacity!=0)
	{
		if(ordernum==0)//处理order为0的情况
		{
			cout<<0<<endl;
		}
		else
		{
			memset(stop,0,sizeof(stop));
			for(i=0; i<ordernum; ++i)
			{
				cin>>tmp.from>>tmp.to>>tmp.passengerCnt;
				if(tmp.passengerCnt<=capacity)//如果单张订单的乘客数大于容量则直接忽略该订单
				{
					tmp.value = (tmp.to-tmp.from)*tmp.passengerCnt;
					order.push_back(tmp);
				}
			}
			sort(order.begin(),order.end(),cmp);//将所有的order按照其提供的价值进行降序排序
			maxV = 0;
			Dfs(0,0);
			cout<<maxV<<endl;
			order.clear();
		}
		cin>>capacity>>station>>ordernum;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值