uva 301 - Transportation

此题大意:给定n个车站,每个车站有到不同目的地的订单,最多有22个订单,每个订单的总额为总的人数乘以经过的站,问最大收益。

 首先想到的思路是直接枚举,使用回朔算法,结果过了但是时间是2.6秒,然后使用了了已接受订单的总额 + 后面所有订单的总额 < ans剪枝,时间在1.1秒


最后看了一下,时间可能浪费在使用了vector和next——permutation上了。


附上参考博客:

http://www.cnblogs.com/liaoguifa/archive/2013/04/25/3043183.html

//
//  main.cpp
//  uva 301 - Transportation
//
//  Created by XD on 15/8/2.
//  Copyright (c) 2015年 XD. All rights reserved.
//

//暴力求解尝试
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;

int cap , n , k ;
struct node
{
    int  end , num ;
    };

int c[24] ;
int ans ;
int sum[23] ;
int stanum[23] ;

vector<node> order[10] ;

void dfs(int sta,int tc , int earnings)
{
    if (sta == n ) {
        ans = ans > earnings ? ans:earnings ;
        return ;
    }
    int te=0 ;
    tc+= c[sta] ;
    int d[23] ;
    int temp[23] ;
    
    int tm = 10101010 ;
    for(int i = 0 ; i < stanum[sta] ; i++)
    {
        tm = tm > order[sta][i].num ? order[sta][i].num : tm ;
    }
    if (tm > tc) {
        dfs(sta + 1 , tc, earnings);
        return;
    }
    memcpy(temp, c, sizeof(int) * (n+1) ) ;
    for(int j = 0  ; j<=stanum[sta] ; j++)
    {
        fill(d, d + stanum[sta] + 1, 1) ;
            for (int i = 0; i < j; i++) {
                d[i] = 0 ;
            }
        do{
            int flag = 0 ;
            int totalnum = tc ;
            te = 0 ;
            for (int i = 0 ; i < stanum[sta]; i++) {
                
                int  num =order[sta][i].num  ;
                if (!d[i] && totalnum>=num ) {
                    totalnum -= num ;
                    te += (num * (order[sta][i].end - sta))  ;
                    c[order[sta][i].end] += num;
                    }
                else if(!d[i]&& totalnum < num){
                    flag = 1 ;
                    break ;
                }
            }
            if(!flag)
            {
                if (ans < earnings +te + sum[sta + 1]) {
                   dfs(sta + 1, totalnum,earnings + te) ;

                }
                            }
            memcpy(c, temp, sizeof(int) * (n+1) ) ;
        }while(next_permutation(d, d + stanum[sta])) ;
    }
}

int main() {
    int s ,end , num ;
    while (scanf("%d%d%d" ,&cap,&n,&k)==3 && cap + n + k != 0) {
        ans= 0  ;
        memset(c, 0, sizeof(c)) ;
        memset(order, 0, sizeof(order)) ;
        memset(stanum, 0, sizeof(stanum)) ;
        memset(sum, 0, sizeof(sum)) ;
        for (int i = 0 ; i < n+1; i++) {
            order[i].clear() ;
        }
        for (int i = 0; i < k ; i++) {
            scanf("%d%d%d" ,&s,&end , &num)  ;
            node temp  ; temp.num = num ; temp.end = end ;
            order[s].push_back(temp) ;
            stanum[s]++ ;
            sum[s]+= num * end - num * s ;
        }
        for (int i = n-1; i >=0 ; i--) {
            sum[i] += sum[i+1] ;
        }
        dfs(0, cap,0) ;
        printf("%d\n" , ans) ;
     }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值