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

来源:互联网 发布:淘宝依视路镜片真假 编辑:程序博客网 时间:2024/05/22 00:20

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 0x3f3f3f3fint 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
原创粉丝点击