Codeforces 808F 网络流最小割(二分图最大点权独立集) 解题报告

F. Card Game

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

【解题报告】
题目大意:
Vova 有 n 张牌。每张牌有pi, ci和li。Vova想要选一些牌使得pi之和大等于k,但是Vova不能同时选两张牌如果两张牌的ci之和为素数。Vova也不能选li大于她人物等级的牌。
一开始Vova是一级。帮助Vova确定她最少需要升到多少级才能使得pi之和大于等于k。

二分来判断最少需要升到多少级
三个数不可能两两相加都是素数
所以是一张二分图
二分图的最大独立集等于 权值和-最小割
注意判断只能放入一个最优的1(三个1可以两两相加都是素数)

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 110
#define inf 0x3f3f3f3f

int cnt,head[N],dis[N],vis[N];
struct Edge{int to,nxt,f;}e[(N*N)<<2];
int t,n,k,ss,tt;
bool np[200007]={1,1};
struct pos{int p,c,l;}ps[N];

void adde(int u,int v,int c)
{
    e[++cnt].to=v;e[cnt].f=c;
    e[cnt].nxt=head[u];head[u]=cnt;
    e[++cnt].to=u;e[cnt].f=0;
    e[cnt].nxt=head[v];head[v]=cnt;
}
bool bfs()
{
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    queue <int> q;
    vis[ss]=1;q.push(ss);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=head[u];~i;i=e[i].nxt)
        {
            int v=e[i].to;
            if(e[i].f&&!vis[v])
            {
                q.push(v);vis[v]=1;
                dis[v]=dis[u]+1;
            }
        }
    }
    return vis[tt];
}
int dfs(int u,int delta)
{
    if(u==tt) return delta;
    int ret=0;
    for(int i=head[u];delta&&~i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(e[i].f&&dis[v]==dis[u]+1)
        {
            int flow=dfs(v,min(e[i].f,delta));
            e[i].f-=flow;
            e[i^1].f+=flow;
            delta-=flow;
            ret+=flow;
        }
    }
    return ret;
}
int Dinic()
{
    int ret=0;
    while(bfs()) ret+=dfs(ss,inf);
    return ret;
}
bool check(int M)
{
    cnt=-1;
    memset(head,-1,sizeof(head));
    int m1=-1,w1=-1;
    for(int i=1;i<=n;++i)
    {
        if(ps[i].l<=M)
        {
            if(ps[i].c==1&&ps[i].p>m1) m1=ps[i].p,w1=i;
        }   
    }
    int ans=0;
    for(int i=1;i<=n;++i)
    if(ps[i].l<=M)
    {
        if(ps[i].c==1&&i!=w1) continue;
        ans+=ps[i].p;
        if(ps[i].c&1)
        {
            adde(ss,i,ps[i].p);
            for(int j=1;j<=n;++j)if(ps[j].l<=M&&(~ps[j].c&1)&&!np[ps[i].c+ps[j].c])
            {
                adde(i,j,inf);
            }
        }
        else adde(i,tt,ps[i].p);
    }
    ans-=Dinic();
    return ans<k;
}
int main()
{
    for(int i=2;i<=200000;++i)if(!np[i])
    for(int j=i*2;j<=200000;j+=i) np[j]=1;
    scanf("%d%d",&n,&k);
    ss=n+1,tt=n+2;
    for(int i=1;i<=n;++i)
    {
        scanf("%d%d%d",&ps[i].p,&ps[i].c,&ps[i].l);
    }
    int L=1,R=n+1,M;
    while(L<R)
    {
        M=L+R>>1;
        if(check(M)) L=M+1;
        else R=M;
    }
    printf("%d",L>n?-1:L);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值