程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Transportation poj1040

Transportation poj1040

編輯:關於C++

 

Ruratania is just entering capitalism and is establishing new enterprising activities in many fields in- cluding 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

 

 

 

(1)題意:火車運輸,n個車站編號0-(n-1),之間有很多訂單,問你,最大收益是多少?火車的載客量一定,車上的人不能超過這個數,先給你3個數,火車載客量m、車站數n、訂單數p。然後p行數據,每行3個數,分別代表訂單的起點、終點和人數。

(2)解法:先對訂單進行排序,按起點站先後排,若一樣,按終點排,然後對訂單進行深度搜索。剪枝:遇到人數超過載客量的時候,就返回。


 

#include
#include
#include
#include
#include
using namespace std;
struct Rac{
int start;
int end1;
int num;
}p[30];


int lode,station,order;
int maxmoney;
int people[30]={0}; //表示到i站的人數,,people[2]表示站1到站二的人數


void DFS(int ding,int money) //第幾訂單,錢;
{
if(ding==order)
{
maxmoney=max(maxmoney,money);
return ;
}
int i,j,flag=1;
for(i=p[ding].start+1;i<=p[ding].end1;i++) //判斷人人數是否超了
{
if(people[i]+p[ding].num>lode)
{
flag=0;
break;
}
}


if(flag==1) //第ding條訂單符合條件~~
{
for(i=p[ding].start+1;i<=p[ding].end1;i++) people[i]=people[i]+p[ding].num; //start到end站都加上該訂單人數
DFS(ding+1,money+(p[ding].end1-p[ding].start)*p[ding].num);
for(i=p[ding].start+1;i<=p[ding].end1;i++) people[i]=people[i]-p[ding].num;//恢復
}


DFS(ding+1,money); //不要該訂單;
}
bool cmp(struct Rac a,struct Rac b)
{
if(a.start!=b.start) return a.start else return a.end1 }
int main()
{
//freopen(test.txt,r,stdin);
while(scanf(%d %d %d,&lode,&station,&order),lode!=0||station!=0||order!=0)
{
int i;
for(i=0;i sort(p,p+order,cmp);
// for(i=0;i

memset(people,0,sizeof(people));
maxmoney=0;
DFS(0,0);
printf(%d ,maxmoney);
}
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved