专栏文章

观 ABC404 D 有感而发

AT_abc404_d题解参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mipelr61
此快照首次捕获于
2025/12/03 10:44
3 个月前
此快照最后确认于
2025/12/03 10:44
3 个月前
查看原文

思路:

因为数据范围小,所以我们使用暴力 DFS 即可通过本题。
时间复杂度为 O(3N×N×M)6×107<109O(3^N \times N \times M) \approx 6 \times 10^7 < 10^9 ,可以通过本题。

代码:

CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
int cost[105],am[105][105];
int h[105],temp[105];
int ans = 9e18;
int n,m;
void dfs(int dth)
{
    if(dth > n)
    {
        memset(temp,0,sizeof(temp));
        int cst = 0;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= m;j++) temp[j] += h[i] * am[i][j];
            cst += cost[i] * h[i];
        }
        for(int j = 1;j <= m;j++)
        {
            if(temp[j] < 2) return;
        }
        ans = min(ans,cst);
        return;
    }
    h[dth] = 2;
    dfs(dth + 1);
    h[dth] = 1;
    dfs(dth + 1);
    h[dth] = 0;
    dfs(dth + 1);
}
signed main()
{
    cin >> n >> m;
    for(int i = 1;i <= n;i++) cin >> cost[i];
    for(int i = 1;i <= m;i++)
    {
        int tmp1,tmp2;
        cin >> tmp1;
        while(tmp1--)
        {
            cin >> tmp2;
            am[tmp2][i] = 1;
        }
    }
    dfs(1);
    cout << ans;
    return 0;
}
/*
4 3
1000 300 700 200
3 1 3 4
3 1 2 4
3 1 2 3
*/

评论

0 条评论,欢迎与作者交流。

正在加载评论...