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

 

枚举站点: 超时啊。。。剪枝了还是超时,按道理复杂度也不会很高啊。订单最多22个,每个站点平均算3个吧,复杂度3^7=2187,  每个站点子集个数2^3, 复杂度(2^3)^7=2097152

但是学习了一些,枚举站点的时候要枚举每个站点的订单的组合子集

更新:后来又提交了一次,2.399 没超时。。

//按站点枚举
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n, city, cnt; struct Order
{
bool operator < (const Order& o) const
{
return s<o.s;
}
int s;
int d;
int p;
}orders[23]; int vis[23]; int biggest; //得到从某个站点出发的订单个数
int getOrdersNum(int d)
{
int num=0;
for(int j=0;j<cnt;j++)
if(orders[j].s==d)
num++;
return num;
}
int getOrderStart(int d)
{
for(int j=0;j<cnt;j++)
if(orders[j].s==d)
return j;
return -1;
} int getNextOrder(int d)
{
for(int j=0;j<cnt;j++)
if(orders[j].s>=d)
return j;
return cnt;
} bool prune(int start, int e)
{
int sum=e;
for(int i=start;i<cnt;i++)
{
sum+=orders[i].p*(orders[i].d-orders[i].s);
}
return sum<=biggest;
} void dfs(int d, int p, int e); void subset(int sum, int s, int start, int d, int p, int e)
{
int vis_old[23];
memcpy(vis_old, vis, sizeof(vis));
for(int i=0;i<sum;i++) if(s&(1<<i))
{
int j=start+i;
if(!vis[j])
{
vis[j]=1;
e+=orders[j].p*(orders[j].d-orders[j].s);
p+=orders[j].p;
}
}
if(p<=n)
dfs(d+1, p, e);
memcpy(vis, vis_old, sizeof(vis));
} void dfs(int d, int p, int e)
{
biggest=max(biggest, e);
if(d==city)
return; //下车的
for(int j=0;j<cnt;j++) if(vis[j] && orders[j].d==d) {
p-=orders[j].p;
} //上车的
//每个站点可能多个上车,还要去枚举可能上车的子集
int sum=getOrdersNum(d);
int start=getOrderStart(d);
//剪枝剩余的订单
if(prune(getNextOrder(d), e))
return;
for(int i=0;i<(1<<sum);i++)
subset(sum, i, start, d, p, e); } int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva301.in", "r", stdin);
#endif
while(scanf("%d%d%d", &n, &city, &cnt)==3 && (n || city || cnt)) {
memset(orders, 0, sizeof(orders));
memset(vis, 0, sizeof(vis));
biggest=0;
for(int i=0;i<cnt;i++) scanf("%d%d%d", &orders[i].s, &orders[i].d, &orders[i].p);
sort(orders, orders+cnt);
dfs(0, 0, 0);
printf("%d\n", biggest);
}
return 0;
}

 

枚举订单:141 ms0.123

//按订单枚举,每个订单只有选和不选两种状态,复杂度2^22=4194304
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=23;
int n, city, cnt; struct Order
{
int s;
int d;
int p;
}orders[maxn]; int station_p[maxn];//每个站点的人数 int biggest; //判断加入剩余订单之后赚的钱是否会比当前的多
bool prune(int start, int e)
{
int sum=e;
for(int i=start;i<cnt;i++)
{
sum+=orders[i].p*(orders[i].d-orders[i].s);
}
return sum<=biggest;
} void dfs(int d, int e)
{
biggest=max(biggest, e);
 if(prune(d, e))
return; if(d==cnt)
return; //不选
dfs(d+1, e);
//选
bool ok=true;
//更新站点人数
for(int i=orders[d].s;i<orders[d].d;i++)
{
station_p[i]+=orders[d].p;
if(station_p[i]>n)
ok=false;
}
if(ok)
dfs(d+1, e+orders[d].p*(orders[d].d-orders[d].s));
//恢复
for(int i=orders[d].s;i<orders[d].d;i++)
station_p[i]-=orders[d].p;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva301.in", "r", stdin);
#endif
while(scanf("%d%d%d", &n, &city, &cnt)==3 && (n || city || cnt)) {
memset(orders, 0, sizeof(orders));
memset(station_p, 0, sizeof(station_p));
biggest=0;
for(int i=0;i<cnt;i++) scanf("%d%d%d", &orders[i].s, &orders[i].d, &orders[i].p);
dfs(0, 0);
printf("%d\n", biggest);
}
return 0;
}

 

总结:

做题时候可以先画图,理清思路。

一种思路不行或实现困难,可以换种。

最新文章

  1. javascript面向对象系列第四篇——选项卡的实现
  2. java1.8常用的函数式接口
  3. mysql 常用语句
  4. javascript实现有向无环图中任意两点最短路径的dijistra算法
  5. Magento怎么设置和使用Cookie和Session?
  6. 怎么用ABBYY打开PDF文档
  7. Studio右键选项中没有Git?
  8. 利用正则表达式解析URL
  9. 【Shell脚本学习11】Shell注释
  10. C# XmlReader/XmlWriter 类
  11. RHEL4-Partition Image系统备份(软件版)
  12. IOC 容器初始化
  13. 201521123117 《Java程序设计》第3周学习总结
  14. git工具——版本的创建与回退
  15. WEB测试总结
  16. Linux下输入某些命令时会提示:bash:command not found
  17. innodb_flush_method理解
  18. python自定义函数的参数之四种表现形式
  19. AutoFac Ioc依赖注入容器
  20. 机器学习入门-文本数据-构造Tf-idf词袋模型(词频和逆文档频率) 1.TfidfVectorizer(构造tf-idf词袋模型)

热门文章

  1. 关于RF 315MHz
  2. 堪称最好的A*算法
  3. struts2异常处理,global-results定义全局结果处理
  4. 模式自由(Schema-free)和数据存储的非格式化趋势
  5. Java 8开发的4大顶级技巧
  6. Markdown 是什么?
  7. 黑马程序员——OC与C语言的异同比较
  8. AI钻石风格logo教程
  9. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程08:虚拟键盘实现》--本系列完结
  10. 分布式文件系统--GFS