注释齐全,枚举状态转移点,枚举状态,更新就OK
// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \
ios::sync_with_stdio(false); \
// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e6 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double pi = acos(-1);
//int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int dis[2][2] = {1, 0, 0, 1};
//int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int a[110][100];
int f[1 << 20]; // µ½´ïµ±Ç°×´Ì¬ËùÐèÒª¹ºÂòµÄ×îÉÙÊýÁ¿
int sta[110]; // ÿһ°üÌǹûµÄ״̬Óöþ½øÖƱíʾÈç¹ûΪ1£¬ÄÇô±íʾÓÐÕâÖÖ¿Úζ
int main()
{
#ifdef WXY
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
IO;
int n, m, k,x;
cin >> n >> m >> k;
memset(f, -1, sizeof f);
f[0] = 0;// ¹ºÂò 0 °ü -> Ò»ÖÖ¿Úζ¶¼µÃ²»µ½
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= k; j++)
{
cin >> x;
sta[i] |= 1 << (x - 1);
}
f[sta[i]] = 1;
}
int all = 1 << m; // ״̬µÄ×ÜÊý
for (int i = 1; i <= n; i++) // ÿ¸ö°ü¹üͨ¹ýËûÃǽøÐÐ״̬תÒÆ
{
for (int j = 0; j < all; j++) // ö¾ÙËùÓеÄ״̬
{
if (f[j] == -1)// Èç¹ûµ±Ç°×´Ì¬¶¼Ã»Óе½´ïÄÇô×ÔÈ»ÎÞ·¨Í¨¹ýËü½øÐÐתÒÆ
continue;
if (f[j | sta[i]] == -1 || f[j] + 1 < f[j | sta[i]])//µ±Ç°×´Ì¬Ã»Óе½´ï»òÕß
f[sta[i] | j] = f[j] + 1; // µ±Ç°×´Ì¬ËùÐèµÄ×îÉÙ¹ºÂòÊý
} // ¿ÉÒÔ¸üСÄÇô¸üÐÂ
}
cout << f[all-1];
return 0;
}