F. Card Game
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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: pi, ci and li (1 ≤ pi ≤ 1000, 1 ≤ ci ≤ 100000, 1 ≤ li ≤ 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.
Examples
input
5 8
5 5 1
1 5 4
4 6 3
1 12 4
3 12 1
output
4
input
3 7
4 4 1
5 8 2
5 3 3
output
2
题目大意
有一些卡片每个卡片都有能量,魔力,等级。要选择一些卡片,使得它们的能量和小于 K ,其中如果两个卡片的魔力和为素数则不能同时选择,也不能选择等级比自己高的卡片。求为了满足要求,所需要的最低等级。
解题思路
首先可以发现等级和要求是满足单调性的,所以可以二分等级。那么对于当前可以选的卡片我们在不能同时选的之间连接边,问题就变成了判断带权的最大独立集是否大于等于
因为除了
2
以外的素数都是奇数,所以它们一定是由一个奇数和一个偶数相加得到的。我们把奇数放到左边,偶数放到右边,这张图也就成为了二分图。再考虑特判
那么求解这个二分图的最大带权独立集就是一个经典问题了。设置源点汇点,源点向每个左边的点连一条容量为权值的边,每个右边的点向汇点连接一条容量为权值的边,左右相连的边容量为无穷大。答案
AC代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
const int MAXN=100+3;
const int MAXP=200000+3;
const int MAXV=MAXN;
struct Edge
{
int to, cap, rev;
Edge(int t, int c, int r):to(t), cap(c), rev(r){}
};
vector<Edge> G[MAXV];//图的邻接表表示
int level[MAXV];//顶点到原点的距离标号
int iter[MAXV];//当前弧,在其之前的边已经没有用了
int V;//顶点数
int N, need, val[MAXN], p[MAXN], l[MAXN];
bool is_prime[MAXP];
//有向图中增加一条从from到to的容量为cap的边
void add_edge(int from, int to, int cap)
{
G[from].push_back(Edge(to, cap, G[to].size()));
G[to].push_back(Edge(from, 0, G[from].size()-1));
}
//通过bfs计算从源点出发的距离标号
void bfs(int s)
{
for(int i=0;i<V;++i)
level[i]=-1;
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty())
{
int u=que.front(); que.pop();
for(int i=0;i<G[u].size();++i)
{
Edge &e=G[u][i];
if(e.cap>0 && level[e.to]<0)
{
level[e.to]=level[u]+1;
que.push(e.to);
}
}
}
}
//通过dfs寻找增广路
int dfs(int u, int t, int f)
{
if(u==t)
return f;
for(int &i=iter[u];i<G[u].size();++i)
{
Edge &e=G[u][i];
if(e.cap>0 && level[u]<level[e.to])
{
int d=dfs(e.to, t, min(f, e.cap));
if(d>0)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
//求解从s到t的最大流
int dinic(int s, int t)
{
int flow=0;
while(true)
{
bfs(s);
if(level[t]<0)
return flow;
for(int i=0;i<V;++i)
iter[i]=0;
int f;
while((f=dfs(s, t, INF))>0)
flow+=f;
}
}
void pre_work()
{
for(int i=2;i<MAXP;++i)
is_prime[i]=true;
for(int i=2;i<MAXP;++i)
if(is_prime[i])
{
int tmp=i+i;
while(tmp<MAXP)
{
is_prime[tmp]=false;
tmp+=i;
}
}
}
bool judge(int mid)
{
// 0 ~ N-1 card
// N s
// N+1 t
V=N+2;
for(int i=0;i<V;++i)
G[i].clear();
int s=N, t=N+1, sum=0;
int the_max=0, ind=-1;
for(int i=0;i<N;++i)
if(l[i]<=mid && p[i]==1 && val[i]>the_max)
{
the_max=val[i];
ind=i;
}
for(int i=0;i<N;++i)
if(l[i]<=mid)
{
if(p[i]==1 && i!=ind)//只留下当前可以用的val最大的1
continue;
sum+=val[i];
if(p[i]&1)
{
add_edge(s, i, val[i]);
for(int j=0;j<N;++j)
if(l[j]<=mid && is_prime[p[i]+p[j]])
add_edge(i, j, INF);
}
else add_edge(i, t, val[i]);
}
return sum-dinic(s, t)>=need;
}
int main()
{
scanf("%d%d", &N, &need);
int the_max=0, ind=-1;
for(int i=0;i<N;++i)
{
scanf("%d%d%d", &val[i], &p[i], &l[i]);
if(p[i]==1 && val[i]>the_max)
{
the_max=val[i];
ind=i;
}
}
pre_work();
int l=0, r=N+1;
while(r-l>1)
{
int mid=(l+r)/2;
if(judge(mid))
r=mid;
else l=mid;
}
printf("%d\n", r==N+1?-1:r);
return 0;
}