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);
}
}