CSP 第34次认证第四题 货物调度

题目链接

只想做一个30分解法。考场上写dfs只能过15分,不思其解。系统未开放评测。
将复现方法粘贴如下,开放数据后再进行测试。

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>
using namespace std;

int n, m, v;
struct store {
    int b, c;
};
struct Goods {
    int a, t;
};
store Store[1010];
Goods goods[1010];

int ans = 0x3f3f3f3f;

bool flag[1010];// 标记每个物品时选择了还是没有选择

void dfs(int u, int sum) {
    if (u == m) {
        // 遍历完了m个物品
        // 求一下cost,需要知道每个选择的物品是哪个仓库的,goods.t 就是。还要知道每个仓库一共调用了多少物品
        map<int, int>count;
        for (int i = 0; i < m; i ++ ) {
            if (flag[i]) // 选择了第i个货物
                count[goods[i].t] ++;
        }
        int cost = 0;
        for (auto [key, value]: count) {
            // key: count.first, value: count.second;
            int basic = Store[key].b;
            int add = Store[key].c;
            cost += basic + add * value;
        }
        if (sum - cost >= v) {
            ans = min(ans, cost);
        }
        return ;
    }
    // 不选择 
    dfs(u + 1, sum);
    // 选择 u
    flag[u] = 1;
    dfs(u + 1, sum + goods[u].a);
    flag[u] = 0;

}


int main() {
    cin >> n >> m >> v;
    for (int i = 0; i < n; i ++ ) {
        cin >> Store[i].b >> Store[i].c;
    }
    for (int i = 0; i < m; i ++ ) {
        cin >> goods[i].a >> goods[i].t;
    }

    // 解决 m = 15 的情况,每个物品选择还是不选择 
    dfs(0, 0); // 从第0个物品开始,已经选择的货物获得的现金为0

    cout << ans << endl;
    
    return 0;
}

相关推荐

  1. CSP 34认证 货物调度

    2024-06-15 17:38:08       29 阅读
  2. CCF-CSP认证考试 202406-4 货物调度 100分题解

    2024-06-15 17:38:08       23 阅读
  3. 数据库作业

    2024-06-15 17:38:08       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 17:38:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 17:38:08       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 17:38:08       82 阅读
  4. Python语言-面向对象

    2024-06-15 17:38:08       91 阅读

热门阅读

  1. 关于编程思想

    2024-06-15 17:38:08       34 阅读
  2. 数列求和、统计输入正数个数 题目

    2024-06-15 17:38:08       33 阅读
  3. 查看队列资源限额和使用情况

    2024-06-15 17:38:08       29 阅读
  4. 一些常见的显示接口

    2024-06-15 17:38:08       37 阅读
  5. 高考报志愿闲谈

    2024-06-15 17:38:08       31 阅读
  6. GraphQL(8):与数据库结合示例

    2024-06-15 17:38:08       31 阅读
  7. 搭建Python虚拟环境(一):基础知识

    2024-06-15 17:38:08       32 阅读