暴力深搜,剪枝
#include <stdio.h> #include <vector> #include <string.h> #include <math.h> #include <algorithm> using namespace std; #define MAXN 35 int n, m, w, ans, asArray[MAXN]; int vis[205]; struct _node { int c, t, idx, num[15]; bool operator < ( const _node & exa ) const { return this->t < exa.t; } void input ( int o ) { scanf ( "%d%d", &c, &t ); for ( int i = 0; i < c; ++i ) { scanf ( "%d", &num[i] ); } idx = o; } } node[MAXN]; /** 深搜、剪枝 */ /// u:当前点; cnt:总的数量; pre上一个时间点; int tmpArray[500], tpCnt, cnArray[MAXN]; void dfs ( int u, int cnt, int pre ) { int i; if ( cnt > ans ) { ans = cnt; for ( int i = 0; i < ans; ++i ) { asArray[i] = cnArray[i]; } } if ( u == n ) { return; } if ( cnt + ( n - u ) <= ans ) { return; } if ( pre >= 0 && node[u].t - pre > w ) { return; } int ntp = tpCnt; for ( i = 0; i < node[u].c; ++i ) { if ( !vis[node[u].num[i]] ) { tmpArray[tpCnt++] = node[u].num[i]; } } if ( tpCnt - ntp < 3 ) { tpCnt = ntp; goto cao; } for (i = ntp; i< tpCnt; ++i) vis[tmpArray[i]] = 1; cnArray[cnt] = node[u].idx; dfs(u+1, cnt+1, node[u].t); for (i = ntp; i< tpCnt; ++i) vis[tmpArray[i]] = 0; tpCnt = ntp; cao: dfs(u+1, cnt, pre); ; } int main() { #ifdef __GNUC__ freopen ( "in.txt", "r", stdin ); #endif // __GNUC__ int t, i; scanf ( "%d", &t ); while ( t-- ) { scanf ( "%d%d%d", &n, &m, &w ); for ( i = 0; i < n; ++i ) { node[i].input ( i ); } sort ( node, node + n ); ans = 0; memset ( vis, 0, sizeof vis ); tpCnt = 0; dfs ( 0, 0, -1 ); printf("%d\n", ans); sort(asArray, asArray+ans); if (ans) printf("%d", asArray[0]+1); for (i = 1; i< ans; ++i) printf(" %d", asArray[i]+1); puts(""); } return 0; }
08-14
08-14
08-14