zoukankan      html  css  js  c++  java
  • uva301

     

    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
    ", 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
    ", biggest);
        }
        return 0;
    }

     

    总结:

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

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

  • 相关阅读:
    只要三步,使用html5+js实现像素风头像生成器
    按Ctrl+Enter发送的实现
    “放到桌面”的Servlet实现
    从tom大叔那想着拿书的,呵呵。
    也写dateUtil.js
    智习室
    零基础爬虫课,不会编程也能做爬虫
    1小时教你学会如何采集微博数据:0基础小白也能轻松学会!
    TransactionScope 分布式事务配置
    centos7创建共享文件夹
  • 原文地址:https://www.cnblogs.com/cute/p/3880905.html
Copyright © 2011-2022 走看看