(简单) 搜索 HOJ 1044 Transportation

来源:互联网 发布:网络购票岫岩到沈阳 编辑:程序博客网 时间:2024/06/05 08:49

Transportation

My Tags  (Edit)
Source : ACM ICPC Central European Regional 1995Time limit : 5 secMemory limit : 32 M

Submitted : 269, Accepted : 148

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 is divided into blocks. The first line in each block contains three integers: passenger capacity n of the train (n > 0), 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 a maximum of 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.

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.

Example

Input file:
10 3 40 2 11 3 51 2 72 3 1010 5 43 5 102 4 90 2 52 5 80 0 0
Output file:
1934
题意:一辆汽车沿途会经过一些车站,他可以选择让一组人全部上车或者不让他们上车,求到终点后,最多能赚多少钱 (每位乘客的需要给的费用为途径车站包括下车,不包括上车的站的站的数量)
思路: 就是搜索嘛,因为最多有22组人,所以可用二进制来表示有没有选改组,在深搜的时候,要检查当前站有没有人下车,剪枝的条件是如果当前所得的钱加上所有剩下最多能得到的钱都不能比目前的ans大的话,就没有必要再递归下去了

代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define MAX 1000+10
#define MOD 100000000
const int inf = 0x7fffffff;

int V,m,n;
struct Station
{
int start;
int target;
int person;
int earn;
}station[25];

bool cmp(const Station& s1,const Station& s2)
{
if (s1.start==s2.start) return s1.target<s2.target;
return s1.start<s2.start;
}

int ans;
void dfs(int cur,int choose,int state,int cap,int earn,int rest)
{
if (earn+rest<=ans) return;
if (cur>=n) 
{
if (ans<earn) ans = earn;
return;
}
for (int i = 0 ; i < choose ; ++i) if (state&(1<<i) && station[i].target==cur)
{
cap += station[i].person;
state -= (1<<i);
}
if (choose < m && cur==station[choose].start)
{
if (cap-station[choose].person>=0) 
dfs(cur,choose+1,state+(1<<choose),cap-station[choose].person,earn+station[choose].earn,rest-station[choose].earn);
dfs(cur,choose+1,state,cap,earn,rest-station[choose].earn);
}
else if (cur<station[choose].start || choose>=m)
dfs(cur+1,choose,state,cap,earn,rest);
}

int main()
{
while (scanf("%d%d%d",&V,&n,&m)==3)
{
if (V+n+m==0) return 0;
int sum = 0;
for (int i = 0 ; i < m ; ++i)
{
scanf("%d%d%d",&station[i].start,&station[i].target,&station[i].person);
station[i].earn = (station[i].target-station[i].start)*station[i].person;
sum += station[i].earn;
}
sort(station,station+m,cmp);
ans = 0;
dfs(0,0,0,V,0,sum);
printf("%d\n",ans);
}
}
0 0
原创粉丝点击