观 ABC404 D 有感而发
Ryzen_9_9950X3D · · 题解
思路:
因为数据范围小,所以我们使用暴力 DFS 即可通过本题。
时间复杂度为
代码:
#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
*/