uva-301-枚举-组合

题意:从A市到B市有n个站点,限制火车上搭乘的乘客数目,每个站点都从有一些乘车的订单,订单信息 从x到y,乘客m人,求解最大的收入是多少

最多7个站,22个订单

选取订单的时候没有顺序问题,所以不是全排列,是组合问题,一个订单俩种情况,拿OR不拿,所以是2^22次方种组合,

pass数组用于记录拿了这些订单后火车在每个站点时的人数

#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<map>
#include<memory.h>
#include <math.h>
#include<time.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int N = 110;

class Node
{
public:
	int s;
	int e;
	int num;
	Node(int s, int e, int num)
	{
		this->s = s;
		this->e = e;
		this->num = num;
	}
	Node()
	{
		this->s = 0;
		this->e = 0;
		this->num = 0;
	}
};
int limit;
int station;
int orders;
int pass[9];
Node node[25];
int maxSum = -1;

void read(int orders, Node nodes[])
{
	for(int i = 0; i < orders; i++)
	{
		Node node;
		cin >> node.s >> node.e >> node.num;
		nodes[i] = node;
	}

}

void dfs(int cn, int sum)
{
	if(cn == orders)
	{
		maxSum = sum > maxSum ? sum : maxSum;
		return;
	}

	int ok = 1;
	int sn[10];
	memset(sn,0,sizeof(sn));
	for(int i = node[cn].s; i < node[cn].e; i++)
		if(pass[i] + node[cn].num <= limit)
		{
			pass[i] += node[cn].num;
			sn[i] = 1;
		}
		else
		{
			ok = 0;
			break;
		}
	if(ok)
		dfs(cn + 1, (node[cn].e - node[cn].s) * node[cn].num + sum);

	for(int i = node[cn].s; i < node[cn].e; i++)
		if(sn[i])
			pass[i] -= node[cn].num;
	dfs(cn + 1, sum);

}

int main()
{
	freopen("d:\1.txt", "r", stdin);
	while ((cin >> limit >> station >> orders) && limit)
	{
		memset(pass, 0, sizeof(pass));
		maxSum = -1;
		read(orders, node);
		dfs(0,0);
		cout<<maxSum<<endl;

	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/7751251.html