Learn C++ from a young age! Record the questions in the study process of preparing for the USACO (American Informatics Olympiad) bronze category competition and record every moment.
Attached is a summary post:Analysis of USACO past bronze group real questions | Summary-CSDN Blog
[Title description]
Farmer John’s N cows (1≤N≤20) lives in a barn with continuous cattle pens numbered 1−100. Cow i occupies the number [si,ti]'s cattle pen. The range of cattle pens occupied by different cows does not overlap with each other. Cows have different cooling requirements, and the temperature of each stall occupied by a cow i must be reduced by at least cici a> unit.
Valley inclusion M 台空调,标记为 1−M (1≤M≤10). No. i Taiwan Air Conditioning Demand Fee mi ] Possessed Niu Kuan's temperature drop < /span> ≤10^6). It is possible to cover the sky and cover the cows. pi(1≤pi bi,ai≤1000) Niu Kuan [ mi
Please help Farmer John find the minimum amount of money it will cost to satisfy the needs of all the cows.
【enter】
First row two integers, division N sum M.
2nd to (N+1) rows, each row 3-integer, division si、ti 和 ci .
th (N+2) to (M +N+1) row, each row four integer, division ai、bi、pi 和 mi.
【Output】
An integer representing the minimum amount of money spent.
【Input sample】
2 4
1 5 2
7 9 3
2 9 2 3
1 6 2 8
1 2 4 2
6 9 1 5
【Output sample】
10
[Detailed code explanation]
#include <bits/stdc++.h>
using namespace std;
int n, M;
int fence[105], fence2[105]={0}; // 定义fence记录奶牛需要的每个牛栏的温度,定义fence2记录每台空调开启后每个牛栏的温度
struct air {
int a, b, p, m;
}a[15];
int book[15] = {0}, ans=1e9;
void dfs(int step)
{
memset(fence2, 0, sizeof(fence2)); // 初始化fence2数组
int sum = 0; // 定义sum,记录开启空调后的花费,每次初始化为0
for (int i=1; i<=M; i++) { // 遍历M台空调
if (book[i]==1) { // 对于选中的那些空调
for (int j=a[i].a; j<=a[i].b; j++) { // 将每台空调对应的牛栏温度进行累加
fence2[j]+=a[i].p;
}
sum += a[i].m; // 并将这些空调运行所需的金钱相加
}
}
int tot=0; // 定义tot统计多少个牛栏已经满足温度降低(这里是提升)要求
for (int i=1; i<=100; i++) { // 遍历所有牛栏
if (fence2[i]<fence[i]) break; // dfs剪枝操作,如果某个位置牛栏上升温度低于要求,直接退出
else tot++; // 否则对这个牛栏进行计数
}
if (tot==100) { // 如果满足要求,应该100个牛栏都满足要求
ans = min(ans, sum); // 此时计算花费最少的金钱
return; // 退出这轮搜索
}
for (int i=1; i<=M; i++) { // dfs的深搜过程,遍历M个空调
if (book[i]==0) { // 对于没有被选中的空调
book[i] = 1; // 选中它
dfs(step+1); // 进行下一步计算
book[i] = 0; // 退回时还原现场
}
}
}
int main()
{
cin >> n >> M; // 输入n和M(因为M与后面的m同名,所以这里用大写)
for (int i=1; i<=n; i++) { // 遍历n行
int s, t, c;
cin >> s >> t >> c; // 输入s、t和c
for (int j=s; j<=t; j++) { // 在数轴上将每个位置的值修改为c
fence[j] = c;
}
}
for (int i=1; i<=M; i++) { // 遍历m行
cin >> a[i].a >> a[i].b >> a[i].p >> a[i].m; // 输入每台空调的a、b、p和m
}
dfs(1); // 开始dfs搜索(这里是0还是1开始没有关系,因为定义了book列表来记录某台空调是否被选中)
cout << ans << endl;
return 0;
}
【operation result】
2 4
1 5 2
7 9 3
2 9 2 3
1 6 2 8
1 2 4 2
6 9 1 5
10