uva301重做,一次ac!

好久没做算法题了,闲来无事,就把过去没ac的题拿出来做做,没看原来的代码,一次ac。现在看看原来的代码,发现思路太窄了。当时肯定做了很久,这东西有时候就得跳出来换个思路再来。

附上ac代码:

#include<iostream>
#include<cstring>
using namespace std;

int n,m,ord,maxe=0,earning=0;
int order[25][3];

void dfs(int trans[],int h){
	// went through all orders and count total earnings
	if (h==ord){
		earning = 0;
		for (int i=0;i<m;i++){
			earning += trans[i];
		}
		if (earning>maxe) maxe=earning;
		return;
	}
	// accept this order
	bool ok = true;
	int tmptrans[7];
	memcpy(tmptrans,trans,sizeof(tmptrans));
	for (int i=order[h][0];i<order[h][1];i++){
		tmptrans[i] += order[h][2];
		if (tmptrans[i]>n){
			// this order cannot be accepted
			ok = false;
			break;
		}
	}
	if (ok) dfs(tmptrans,h+1);
	// reject order h
	dfs(trans,h+1);
	
}
int main(){
	while(cin>>n>>m>>ord&&(n||m||ord)){
		maxe=earning=0;
		memset(order,0,sizeof(order));
		for (int i=0;i<ord;i++){
			cin>>order[i][0]>>order[i][1]>>order[i][2];

		}
		int t[7];
		memset(t,0,sizeof(t));
		dfs(t,0);
		cout<<maxe<<endl;

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值