Digital collectible card games have become very popular recently. So Vova decided to try one of these.

Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck with total power not less than k, but magic numbers may not allow him to do so — Vova can't place two cards in a deck if the sum of the magic numbers written on these cards is a prime number. Also Vova cannot use a card if its level is greater than the level of Vova's character.

At the moment Vova's character's level is 1. Help Vova to determine the minimum level he needs to reach in order to build a deck with the required total power.

Input

The first line contains two integers $n$ and $k$ $(1 ≤ n ≤ 100, 1 ≤ k ≤ 100000)$.

Then n lines follow, each of these lines contains three numbers that represent the corresponding card: $p_i$, $c_i$ and $l_i$ $(1 ≤ p_i ≤ 1000, 1 ≤ c_i ≤ 100000, 1 ≤ l_i ≤ n)$.

Output

If Vova won't be able to build a deck with required power, print $ - 1$. Otherwise print the minimum level Vova has to reach in order to build a deck.

题目大意

有$n$个物品,每个物品具有属性$p_i能量,c_i魔力,l_i等级$。要求选出一些物品在满足 两两魔力和为合数 且 总能量大于等于k 的情况下等级最低。


题目分析

首先可以用二分去掉等级这一维。对物品魔力的奇偶性考虑,则可以将物品分为奇偶两列,并保证了这两列物品各自是只能组成合数的(对于特殊的素数2=1+1,需要预先只保留一个能量最大且合法的1)。

考虑以上方式的建图,直观的意义就是若选了物品i,j(各为奇偶),那么就一定会导致不合法。用边权来表示避免这种情况的代价,则是下图所示的建图方式。

由此可见,对于一种情况的最大能量,就是该图的能量总和-最小割。

听说这是一种经典的最小割模型?

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ;
const int maxNum = ;
const int INF = 2e9; struct Edge
{
int u,v,f,c;
Edge(int a=, int b=, int c=, int d=):u(a),v(b),f(c),c(d) {}
}edges[maxm];
bool pr[maxNum];
int n,K,ans,L,R,mid,tag,cnt,S,T;
int p[maxn],c[maxn],l[maxn],sv[maxn];
int edgeTot,head[maxn],nxt[maxm],lv[maxn]; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void addedge(int u, int v, int c)
{
edges[edgeTot] = Edge(u, v, , c), nxt[edgeTot] = head[u], head[u] = edgeTot++;
edges[edgeTot] = Edge(v, u, , ), nxt[edgeTot] = head[v], head[v] = edgeTot++;
}
bool buildLevel()
{
memset(lv, , sizeof lv);
std::queue<int> q;
lv[S] = , q.push(S);
for (int tmp; q.size(); )
{
tmp = q.front(), q.pop();
for (int i=head[tmp]; i!=-; i=nxt[i])
{
int v = edges[i].v;
if (!lv[v]&&edges[i].f < edges[i].c){
lv[v] = lv[tmp]+, q.push(v);
if (v==T) return true;
}
}
}
return false;
}
int fndPath(int x, int lim)
{
if (x==T) return lim;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].v, val;
if (lv[x]+==lv[v]&&edges[i].f < edges[i].c){
if ((val = fndPath(v, std::min(lim, edges[i].c-edges[i].f)))){
edges[i].f += val, edges[i^].f -= val;
return val;
}else lv[v] = -;
}
}
return ;
}
int dinic()
{
int ret = , val;
while (buildLevel())
while ((val = fndPath(S, INF)))
ret += val;
return ret;
}
int main()
{
n = read(), K = read();
for (int i=; i<maxNum; i++)
if (!pr[i]) for (int j=i+i; j<maxNum; j+=i)
pr[j] = true;
for (int i=; i<=n; i++)
p[i] = read(), c[i] = read(), l[i] = read();
ans = -, L = , R = , S = , T = n+;
for (mid=(L+R)>>; L<=R; mid=(L+R)>>)
{
memset(head, -, sizeof head);
sv[] = tag = cnt = edgeTot = ;
for (int i=; i<=n; i++)
if ((c[i]==&&p[i] > p[tag])&&(l[i] <= mid)) tag = i;
for (int i=; i<=n; i++)
if ((c[i]!=||i==tag)&&(l[i] <= mid)) sv[++sv[]] = i;
for (int i=; i<=sv[]; cnt += p[sv[i]], i++)
if (c[sv[i]]&) addedge(S, i, p[sv[i]]);
else addedge(i, T, p[sv[i]]);
for (int i=; i<=sv[]; i++)
if (c[sv[i]]&) for (int j=; j<=sv[]; j++)
if ((~c[sv[j]]&)&&(!pr[c[sv[i]]+c[sv[j]]]))
addedge(i, j, INF);
cnt -= dinic();
if (cnt >= K) ans = mid, R = mid-;
else L = mid+;
}
printf("%d\n",ans);
return ;
}

END

最新文章

  1. VS2012程序打包部署详解
  2. bzoj2928: [Poi1999]飞弹
  3. C#利用NOPI处理Excel的代码
  4. SOME USEFUL MACHINE LEARNING LIBRARIES.
  5. C#综合揭秘——深入分析委托与事件
  6. EndNote文献管理
  7. iOS 进阶 第一天(0323)
  8. hdu 2106
  9. poj1487
  10. jQueryRotate 转盘抽奖代码实现
  11. heap和stack的区别
  12. usaco training 4.1.3 fence6 题解
  13. 重温《STL源码剖析》笔记 第六、七、八章 next_permutation (字典序)
  14. Redis主从+读写分离中可以在从机读取到过期数据
  15. 默认空间和webapps下项目部署
  16. 聊聊JVM(二)说说GC的一些常见概念
  17. windows基于vs2017的opencv安装
  18. php 图像处理函数
  19. ios测试宏指令出错:“Expected identefier”
  20. PHP RBAC权限管理 基于角色的访问控制演示

热门文章

  1. Linux常用命令(补充)-grep
  2. ASP .NET Core 2.1 HTTP Error 502.5 – Process Failure
  3. net core建站
  4. 《从0到1学习Flink》—— Mac 上搭建 Flink 1.6.0 环境并构建运行简单程序入门
  5. Oracle中文乱码,字符集问题处理
  6. Code First 2
  7. 【转载】【MVC 学习 Razor语法】
  8. asp.net重启web应用程序域
  9. spring中自动装配bean
  10. zuul的本地跳转