bzoj4514 [Sdoi2016]数字配对

37 篇文章 0 订阅
18 篇文章 0 订阅

Description


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

n≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5

Solution


看到n很小就想到网络流了,有边权就考虑费用流。可以先把a[i]质因数分解得到指数之和,并按照奇偶性作二分图,能整除且指数和差为1的就连边
然后就能二分答案看能否达到限度了,如果建图放在外面还是跑得很快的

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))

typedef long long LL;
typedef std:: pair <int,LL> pair;
const LL inf=1000000000000000LL;
const int INF=0x3f3f3f3f;
const int N=2005;
const int E=500005;

struct edge {int x,y,w; LL c; int next;} e[E];

int queue[N],head,tail;
int pre[N],rec[N],a[N],b[N];
int ls[N],edCnt=1,tot;

LL dis[N],c[N];

bool vis[N];

void addEdge(int x,int y,int w,LL c) {
    e[++edCnt]=(edge) {x,y,w,c,ls[x]}; ls[x]=edCnt;
    e[++edCnt]=(edge) {y,x,0,-c,ls[y]}; ls[y]=edCnt;
}

bool spfa(int st,int ed) {
    head=1; tail=0;
    rep(i,0,st) dis[i]=-inf; dis[st]=0;
    rep(i,0,st) vis[i]=0; vis[st]=1;
    rep(i,0,st) pre[i]=0;
    queue[++tail]=st;
    while (head<=tail) {
        int now=queue[head++];
        for (int i=ls[now];i;i=e[i].next) {
            if (e[i].w>0&&dis[now]+e[i].c>dis[e[i].y]) {
                dis[e[i].y]=dis[now]+e[i].c;
                pre[e[i].y]=i;
                if (vis[e[i].y]) continue;
                queue[++tail]=e[i].y;
                vis[e[i].y]=1;
            }
        }
        vis[now]=0;
    }
    return dis[ed]!=-inf;
}

pair modify(int ed) {
    int mn=INF; LL cost=0;
    for (int i=ed;pre[i];i=e[pre[i]].x) {
        mn=std:: min(mn,e[pre[i]].w);
        cost+=e[pre[i]].c;
    }
    for (int i=ed;pre[i];i=e[pre[i]].x) {
        e[pre[i]].w-=mn; e[pre[i]^1].w+=mn;
    }
    return pair(mn,cost*(LL)mn);
}

pair mcf(int st,int ed) {
    pair ret(0,0);
    while (spfa(st,ed)) {
        pair tmp=modify(ed);
        ret.first+=tmp.first;
        ret.second+=tmp.second;
    }
    return ret;
}

void build_graph(int n,int lim) {
    e[edCnt^1].w=lim; e[edCnt].w=0;
    for (int i=2;i<=edCnt;i+=2) {
        e[i].w+=e[i^1].w;
        e[i^1].w=0;
    }
}

bool check(int n,int lim) {
    build_graph(n,lim);
    pair tmp=mcf(n+2,n+1);
    return (tmp.second>=0)&&(tmp.first==lim);
}

int solve(int n) {
    int l=1,r=tot;
    while (l<=r) {
        int mid=(l+r)>>1;
        if (check(n,mid)) l=mid+1;
        else r=mid-1;
    }
    return l-1;
}

int cal(int n) {
    int ret=0;
    for (int i=2;i*i<=n;i++) {
        if (n%i) continue;
        for (;n%i==0;n/=i,ret++);
    }
    if (n!=1) ret++;
    return ret;
}

int main(void) {
    int n; scanf("%d",&n);
    rep(i,1,n) scanf("%d",&a[i]);
    rep(i,1,n) scanf("%d",&b[i]);
    rep(i,1,n) scanf("%lld",&c[i]);
    rep(i,1,n) rec[i]=cal(a[i]);
    rep(i,1,n) tot+=b[i];
    rep(i,1,n) {
        if (rec[i]%2) {
            addEdge(i,n+1,b[i],0);
            continue;
        }
        addEdge(0,i,b[i],0);
        rep(j,1,n) if (rec[j]%2) {
            if (a[i]%a[j]==0&&rec[j]+1==rec[i]) {
                addEdge(i,j,INF,c[i]*c[j]);
            } else if (a[j]%a[i]==0&&rec[i]+1==rec[j]) {
                addEdge(i,j,INF,c[i]*c[j]);
            }
        }
    }
    addEdge(n+2,0,0,0);
    int prt=solve(n);
    printf("%d\n",prt);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值