题目背景
一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥。
题目描述
桥已经很旧了, 所以它不能承受太重的东西。任何时候队伍在桥上的人都不能超过一定的限制。 所以这只队伍过桥时只能分批过,当一组全部过去时,下一组才能接着过。队伍里每个人过桥都需要特定的时间,当一批队员过桥时时间应该算走得最慢的那一个,每个人也有特定的重量,我们想知道如何分批过桥能使总时间最少。
输入格式
第一行两个数: W表示桥能承受的最大重量和 n表示队员总数。
接下来 n 行:每行两个数: t 表示该队员过桥所需时间和 w表示该队员的重量。
输出格式
输出一个数表示最少的过桥时间。
输入输出样例
输入 #1
100 3
24 60
10 40
18 50
输出 #1
42
说明/提示
对于 100% 的数据,100≤W≤400 ,1≤n≤16,1≤t≤50,10≤w≤100。
#include<bits/stdc++.h>
using namespace std;
int w,n;
int ans;
int cnt=1;
struct Peo{
int tim;
int wgt;
int bg;//记录是否过桥
}p[20];
bool cmp(Peo x,Peo y){
if(x.tim!=y.tim)
return x.tim>y.tim;
return x.wgt>y.wgt;
}
int main(){
scanf("%d%d",&w,&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&p[i].tim,&p[i].wgt);
sort(p+1,p+n+1,cmp);
while(1){
while(cnt<=n&&p[cnt].bg==1)
cnt++;
if(cnt>n)
break;
int thw=w;
ans+=p[cnt].tim;
p[cnt].bg=1;
thw-=p[cnt].wgt;
for(int i=cnt+1;i<=n;i++){
if(p[i].bg==0&&thw>p[i].wgt){
thw-=p[i].wgt;
p[i].bg=1;
}
}
cnt++;
}
printf("%d",ans);
return 0;
}