bzoj 4514: [Sdoi2016]数字配对 二分图+费用流

Description
有 n 种数字,第 i 种数字是 ai、有 bi 个,权值是 ci。
若两个数字 ai、aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数,
那么这两个数字可以配对,并获得 ci×cj 的价值。
一个数字只能参与一次配对,可以不参与配对。
在获得的价值总和不小于 0 的前提下,求最多进行多少次配对。
HINT
n≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5
题解:
巧妙的将点拆成二分图。
建图1:按质因数的指数之和的奇偶性分治,因为a[i] / a[j] = p,所以a[i]与a[j]的奇偶性不同构成二分图
建图2:强行拆点,对于i和j配对都连边,利用对称性,ans/2即可

然后跑最大费用流,因为每次增广先用费用更大的边,所以这样跑出来使得总cost>=0的流量就是最大配对数
用miler rabin判定质数,这种简单的版要随时会打

#include<bits/stdc++.h>
using namespace std;
#define maxn 520
#define maxm 40020
#define inf 1e8
#define INF 1e12

typedef long long ll;
struct node{
    int next,to,f,from;
    ll ct;
}e[maxm * 2];
int head[maxn],cnt = 1;
int q[maxn * 20],hh,tt,inq[maxn],S,T,pre[maxn];
int prime[maxm],tot,tag[maxm];
int n,a[maxn],b[maxn],c[maxn],bl[maxn];
ll cost,p,dis[maxn];
int ans;

void init(){
    for (int i = 2 ; i <= 40000 ; i++){
        if ( !tag[i] ) prime[++tot] = i;
        for (int j = 1 ; j <= tot && prime[j] * i <= 40000 ; j++){
            tag[i * prime[j]] = 1;
            if ( (i % prime[j]) == 0 ) break;
        }
    }
}
ll power(ll x,int y){
    ll res = 1;
    while ( y ){
        if ( y & 1 ) res = res * x % p;
        x = x * x % p;
        y >>= 1;
    }
    return res;
}
bool cal(ll x,ll d,ll r){
    x = power(x,d);
    if ( x == 1 || x == p - 1 ) return 1;
    for (int i = 1 ; i <= r ; i++){
        x = x * x % p;
        if ( x == p - 1 ) return 1;
    }
    return 0;
}
inline bool isprime(int x){
    if ( x <= 1 ) return 0;
    if ( x <= 40000 ) return !tag[x];
    int a = x - 1,num = 0; p = x;
    while ( !(a & 1) ) a >>= 1 , num++;
    for (int i = 0 ; i < 10 ; i++){
        int cur = rand() % (p - 1) + 1;
        if ( !cal(cur,a,num) ) return 0; 
    }
    return 1;
}
inline void adde(int x,int y,int c,ll ct){
    e[++cnt].to = y;
    e[cnt].next = head[x];
    e[cnt].from = x;
    e[cnt].f = c;
    e[cnt].ct = ct;
    head[x] = cnt;
    e[++cnt].to = x;
    e[cnt].next = head[y];
    e[cnt].from = y;
    e[cnt].ct = -ct;
    head[y] = cnt;
}
//建图1:按质因数的指数之和的奇偶性分治,因为a[i] / a[j] = p,所以a[i]与a[j]的奇偶性不同构成二分图
/*void build(){
    S = n + 1, T = n + 2;
    for (int i = 1 ; i <= n ; i++){
        int cur = a[i],c = 0;
        for (int j = 1 ; prime[j] * prime[j] <= a[i] ; j++){
            while ( (cur % prime[j]) == 0 ) cur /= prime[j] , c++;
        }
        if ( cur > 1 ) c++;
        bl[i] = c & 1;
    }
    for (int i = 1 ; i <= n ; i++)
        for (int j = 1 ; j <= n ; j++){
            if ( (a[i] % a[j] == 0) && isprime(a[i] / a[j]) ){
                if ( bl[i] ) adde(i,j,inf,(ll)c[i] * c[j]);
                else adde(j,i,inf,(ll)c[i] * c[j]);
            }
        }
    for (int i = 1 ; i <= n ; i++)
        if ( bl[i] ) adde(S,i,b[i],0);
        else adde(i,T,b[i],0);
}*/
//建图2:强行拆点,对于i和j配对都连边,利用对称性,ans/2即可
void build(){
    S = n * 2 + 1, T = n * 2 + 2;
    for (int i = 1 ; i <= n ; i++)
        for (int j = 1 ; j <= n ; j++){
            if ( (a[i] % a[j] == 0) && isprime(a[i] / a[j]) ){
                adde(i,j + n,inf,(ll)c[i] * c[j]);
                adde(j,i + n,inf,(ll)c[i] * c[j]);
            }
        }
    for (int i = 1 ; i <= n ; i++)
        adde(S,i,b[i],0) , adde(i + n,T,b[i],0);
}
bool spfa(){
    for (int i = 1 ; i <= T ; i++) dis[i] = -INF;
    hh = tt = 0 , dis[S] = 0 , q[tt++] = S;
    while ( hh < tt ){
        int x = q[hh++];
        inq[x] = 0;
        for (int i = head[x] ; i ; i = e[i].next){
            if ( e[i].f && dis[e[i].to] < dis[x] + e[i].ct ){
                dis[e[i].to] = dis[x] + e[i].ct;
                pre[e[i].to] = i;
                if ( !inq[e[i].to] ) inq[q[tt++] = e[i].to] = 1;
            }
        }
    }
    return dis[T] != -INF;
}
int dfs(){
    int x = T,delta = inf;
    while ( x != S ){
        delta = min(delta,e[pre[x]].f);
        x = e[pre[x]].from;
    }
    x = T;
    while ( x != S ){
        e[pre[x]].f -= delta , e[pre[x] ^ 1].f += delta;
        x = e[pre[x]].from;
    }
    return delta;
}
int main(){
    freopen("input.txt","r",stdin);
    init();
    scanf("%d",&n);
    for (int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
    for (int i = 1 ; i <= n ; i++) scanf("%d",&b[i]);
    for (int i = 1 ; i <= n ; i++) scanf("%d",&c[i]);
    build();
    while ( spfa() ){
        int d = dfs();
        if ( cost + dis[T] * d >= 0 ) cost += dis[T] * d , ans += d;
        else{
            ans += cost / (-dis[T]);
            break;
        }   
    }
    cout<<ans / 2<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值