Tju 1003 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 on-route 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, 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.

Output

The output consists of lines corresponding to the blocks of the input 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

題目分析:列車經過一些站,每個站都會有幾隊人要上站,但是列車的載人數有限,如果空位不足則此隊人都不能上,車票是經過站的數,最後求最大利潤。很容易想到用深搜

但是深搜站,會出現一個站可以上兩隊,如果回身再來一遍則會超時,下面是超時代碼。

#include <stdio.h>
#include <iostream>
using namespace std;
struct{
	int num;
	int sta;
	int end;
	int flag;
}point[30],x;
int car[9];
int sum;
int n,a,t;
int ss(int s_now,int s_end,int c_num,int sum_t)
{
	//printf("%d %d %d %d\n",s_now,s_end,c_num,sum_t);
	c_num+=car[s_now];
	if(s_now==s_end)
	{   if(sum_t>sum)
				sum=sum_t;
		return 0;
	}
	for(int i=1;i<=t;i++)
	{
		if(point[i].sta==s_now&&point[i].num<=c_num&&point[i].end<=s_end&&point[i].flag==0)
		{
			car[point[i].end]+=point[i].num;
			point[i].flag=1;
			ss(s_now,s_end,c_num-point[i].num,sum_t+point[i].num*(point[i].end-point[i].sta));
			car[point[i].end]-=point[i].num;
			point[i].flag=0;
		}
	}
	ss(s_now+1,s_end,c_num,sum_t);
	return 0;
}

int main()
{
	int i,j;
	while(scanf("%d %d %d",&n,&a,&t)&&(n!=0||a!=0||t!=0))
	{
		for(i=1;i<=t;i++)
		{
			scanf("%d %d %d",&point[i].sta,&point[i].end,&point[i].num);
			point[i].flag=0;
		}
		for(i=0;i<9;i++)
		{
			car[i]=0;
		}
		sum=0;
		ss(0,a,n,0);
		printf("%d\n",sum);
	}
	return 0;
	
}
因此要對每對人進行深搜,首先就要排序,按上站順序排,如果一樣則按下站順序排。來自一個大牛的代碼:

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define maxm 23
#define maxn 8
int cap, n, m;
int ans;
int ocount;
int down[maxn];
struct Order{    
	int s, e, p;
}order[maxm];

int max(int a,int b){
	return a>b?a:b;
}
bool operator < (const Order &a, const Order &b){
    if (a.s == b.s)        
		return a.e < b.e;    
	return a.s < b.s;
}

void dfs(int i, int p, int money){   
	if (i == m){      
		ans = max(ans, money);      
		return;    
	} 
	if (i > 0)        
		for (int j = order[i - 1].s + 1; j <= order[i].s; j++)            
			p -= down[j]; 
	if (p + order[i].p <= cap) {       
		down[order[i].e] += order[i].p; 
		dfs(i + 1, p + order[i].p, money + order[i].p * (order[i].e - order[i].s));
		down[order[i].e] -= order[i].p;
	} 
	dfs(i + 1, p, money);
}

int main(){
	int i;
	while(cin>>cap>>n>>m,cap||n||m){
		for(i=0;i<m;i++){   
			scanf("%d%d%d", &order[i].s, &order[i].e, &order[i].p);        
		}
		sort(order,order+m);
		ans = 0;
		dfs(0,0,0);
		printf("%d\n",ans);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章