Transportation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4980 | Accepted: 2042 |
Description
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.
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
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<memory.h>
#include<algorithm>
using namespace std;
struct orders
{
int start;
int end;
int pas;
}ord[23];
int n,numb,numord;
int maxearn,curearn,off[8];//off记录每一站的应下车人数
int comp(const void *aa,const void *bb)
{
struct orders *a=(orders*) aa;
struct orders *b=(orders*) bb;
if(a->start!=b->start) return a->start-b->start;
else return a->end-b->end;
}
//参数num是order的编号,total是车上总人数
void dfs(int num,int total)
{
if(num==numord)//order由零开始编号,故此时已搜索所有order,回溯??
{
if(curearn>maxearn) maxearn=curearn;
return ;
}
else
{
if(num>0)//减去下车人数,注意无论当前order是否accepted,都要执行
for(int i=ord[num-1].start+1;i<=ord[num].start;i++)
{
total-=off[i];
}
total+=ord[num].pas;
if(total<=n)//加上当前order的上车人数不超载就搜索下一张order?
{
curearn+=(ord[num].end-ord[num].start)*ord[num].pas;
off[ord[num].end]+=ord[num].pas;
dfs(num+1,total);
//以下两条指令必须在当前的if中,因为上面的“CurEarn+= ...”和“off+=”
//是满足if 的条件时才执行的。
curearn-=(ord[num].end-ord[num].start)*ord[num].pas;
off[ord[num].end]-=ord[num].pas;
}
total-=ord[num].pas;//还原现场
dfs(num+1,total);
}
return ;
}
int main()
{
while(cin>>n>>numb>>numord&&(n||numb||numord))
{
memset(off,0,sizeof(off));
maxearn=0;curearn=0;
for(int i=0;i<numord;i++)
{
cin>>ord[i].start>>ord[i].end>>ord[i].pas;
}
qsort(ord,numord,sizeof(ord[0]),comp);
dfs(0,0);
cout<<maxearn<<endl;;
}
return 0;
}
C语言中排序的算法有很多种,系统也提供了一个函数qsort()可以实现快速排序。原型如下:
void qsort(void *base, size_t nmem, size_t size, int (*comp)(const void *, const void *));
它根据comp所指向的函数所提供的顺序对base所指向的数组进行排序,nmem为参加排序的元素个数,size为每个元素所占的字节数。例如要 对元素进行升序排列,则定义comp所指向的函数为:如果其第一个参数比第二个参数小,则返回一个小于0的值,反之则返回一个大于0的值,如果相等,则返 回0。
七种qsort排序方法
<本文中排序都是采用的从小到大排序>
一、对int类型数组排序
C/C++ code int num[100]; Sample: int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; } qsort(num,100,sizeof(num[0]),cmp); |
二、对char类型数组排序(同int类型)
C/C++ code char word[100]; Sample: int cmp( const void *a , const void *b ) { return *(char *)a - *(int *)b; } qsort(word,100,sizeof(word[0]),cmp); |
三、对double类型数组排序(特别要注意)
C/C++ code double in[100]; int cmp( const void *a , const void *b ) { return *(double *)a > *(double *)b ? 1 : -1; } qsort(in,100,sizeof(in[0]),cmp); |
四、对结构体一级排序
C/C++ code struct In { double data; int other; }s[100] //按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种,参考上面的例子写 int cmp( const void *a ,const void *b) { return (*(In *)a)->data > (*(In *)b)->data ? 1 : -1; } qsort(s,100,sizeof(s[0]),cmp); |
C/C++ code struct In { int x; int y; }s[100]; //按照x从小到大排序,当x相等时按照y从大到小排序 int cmp( const void *a , const void *b ) { struct In *c = (In *)a; struct In *d = (In *)b; if(c->x != d->x) return c->x - d->x; else return d->y - c->y; } qsort(s,100,sizeof(s[0]),cmp); |
C/C++ code struct In { int data; char str[100]; }s[100]; //按照结构体中字符串str的字典顺序排序 int cmp ( const void *a , const void *b ) { return strcmp( (*(In *)a)->str , (*(In *)b)->str ); } qsort(s,100,sizeof(s[0]),cmp); |
C/C++ code int cmp(const void *a,const void *b) //重点cmp函数,把除了1点外的所有点,旋转角度排序 { struct point *c=(point *)a; struct point *d=(point *)b; if( calc(*c,*d,p[1]) < 0) return 1; else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y)) //如果在一条直线上, 则把远的放在前面 return 1; else return -1; } |