-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1027.cpp
More file actions
65 lines (61 loc) · 1.22 KB
/
Copy path1027.cpp
File metadata and controls
65 lines (61 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//带权活动选择
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int m, n;//m组测试数据,活动数量
int mostWeight[10001];//mostWeight[i]表示第i个活动及之前的活动的集合可获得的最大权值
struct activity
{
int si; // si表示活动开始时间
int fi; // fi表示活动的结束时间
int vi; // vi表示活动的权重
// 构造函数
activity(int start, int end, int weight)
{
si = start;
fi = end;
vi = weight;
}
};
//仿函数
bool cmp(const activity& a, const activity& b) {//按活动结束时间排序
return a.fi < b.fi;
}
int selAct(vector<activity>& act)
{
sort(act.begin(), act.end(), cmp);//按活动结束时间排序
mostWeight[0] = 0;
mostWeight[1] = act[1].vi;
for (int i = 2; i <= n; i++)
{
for (int j=i;j>=0;j--)
{
if (act[j].fi <= act[i].si)
{
mostWeight[i] = max(mostWeight[j] + act[i].vi, mostWeight[i - 1]);
break;
}
}
}
return mostWeight[n];
}
int main() {
cin >> m;
while (m--)
{
cin >> n;//活动数量
vector<activity>act;
act.push_back(activity(0,0,0));//活动下标从1开始
int u1, v1, w1;
for (int i = 0; i < n; i++) {//输入无向边的信息
cin >> u1;
cin >> v1;
cin >> w1;
act.push_back(activity(u1, v1, w1));
}
cout << selAct(act) << endl;
}
return 0;
}