【BZOJ 4514】数字配对:费用流

题目描述

有 n 种数字,第 i 种数字是 ai、有 bi 个,权值是 ci。
若两个数字 ai、aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数,
那么这两个数字可以配对,并获得 ci×cj 的价值。
一个数字只能参与一次配对,可以不参与配对。
在获得的价值总和不小于 0 的前提下,求最多进行多少次配对。

分析

建图比较裸,类似于二分图的建图方法即可
需要注意的是,要求在获得的价值总和不小于 0 的前提下进行配对,所以我们spfa每次跑最长路,如果这一次spfa的产生的费用会导致总费用变成负数,我们就要贪心的去想我们最多可以完成几次配对即可

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 220 * 2,M = N * N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N],ne[M],e[M],idx;
ll w[M],d[N],incf[N],f[M];
int n,m,S,T;
int a[N],b[N];
ll c[N];
int q[N],pre[N];
bool st[N];

void add(int a,int b,ll c,ll d){
    ne[idx] = h[a],e[idx] = b,f[idx] = c,w[idx] = d,h[a] = idx++;
    ne[idx] = h[b],e[idx] = a,f[idx] = 0,w[idx] = -d,h[b] = idx++;
}

bool spfa(){
    int hh = 0,tt = 1;
    memset(d,-0x3f,sizeof d);
    memset(incf,0,sizeof incf);
    q[0] = S,d[S] = 0,incf[S] = INF;
    while(hh != tt){
        int t = q[hh++];
        if(hh == N) hh = 0;
        st[t] = false;
        for(int i = h[t];~i;i = ne[i]){
            int ver = e[i]; 
            if(f[i] && d[ver] < d[t] + w[i]){
                pre[ver] = i;
                d[ver] = d[t] + w[i];
                incf[ver] = min(f[i],incf[t]);
                if(!st[ver]){
                    q[tt++] = ver;
                    if(tt == N) tt = 0;
                    st[ver] = true;
                }
            }
        }
    }
    return incf[T] > 0;
}

void EK(ll &flow,ll &cost){
    flow = cost = 0;
    while(spfa()){
        ll t = incf[T];
        if(cost + t * d[T] >= 0) cost += t * d[T],flow += t;
        else {
            flow += cost / (-d[T]);
            return;
        }
        for(int i = T;i != S;i = e[pre[i] ^ 1]){
            f[pre[i]] -= t;
            f[pre[i] ^ 1] += t;
        }
    }
}

bool isprime(int x){
    for(int i = 2;i <= sqrt(x);i++) if(x % i == 0) return true;
    return false;
}

int main() {
    memset(h,-1,sizeof h);
    read(n);
    for(int i = 1;i <= n;i++) read(a[i]);
    for(int i = 1;i <= n;i++) read(b[i]);
    for(int i = 1;i <= n;i++) read(c[i]);
    S = 0,T = N - 1;    
    for(int i = 1;i <= n;i++) add(S,i,b[i],0);
    for(int i = n + 1;i <= 2 * n;i++) add(i,T,b[i - n],0);
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= n;j++){
            if(a[i] % a[j] || a[i] / a[j] <= 1) continue;
            if(isprime(a[i] / a[j])) continue;
            add(i,n + j,100000000,c[i] * c[j]);
            add(j,i + n,100000000,c[i] * c[j]);
        }
    ll flow = 0,cost = 0; 
    EK(flow,cost);
    dl(flow / 2);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值