题意:
给你一些卡片,每个卡片有价值,魔法值,等级。
现在要你组成一个卡组,使得卡组里面任意两个数魔法值相加不是质数,并且价值之和大于k。
问你需要达到的最低等级,你可以使用等级小于等于你自己等级的卡片。
题解:
如果我们将加起来为素数的任意两个数连边,那么我们就是要求一个独立集,使得点权之和大于k。
而且我们发现,两数相加为质数,一定是一个奇数,一个偶数(除了两个1相加)的二分图。
1的问题怎么解决? 由于只能选择最多一个1,所以我们保留权值最大的那个1,然后再跑二分图最大点权独立集即可。
二分一下答案。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <bitset>
#include <map>
#include <vector>
#include <stack>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <ctime>
#ifdef LOCAL
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define debug(x) 1;
#endif
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lowbit(x) x&-x
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> pii;
const int MOD = 1e9 + 7;
const double PI = acos (-1.);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 2e5 + 5;
int p[MAXN], c[MAXN], l[MAXN];
const int inf = 0x3f3f3f3f;
const int MX = 205;
const int MXE = 4 * MX * MX;
struct MaxFlow {
struct Edge {
int v, w, nxt;
} edge[MXE];
int tot, num, s, t;
int head[MX];
void init() {
memset (head, -1, sizeof (head) );
tot = 0;
}
void add (int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
edge[tot].v = u;
edge[tot].w = 0;
edge[tot].nxt = head[v];
head[v] = tot++;
}
int d[MX], vis[MX], gap[MX];
void bfs() {
memset (d, 0, sizeof (d) );
memset (gap, 0, sizeof (gap) );
memset (vis, 0, sizeof (vis) );
queue<int>q;
q.push (t);
vis[t] = 1;
while (!q.empty() ) {
int u = q.front();
q.pop();
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if (!vis[v]) {
d[v] = d[u] + 1;
gap[d[v]]++;
q.push (v);
vis[v] = 1;
}
}
}
}
int last[MX];
int dfs (int u, int f) {
if (u == t) return f;
int sap = 0;
for (int i = last[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if (edge[i].w > 0 && d[u] == d[v] + 1) {
last[u] = i;
int tmp = dfs (v, min (f - sap, edge[i].w) );
edge[i].w -= tmp;
edge[i ^ 1].w += tmp;
sap += tmp;
if (sap == f) return sap;
}
}
if (d[s] >= num) return sap;
if (! (--gap[d[u]]) ) d[s] = num;
++gap[++d[u]];
last[u] = head[u];
return sap;
}
int solve (int st, int ed, int n) {
int flow = 0;
num = n;
s = st;
t = ed;
bfs();
memcpy (last, head, sizeof (head) );
while (d[s] < num) flow += dfs (s, inf);
return flow;
}
} F;
int n, k;
int st, en;
int is_prime[MAXN];
void init() {
memset(is_prime, -1, sizeof(is_prime));
for (int i = 2; i < MAXN; i++)
if(is_prime[i]) for (int j = i + i; j < MAXN; j += i) is_prime[j] = 0;
}
bool check (int x) {
F.init();
st = 0, en = n + 1;
int ind = 0, maxval = 0;
int tot = 0;
for (int i = 1; i <= n; i++) {
if (l[i] > x) continue;
if (c[i] == 1) {
ind = i;
maxval = max (maxval, p[i]);
continue;
}
tot += p[i];
if (c[i] & 1) F.add (st, i, p[i]);
else F.add (i, en, p[i]);
}
tot += maxval;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if(is_prime[c[i] + c[j]]) {
if (c[i] & 1) F.add(i, j, INF);
else F.add(j, i, INF);
}
}
}
if (ind) F.add (st, ind, maxval);
return tot - F.solve (st, en, n + 2) >= k;
}
int main() {
#ifdef LOCAL
freopen ("input.txt", "r", stdin);
#endif
scanf ("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf ("%d %d %d", &p[i], &c[i], &l[i]);
F.init();
init();
int l = 1, r = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check (mid) ) r = mid - 1;
else l = mid + 1;
}
printf ("%d\n", r == n ? -1 : r + 1);
return 0;
}