uva 301 Transportation(回溯)

uva 301 Transportation

Ruratania is just entering capitalism and is establishing new enterprising activities in many fields including transport. The transportation company TransRuratania is starting a new express train from city A to city B with several stops in the stations on
the way. The stations are successively numbered, city A station has number 0, city B station number
m. The company runs an experiment in order to improve passenger transportation capacity and thus to increase its earnings. The train has a maximum capacity
n passengers. The price of the train ticket is equal to the number of stops (stations) between the starting station and the destination station (including the destination station). Before the train starts its route from the city A, ticket orders are
collected from all onroute stations. The ticket order from the station S means all reservations of tickets from S to a fixed destination station. In case the company cannot accept all orders because of the passenger capacity limitations, its rejection policy
is that it either completely accept or completely reject single orders from single stations.

Write a program which for the given list of orders from single stations on the way from A to B determines the biggest possible total earning of the TransRuratania company. The earning from one accepted order is the product of the number of passengers included
in the order and the price of their train tickets. The total earning is the sum of the earnings from all accepted orders.

Input

The input file is divided into blocks. The first line in each block contains three integers: passenger capacity
n of the train, the number of the city B station and the number of ticket orders from all stations. The next lines contain the ticket orders. Each ticket order consists of three integers: starting station, destination station, number of passengers.
In one block there can be maximum 22 orders. The number of the city B station will be at most 7. The block where all three numbers in the first line are equal to zero denotes the end of the input file.

Output

The output file consists of lines corresponding to the blocks of the input file except the terminating block. Each such line contains the biggest possible total earning.

Sample Input

10 3 4
0 2 1
1 3 5
1 2 7
2 3 10
10 5 4
3 5 10
2 4 9
0 2 5
2 5 8
0 0 0

Sample Output

19
34

题目大意:有一家运输公司, 运营一段铁路, 该铁路A 站到B站。 从A站开始到B站编号为0....N-1。每辆火车的限载人数为n人, 车票的价钱按站数计算,搭一个站收1元, n个站即n元。为了让收益最大化, 每次开车前,都会先分析所有的车票,相同起点和终点的归为同一个订单。因为人数限制,如果人数太多的话,就必须要放弃一些订单。
这个公司的做法有点极端, 要么整个订单都放弃,要么整个订单都接受。编写一个程序,输出最大能收入多少钱。

解题思路:定义一个people数组记录每个站点的人数,来判断当前订单是否会使货车超载。利用回溯获取最高利润。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int peo, sta, n, people[30];
struct ticket {
	int begin, last, man;
};
ticket t[30];
int ans;
void DFS(int cnt) {
	if (cnt == n) { //订单遍历结束,找出最大利润
		int sum = 0;
		for (int i = 0; i < sta; i++) {
			sum += people[i];
		}
		if (ans < sum) ans = sum;
	}
	else {
		int flag = 1;
		for (int i = t[cnt].begin; i < t[cnt].last; i++) { //判断该订单是否会使列车超载
			if (people[i] + t[cnt].man > peo)	flag = 0;
		}
		if (flag) {
			for (int i = t[cnt].begin; i < t[cnt].last; i++) {
				people[i] += t[cnt].man;
			}
			DFS(cnt + 1); //接受该份订单
			for (int i = t[cnt].begin; i < t[cnt].last; i++) {
				people[i] -= t[cnt].man;
			}
		}
		DFS(cnt + 1); //拒绝该份订单
	}
}
int main() {
	while (scanf("%d %d %d", &peo, &sta, &n) == 3, peo || sta || n) {
		memset(people, 0, sizeof(people));
		for (int i = 0; i < n; i++) {
			scanf("%d %d %d", &t[i].begin, &t[i].last, &t[i].man);
		}
		ans = 0;
		DFS(0);
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-08-01 13:58:03

uva 301 Transportation(回溯)的相关文章

uva 1486 Transportation (最大流+拆边)

uva 1486 Transportation Description There are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry,

UVA 1486 - Transportation(网络流+拆边)

UVA 1486 - Transportation 题目链接 题意:一个有向图上运输k货物,有一些边,每个边一个系数a,经过该边如果有x货物,就要缴纳a x x的钱,问运输从1到n最小的代价 思路:费用流,这题边的容量c最大只有5,所以可以拆边,一条边拆成c条边,每条容量1,对应相应的代价为a * (i^2 - (i - 1)^2),然后跑一下费用流即可 代码: #include <cstdio> #include <cstring> #include <vector>

UVA - 301

 Transportation  Ruratania is just entering capitalism and is establishing new enterprising activities in many fields including transport. The transportation company TransRuratania is starting a new express train from city A to city B with several st

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

uva 165 Stamps (回溯)

uva 165 Stamps The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can derive revenue from them. In terms of recent legislation, each class of document is limited in the number

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

uva 11218 KTV(DFS+回溯)

uva 11218 KTV One song is extremely popular recently, so you and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing together each time (yes, there are 3 microphones in the room). There are exactly 9 peo

uva 639 Don&#39;t Get Rooked (暴力回溯 )

uva 639 Don't Get Rooked In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this problem we will consider small chess boards (at most 44) that can also contain walls through which rooks cannot move. The g

uva 639 Don&#39;t Get Rooked ( 回溯 )

这道题确实是标准的回溯,果然还是早上比较清醒一些,昨天晚上想了好长一会儿都没有想起来,早上一会的功夫就A 了,估计也有昨天晚上的帮助...总感觉不想写太多私人的东西在这上面,因为这个是每个人都可以无条件访问的... 思路: 由于数据比较小,可以把每个元素都遍历一遍,回溯选择,最多4*4,还是很小的,我交的才1ms,1A.. 贴代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int map[10][1