UVA 301 - Transportation

题目大意:有一列载客量为 N 的火车,有 M 个站点,K 个单子,求在不超载的情况下接哪些单子可以获得最大收益


解题思路:用 DFS 查找出所有可能的情况,记录下收益最大的那个

#include <cstdio>
int orders[25][3], station[10] = {0}, capacity, node, num, max;

int dfs(int count, int sum) {
	if (sum > max)
		max = sum;

	bool ok = true;
	if (count >= num)
		return 0;

	for (int i = orders[count][0]; i < orders[count][1]; i++) {
		station[i] += orders[count][2];
		if (station[i] > capacity) //判断人数是否超载
			ok = false;
	}

	if (ok)
		dfs(count + 1, sum + orders[count][2] * (orders[count][1] - orders[count][0])); //如果人数不超载就接此单

	for (int i = orders[count][0]; i < orders[count][1]; i++)
		station[i] -= orders[count][2];

	dfs(count + 1, sum); //不接此单
}

int main() {
	while (scanf("%d%d%d", &capacity, &node, &num), capacity) {
		max = 0;
		for (int i = 0; i < num; i++)
			scanf("%d%d%d", &orders[i][0], &orders[i][1], &orders[i][2]);
		dfs(0, 0);
		printf("%d\n", max);
	}
	return 0;
}


你可能感兴趣的:(UVA 301 - Transportation)