码迷,mamicode.com
首页 > 其他好文 > 详细

uva301 - Transportation

时间:2014-07-31 16:49:46      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   for   art   

 

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

但是学习了一些,枚举站点的时候要枚举每个站点的订单的组合子集

//按站点枚举
#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\n", biggest);
        }
    return 0;
}

 

枚举订单:141 ms

//按订单枚举,每个订单只有选和不选两种状态,复杂度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\n", biggest);
    }
    return 0;
}

uva301 - Transportation,布布扣,bubuko.com

uva301 - Transportation

标签:des   style   blog   color   os   io   for   art   

原文地址:http://www.cnblogs.com/cute/p/3880905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!