uva 301 - Transportation

点击打开链接

题目意思:   有一辆车从A城市开往B城市,途中有m个站,车上最多的载客人数为n人,每一个站的价格就是终点和起点的差值,现在有k分订单,要求找到这辆车的最大利润

解题思路:   这一题如果我们去搜索站点,那么情况将会非常糟糕,但是如果我么去搜索订单,那么对于每一个订单而言就是取和不取,那么我们就可以知道这个解空间树的每一层就是一个订单,那么我们只要对这个订单编号,然后搜索订单即可。另外我们开一个数组,专门来存储每一个站点当前的人数,注意这里的人数处理,一份订单进来,那么起点---终点前一站都是要加上的,终点下车不用加,还有做dfs之前都是先判断judge函数,最后恢复现场。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 25;

int n, m, d;
int num[10]; //记录每一个站点的人数
int ans;

struct Order {//订单的结构体,存储起点和终点和人数
    int start;
    int end;
    int number;
};
Order o[MAXN];//结构体数组

//判断当前的所有的站点的人数是否
int judge() {
    for(int i = 0 ; i <= m ; i++){
        if(num[i] > n)//如果人数大于n则不满足
           return 0;
    }
    return 1;
}

void dfs(int k , int max) {
    if(ans < max)
        ans = max;
    while(k < d){
        //如果要选择该订单
        for(int i = o[k].start ; i < o[k].end ; i++)
            num[i] += o[k].number;
        if(judge())
            dfs(k+1 , max+o[k].number*(o[k].end-o[k].start));
       //由于之前就加上了人数,所以这里一定要减去人数
       for(int i = o[k].start ; i < o[k].end ; i++)
            num[i] -= o[k].number;
        ++k;//下一个订单
    }
}
//
int main() {
    while (scanf("%d%d%d%*c", &n, &m, &d)) {
        if( n == 0 && m == 0 && d == 0)//注意是 0 0 0 结束
            break;
        memset(num, 0, sizeof (num));
        ans = 0;
        for (int i = 0; i < d; i++)
            scanf("%d%d%d", &o[i].start, &o[i].end, &o[i].number);
        dfs(0, 0);
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-14 19:30:21

uva 301 - Transportation的相关文章

UVa 301:Transportation

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=237 题目类型: 回溯法 原题: Ruratania is just entering capitalism and is establishing new enterprising activities in many fields includ

UVa 208:Firetruck,双向搜索进行剪枝

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=144 类型: 回溯法 原题: The Center City fire department collaborates with the transportation department to maintain maps of the city

UVa 10313 Pay the Price:DP&amp;amp;整数拆分

10313 - Pay the Price Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1254 In ancient days there was a country whose people had very interesting habits.

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

HTTP常见状态码 200 301 302 404 500

HTTP状态码(HTTP Status Code) 一些常见的状态码为: 一.1开头 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码.代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101 (切换协议) 请求者已要求服务器切换协议,服务器已确认并准备切换. 二.2开头 2xx (成功)表示成功处理了请求的状态代码.代码 说明 200 (成功) 服务器已成功处理了请求. 通常,这表示服务器提供了请求的网页. 2

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.