您的位置:首页 > 其它

uva 301 - Transportation

2015-08-02 22:58 351 查看
此题大意:给定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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: