24h購物| | PChome| 登入
2013-07-22 17:19:34| 人氣654| 回應0 | 上一篇 | 下一篇

[UVA][搜索] 301 - Transportation

推薦 0 收藏 0 轉貼0 訂閱站台


 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

網路上已經有中文題目,在此就不說明了。

題目解法:
之前一直做錯,原因是在於檢查的思路完全錯誤,

原本是這麼寫的->
一開始是對起站作排序,然後依序窮舉是否有超過人數,以及是否讓它訂票。
由於對於該票的起站,標記一個下站的人數,然後就在該站放人下車
這樣就不用使用區域累計的模擬,在 O(1) 就可以完成,但不料這麼做很危險。

由於錯了很多次,還是改成區域模擬。

#include <stdio.h>
#include <algorithm>
using namespace std;
struct E {
int st, ed, n;
bool operator<(const E &a) const {
return st < a.st;
}
};
int n, m, q;
E D[50];
int cap[10], ret, cost[50];
void dfs(int idx, int earn) {
ret = max(ret, earn);
if(idx == q)
return;
if(earn+cost[idx] <= ret) return;
int i, j = 0;
for(i = D[idx].st; i < D[idx].ed; i++) {
cap[i] += D[idx].n;
if(cap[i] > n) j = 1;
}
if(j == 0) {
dfs(idx+1, earn+(D[idx].ed-D[idx].st)*D[idx].n);
}
for(i = D[idx].st; i < D[idx].ed; i++)
cap[i] -= D[idx].n;
dfs(idx+1, earn);
}
int main() {
int i;
while(scanf("%d %d %d", &n, &m, &q) == 3) {
if(n+m+q == 0) break;
for(i = 0; i < q; i++)
scanf("%d %d %d", &D[i].st, &D[i].ed, &D[i].n);
sort(D, D+q);
ret = 0;
for(i = 0; i < q; i++)
cost[i] = (D[i].ed-D[i].st)*D[i].n;
for(i = q-2; i >= 0; i--)
cost[i] += cost[i+1];
dfs(0, 0);
printf("%d\n", ret);
}
return 0;
}

台長: Morris
人氣(654) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][組合] 10475 - Help the Leaders
此分類上一篇:[UVA][模擬] 380 - Call Forwarding

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文