首页 > 代码库 > uva 301 Transportation(回溯)
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 numberm. The company runs an experiment in order to improve passenger transportation capacity and thus to increase its earnings. The train has a maximum capacityn 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 capacityn 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; }
uva 301 Transportation(回溯)