题目背景:
一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥。
题目描述:
桥已经很旧了, 所以它不能承受太重的东西。任何时候队伍在桥上的人都不能超过一定的限制。
所以这只队伍过桥时只能分批过,当一组全部过去时,下一组才能接着过。队伍里每个人过桥都需要特定的时间,当一批队员过桥时时间应该算走得最慢的那一个,每个人也有特定的重量,我们想知道如何分批过桥能使总时间最少。
输入格式:
第一行两个数: WW 表示桥能承受的最大重量和 nn 表示队员总数。
接下来 nn 行:每行两个数: tt 表示该队员过桥所需时间和 ww 表示该队员的重量。
输出格式:
输出一个数表示最少的过桥时间。
输入输出样例:
输入:
100 3
24 60
10 40
18 50
输出:
42
说明:
对于 100%100% 的数据,100\le W \le400100≤W≤400 ,1\le n\le 161≤n≤16,1\le t\le501≤t≤50,10\le w\le10010≤w≤100。
思路:
贪心吧。。我是用贪心做的,数据规模比较小。大佬们好像用的状压dp写的,不太会。。
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct people
{
int time;
int weight;
bool operator < (const people b)const
{
return time > b.time;
}
}a[20];
int b[20];
int main()
{
int i, cnt = 0;
int ans = 0;
int w, n;
cin >> w >> n;
for (i = 1; i <= n; ++i)
{
cin >> a[i].time >> a[i].weight;
}
sort(a + 1, a + n + 1);
int sum = 0;
b[cnt++] = 1;
for (i = 1; i <= n; ++i)
{
sum += a[i].weight;
if (sum > w)
{
b[cnt++] = i;
sum = 0;
}
}
for (i = 0; i < cnt; ++i)
{
ans += a[b[i]].time;
}
cout << ans << endl;
return 0;
}