【Codeforces 808F】【网络流】Card Game 题解

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2100000000
#define LL long long
#define clr(x) memset(x, 0, sizeof(x))
#define ms(a, x) memset(x, a, sizeof(x))

using namespace std;

template <class T> inline void read(T &x) {
    int flag = 1; x = 0;
    char ch = (char)getchar();
    while(ch <  '0' || ch >  '9') { if(ch == '-')  flag = -1; ch = (char)getchar(); }
    while(ch >= '0' && ch <= '9') { x = (x<<1)+(x<<3)+ch-'0'; ch = (char)getchar(); }
    x *= flag;
}

const int N = 105, M = 50005, NN = 200000;
int n,p[N],C[N],L[N],h[N],e[M],nxt[M],fl[M],m,l,r,mid,ch[M],tot,f[N],visit[N],ans,now[N];
bool bz[NN+5];
int pr[NN+5];
char c;

inline void add(int x, int y, int f, int o) {
    e[++tot] = y; nxt[tot] = h[x]; fl[tot] = f; ch[tot] = tot+o; h[x] = tot;
}

int aug(int x, int F) {
    visit[x] = tot;
    if(x == n+1) return F;
    for(int i = now[x]; i; i = nxt[i])
        if(fl[i] > 0 && f[x] == f[e[i]]+1 && visit[e[i]] < tot) {
            int tmp = aug(e[i], min(F, fl[i]));
            if(tmp > 0) {
                fl[i] -= tmp;
                fl[ch[i]] += tmp;
                now[x] = i;
                return tmp;
            }
        }
    return now[x] = 0;
}

void flow() {
    for(int i = 0; i <= n+1; i++) now[i] = h[i];
    while (1) {
        tot++;
        int tmp = aug(0, INF);
        if(!tmp) return;
        ans -= tmp;
    }
}

bool check() {
    int tmp = INF;
    for(int i = 0; i <= n+1; i++) if (visit[i] == tot)
        for(int j = h[i]; j; j = nxt[j]) if(fl[j] > 0 && visit[e[j]] < tot && f[e[j]]+1-f[i] < tmp)
            tmp = f[e[j]]+1-f[i];
    if(tmp == INF) return 0;
    for(int i = 0; i <= n+1; i++) if(visit[i] == tot) f[i] += tmp;
    return 1;
}

bool Judge(int x) {
    int t = 0; tot = ans = 0; clr(h);
    for(int i = 1; i <= n; i++) if(L[i] <= x && C[i] == 1 && (!t || p[i] > p[t])) t = i;
    for(int i = 1; i <= n; i++) if(L[i] <= x && !(C[i] == 1 && i != t))
        if(C[i]&1) add(0, i, p[i], 1), add(i, 0, 0, -1), ans += p[i];
        else add(i, n+1, p[i], 1), add(n+1, i, 0, -1), ans += p[i];
    for(int i = 1; i <= n; i++) if(L[i] <= x)
        for(int j = 1; j < i; j++) if(L[j] <= x && !bz[C[j]+C[i]])
            if (C[i]&1) add(i, j, INF, 1), add(j, i, 0, -1);
            else add(j, i, INF, 1), add(i, j, 0, -1);
    clr(f); clr(visit); tot = 0;
    for(flow(); check(); flow());
    return ans >= m;
}

int main() {
    for (int i = 2; i <= NN; i++) {
        if(!bz[i]) pr[tot++] = i;
        for(int j = 0; j < tot && i*pr[j] <= NN; j++) {
            bz[i*pr[j]] = 1;
            if(!(i%pr[j])) break;
        }
    }
    read(n); read(m);
    for(int i = 1; i <= n; i++) read(p[i]), read(C[i]), read(L[i]);
    for(l = 1, r = n+1, mid = (l+r)>>1; l < r; mid = (l+r)>>1)
        if (Judge(mid)) r = mid;
        else l = mid+1;
    if(l > n) printf("-1\n");
    else printf("%d\n",l);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值