UVA 301 Transportation

UVA-301

题意:给出车量承载人数,城市数,订单数,以及每个订单的起始城市,终点城市和人数。城市的到达顺序0->1->2->……>n,每个人每坐到下一个城市需要1个花费,求车子最多能获得多少钱。
解题思路:枚举每个订单接不接,起始站的num[star]+= order_number[i]; 终点站 num[end]-=order_number[i]。然后从0开始一路累加num[i],到城市i,前面 i-1 个num的累加的结果就是从在i-1到这个城市车上还有的人数。计算总花费,取最大的值。

/*************************************************************************
    > File Name: UVA-301.cpp
    > Author: Narsh
    > 
    > Created Time: 2016年07月27日 星期三 14时40分44秒
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
    int x,y,c;
} w[30];
int n,m,k,Max,temp[20];
void dfs(int i) {
    if (i > m) {
        int s=0,t=0;
        for (int i = 0; i <= n; i++) {
            s+=t;
            t=t+temp[i];
            if (t > k) {
                s=0;
                break;
            }
        }
        if (s > Max) Max = s;
        return ;
    }
    temp[w[i].x]+=w[i].c;
    temp[w[i].y]-=w[i].c;
    dfs(i+1);
    temp[w[i].x]-=w[i].c;
    temp[w[i].y]+=w[i].c;
    dfs(i+1);
}
int main() {
    while (scanf("%d%d%d",&k,&n,&m) && n+k+m) {
        memset(temp,0,sizeof(temp));
        for (int i = 1; i <= m; i++) 
            scanf("%d%d%d",&w[i].x,&w[i].y,&w[i].c);
        Max=0;
        dfs(1);
        printf("%d\n",Max);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值