Transportation poj1040

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);

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 15:17:25

Transportation poj1040的相关文章

poj1040 Transportation

题目链接 http://poj.org/problem?id=1040 题意 城市A,B之间有m+1个火车站,第一站A站的编号为0,最后一站B站的编号为m,火车最多可以乘坐n人.火车票的票价为票上终点站的编号减去起点站的编号.输入火车票订单的数目orderNums,接着输入orderNums个订单,每个订单由3个数字s,e,p组成,分别表示订单的起点站,终点站,该订单的人数,必须全部接受订单上的乘客或者全部不接受,求铁路公司最多可以赚多少钱. 思路 对于一个订单来说,有两种情况:接受和不接受.我

POJ 1797 Heavy Transportation (Dijkstra变形)

F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand busines

zoj3231 Apple Transportation(最大流)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Apple Transportation Time Limit: 1 Second      Memory Limit: 32768 KB There's a big apple tree in the forest. In the tree there are N nodes (numbered from 0 to N - 1), and the nodes are conne

POJ 3228 Gold Transportation

Gold Transportation Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 322864-bit integer IO format: %lld      Java class name: Main Recently, a number of gold mines have been discovered in Zorroming State. To p

[codeforces724E]Goods transportation

试题描述 There are n cities located along the one-way road. Cities are numbered from 1 to n in the direction of the road. The i-th city had produced pi units of goods. No more than si units of goods can be sold in the i-th city. For each pair of cities i

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

POJ 1797 Heavy Transportation SPFA变形

原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 24576   Accepted: 6510 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand

HDU 3667 Transportation(网络流之费用流)

题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k,需要让每一次增广到的流量都是1,这就需要把每一条边的流量都是1才行.但是每条边的流量并不是1,该怎么办呢.这个时候可以拆边,反正c最多只有5,拆成5条流量为1的边.但是这时候费用怎么办呢,毕竟平方的关系不能简单把每一条边加起来.这时候可以把拆的边的流量设为1,3,5,7,9.如果经过了3个流量,那就肯定会流1,3,5,费用为9,是3的平方,同理,其他的也是如此.然后按照给出的边建图跑一次费用流就可以了. 代码如下: #i

HDU 4940 Destroy Transportation system 规律题

答案只有2种情况,所以ans = rand()%2; if(ans)puts("happy") else puts("unhappy"); == 想过无源汇的网络流,还是比较麻烦的,然后没往下想... 设s点集有一些点, 多加一个点一定是y增加比较快_(:зゝ∠)_ 然后设s点集只有一个点 #include <cstdio> #include <map> #include <set> #include <algorithm&