题意:给出N个切水果的方案,求相邻切水果时间不超过w,且每次最少切3个水果的最多切割次数。
直接暴力枚举,截点枝就可以过了。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef __int64 int64;
typedef long long ll;
#define M 50005
#define N 1000005
#define max_inf 0x7f7f7f7f
#define min_inf 0x80808080
#define mod 1000000007
int pos[35] , T[35] , vis[205] , path[35];
int n , m , w;
vector<int> sq[35] , ans;
bool cmp(int i , int j)
{
return T[i] < T[j];
}
void Dfs(int index , int cnt , int tm)
{
if (index > n)return;
if (n-index+cnt <= ans.size())return;
int v = pos[index];
if (T[v] - tm > w && cnt > 1)return;
vector<int> temp;
int i , up = sq[v].size();
for (i = 0 ; i < up ; i++)
{
if (!vis[sq[v][i]])
temp.push_back(sq[v][i]);
}
if (temp.size() >= 3)
{
path[cnt] = v;
for (i = 0 , up = temp.size() ; i < up ; i++)vis[temp[i]] = 1;
Dfs(index+1 , cnt+1 , T[v]);
if (ans.size() < cnt)
{
for (ans.clear() , i = 1 ; i <= cnt ; i++)ans.push_back(path[i]);
}
for (i = 0 , up = temp.size() ; i < up ; i++)vis[temp[i]] = 0;
}
Dfs(index+1,cnt,tm);
}
int main()
{
int t , i;
scanf("%d",&t);
while (t--)
{
ans.clear();
memset(vis , 0 , sizeof vis);
scanf("%d%d%d",&n,&m,&w);
for (i = 1 ; i <= n ; i++)
{
sq[i].clear();
pos[i] = i;
int q , b;
scanf("%d%d",&q,&T[i]);
while (q--)
{
scanf("%d",&b);
sq[i].push_back(b);
}
}
sort(pos+1,pos+n+1,cmp);
Dfs(1 , 1 , 0);
int up = ans.size();
printf("%d\n",up);
sort(ans.begin() , ans.end());
for (i = 0 ; i < up ; i++)
printf(i+1 < up ? "%d " : "%d\n",ans[i]);
}
return 0;
}