UVA Transportation (DFS)

 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


       题意:第一行输入w,n,m,其中w代表车的最大载客数,n代表有n个站牌,
然后有m组数据。一辆公交车从起点0出发至终点站牌n,每个人交的乘车的价钱是他的乘车的站牌数(例如自站牌1做到站牌2需要1元钱,自站牌1做到站牌3需要3元钱,以此类推)。需要注意的是如果到达某一个站牌时车上还剩5个座位,但是在这个站牌到达下个站牌的人数大于5个,则这辆车将不会让这群人上车(意思是要上车就全都上,否则全都不让上。),求出在这样的规则下,公交车的最大获益金额。

代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

struct node
{
    int x;
    int y;
    int z;
    int num;
    int ss;
}q[10010];

int n,m,w;
int t;
int a[10010];
int maxx;

void add(int x,int y,int z,int num)
{
    q[t].x = x;
    q[t].y = y;
    q[t].z = z;
    q[t].num = num;
    q[t++].ss = num;
}

int cmp(const void *a,const void *b)
{
    struct node *aa,*bb;
    aa = (struct node*)a;
    bb = (struct node*)b;
    if(aa->num!=bb->num)
    {
        return bb->num - aa->num;
    }
    else
    {
        return aa->x - bb->x;
    }
}

void DFS(int sum,int cnt)
{
    if(maxx < sum)
    {
        maxx = sum;
    }
    for(;cnt<t;cnt++)
    {
        if(sum + q[cnt].ss < maxx)
        {
            return ;
        }
        int i;
        for(i=q[cnt].x;i<q[cnt].y;i++)
        {
            a[i] += q[cnt].z;
            if(a[i]>w)
            {
                break;
            }
        }

        if(i == q[cnt].y)
        {
            DFS(sum+q[cnt].num,cnt+1);
            i--;
        }
        for(;i>=q[cnt].x;i--)
        {
            a[i] -= q[cnt].z;
        }
    }
}

int main()
{
    while(scanf("%d%d%d",&w,&n,&m)!=EOF)
    {
        if(w == 0 && n == 0 && m == 0)
        {
            break;
        }
        t = 0;
        maxx = 0;
        memset(a,0,sizeof(a));
        int x,y,z,pnum;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(z<=w)
            {
                pnum = (y-x)*z;
                add(x,y,z,pnum);
            }
        }
        qsort(q,t,sizeof(q[0]),cmp);
        for(int i=t-2;i>=0;i--)
        {
            q[i].ss = q[i].ss + q[i+1].ss;
        }
        DFS(0,0);
        printf("%d\n",maxx);
    }
    return 0;
}

				
时间: 2024-10-12 08:21:13

UVA Transportation (DFS)的相关文章

uva 211(dfs)

211 - The Domino Effect Time limit: 3.000 seconds A standard set of Double Six dominoes contains 28 pieces (called bones) each displaying two numbersfrom 0 (blank) to 6 using dice-like pips. The 28 bones, which are unique, consist of the followingcom

uva :185 - Roman Numerals(dfs)

题目:uva :185 - Roman Numerals 题目大意:给出一个字符串的等式,问这个字符串是否是罗马等式吗?有符合的阿拉伯等式吗?前者是就输出correct or incorrect ,后者就得分情况: ambiguous 能组成阿拉伯等式的字母组合大于等于2, valid 能组成阿拉伯等式的字母组合只有1种 impossible 没有符合阿拉伯等式的字母组合. 解题思路: 1.能不能组成罗马等式的规则:每个当前的字母(i)的左边的字母((i-1)所对应的值如果比i所对应的值小的话,

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

11218 - KTV(dfs)

问题 C: Repeat Number 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 7 [提交][状态][论坛] 题目描述 Definition: a+b = c, if all the digits of c are same ( c is more than ten),then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y]. 输入 There

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar