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<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<iostream>

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<b.start;

    else return a.end1<b.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<order;i++) scanf("%d %d %d",&p[i].start,&p[i].end1,&p[i].num);

        sort(p,p+order,cmp);

       // for(i=0;i<order;i++) printf("%d %d %d\n",p[i].start,p[i].end1,p[i].num);





        memset(people,0,sizeof(people));

        maxmoney=0;

        DFS(0,0);

        printf("%d\n",maxmoney);

    }

}

Transportation poj1040的更多相关文章

  1. POJ1040 Transportation

    题目来源:http://poj.org/problem?id=1040 题目大意: 某运输公司要做一个测试.从A城市到B城市的一条运输线路中有若干个站,将所有站包括A和B在内按顺序编号为0到m.该路线 ...

  2. poj1040 Transportation(DFS)

    题目链接 http://poj.org/problem?id=1040 题意 城市A,B之间有m+1个火车站,第一站A站的编号为0,最后一站B站的编号为m,火车最多可以乘坐n人.火车票的票价为票上终点 ...

  3. POJ 1797 Heavy Transportation(最大生成树/最短路变形)

    传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accept ...

  4. 【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)

    Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s repr ...

  5. Heavy Transportation(最短路 + dp)

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  6. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  7. poj 1797 Heavy Transportation(最短路径Dijkdtra)

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 26968   Accepted: ...

  8. POJ 1797 Heavy Transportation

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  9. uva301 - Transportation

      Transportation Ruratania is just entering capitalism and is establishing new enterprising activiti ...

随机推荐

  1. iframe操作(跨域解决等)

    note:当页面内嵌入一个iframe实际上是在dom上新建了一个新的完整的window对象 iframe中取得主窗体 window.top (顶级窗口的window对象) window.parent ...

  2. (3) python--matplotlib

    (一)1.如何绘制散点图 import numpy as np import matplotlib.pyplot as plt # 如何绘制散点图 # 先随机生成数据 x = np.array(ran ...

  3. hdu 5102(巧妙的搜索)

    The K-th Distance Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. [BZOJ1455]罗马游戏 左偏树+并查集

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 2285  Solved: 994[Submit][Status][Discuss] ...

  5. 洛谷 P1048 采药【裸01背包】

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:" ...

  6. @Transactional注解事务不起作用

    @Transactional注解事务不起作用 问题:今天在项目中碰到一个事务问题,使用@Transactional注解事务,抛出异常不会滚. 解决一:https://blog.csdn.net/u01 ...

  7. Manacher【p4555】 [国家集训队]最长双回文串

    题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 n 的串 S ,求 S 的最长双回文子串 T ,即可 ...

  8. [BZOJ 1806] Miners 矿工配餐

    Link: BZOJ 1806 传送门 Solution: 为了使状态包含每个节点前所有必须的信息: 设$dp[i][a1][a2][b1][b2]$为配送到第$i$个,一厂前两个为$a1,a2$,二 ...

  9. linux安装mysql数据库(5.7之前的版本)

    到mysql官网下载mysql编译好的二进制安装包   解压32位安装包: 进入安装包所在目录,执行命令:tar mysql-5.6.17-linux-glibc2.5-i686.tar.gz   复 ...

  10. 集合框架(中):Map

    Map接口: Map提供了一种映射关系,其中的元素就是以键值对(key-value)的形式存储的,能够实现根据key快速查找value Map中的键值对以Entry类型的对象实例形式存在 键(key值 ...