题目大意:给定n个数字以及每个数字的个数和权值,将满足条件的数字配对,使得总代价不小于0,且配对最多

最大费用最大流拆点,对于每个点,连一条由S到该点的边,容量为b,花费为0,再连一条到T的边

对于每个合法的配对,连一条容量无穷,费用为ci*cj的边

跑最大费用最大流即可

#include<bits/stdc++.h>
using namespace std;
#define inf (1ll<<50)
#define ll long long
struct edge{
int to,next,flom;
ll cost,cap;
}G[];
int tot=,h[];
int S,T,ans;
ll cost;
int n;
int a[],b[],c[];
void add(int x,int y,ll z,ll c){
tot++;G[tot].to=y;G[tot].next=h[x];h[x]=tot;G[tot].flom=x;G[tot].cap=z;G[tot].cost=c;
}
void ins(int x,int y,ll z,ll c){
add(x,y,z,c);add(y,x,,-c);
}
ll dis[];int vis[],p[];
bool check(int x){
int k=sqrt(x);
for(int i=;i<=k;++i)
if(x%i==)return ;
return ;
}
bool spfa(){
memset(vis,,sizeof(vis));
memset(p,,sizeof(p));
for(int i=;i<=(n<<|);++i)dis[i]=-inf;dis[S]=;
queue<int>Q;Q.push(S);vis[S]=;
while(!Q.empty()){
int u=Q.front();Q.pop();vis[u]=;
for(int i=h[u];i;i=G[i].next){
int v=G[i].to;
if(G[i].cap>&&dis[v]<dis[u]+G[i].cost){
dis[v]=dis[u]+G[i].cost;p[v]=i;
if(!vis[v])vis[v]=,Q.push(v);
}
}
}
return dis[T]>-inf;
}
void mcf(){
int t=T;ll k=inf;
while(t){
t=p[t];k=min(k,G[t].cap);t=G[t].flom;
}
t=T;ll c=;
while(t){
t=p[t];c+=G[t].cost;G[t].cap-=k;G[t^].cap+=k;t=G[t].flom;
}
if(cost+c*k>=){
ans+=k;cost+=c*k;
}else{
k=cost/(-c);
ans+=k;
printf("%d\n",ans/);
exit();
}
}
int main(){
scanf("%d",&n);S=,T=;
for(int i=;i<=n;++i)scanf("%d",&a[i]);
for(int i=;i<=n;++i)scanf("%d",&b[i]);
for(int i=;i<=n;++i)scanf("%d",&c[i]);
for(int i=;i<=n;++i)ins(S,i<<,b[i],);
for(int i=;i<=n;++i)ins(i<<|,T,b[i],);
for(int i=;i<=n;++i)
for(int j=i+;j<=n;++j)
if((a[i]%a[j]==||a[j]%a[i]==)&&a[i]!=a[j]){
int k=a[i]>a[j]?a[i]/a[j]:a[j]/a[i];
if(check(k))ins(i<<,j<<|,inf,1ll*c[i]*c[j]),ins(j<<,i<<|,inf,1ll*c[i]*c[j]);
}
while(spfa())mcf();
printf("%d\n",ans/);
}

SDOI 2016 数字配对的更多相关文章

  1. 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛

    4514: [Sdoi2016]数字配对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 726  Solved: 309[Submit][Status ...

  2. 图论(费用流):BZOJ 4514 [Sdoi2016]数字配对

    4514: [Sdoi2016]数字配对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 820  Solved: 345[Submit][Status ...

  3. BZOJ 4514: [Sdoi2016]数字配对 [费用流 数论]

    4514: [Sdoi2016]数字配对 题意: 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数 ...

  4. [SDOI2016 Round1] 数字配对

    COGS 2221. [SDOI2016 Round1] 数字配对 http://www.cogs.pro/cogs/problem/problem.php?pid=2221 ★★★   输入文件:m ...

  5. 【BZOJ4514】【SDOI2016】数字配对 [费用流]

    数字配对 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 有 n 种数字,第 i 种数字是 ...

  6. 【bzoj4514】: [Sdoi2016]数字配对 图论-费用流

    [bzoj4514]: [Sdoi2016]数字配对 好像正常的做法是建二分图? 我的是拆点然后 S->i cap=b[i] cost=0 i'->T cap=b[i] cost=0 然后 ...

  7. BZOJ 4514: [Sdoi2016]数字配对

    4514: [Sdoi2016]数字配对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1606  Solved: 608[Submit][Statu ...

  8. SDOI 2016 Round1 Day1

    储能表 /* 引自zyz大佬的数学思想 */ #include<cstdio> #include<iostream> using namespace std; typedef ...

  9. 【BZOJ4514】[Sdoi2016]数字配对 费用流

    [BZOJ4514][Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ...

随机推荐

  1. java.io.EOFException

    错误代码如下: 严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException ...

  2. Delphi编程时候诡异地出现ORA-00937错误,记录解决它的思路和方法

    首先需要说明,这个问题的出现需要几个前提:使用微软的Oracle驱动(使用Oracle自己的驱动不会出现这个问题).使用绑定变量法,使用Format等方式拼接SQL也不会出现这个问题,还有一些诡异的规 ...

  3. 改变linux默认配色方案(dircolors和dircolors-solarized使用)

    前言 前几天刚买了阿里云的云服务器,今天使用putty进入服务器,发现linux默认的bash配色实在太丑. 特别是文件夹显示为深蓝色,到了白天,和黑色背景一搭配,根本看不清文字. 好在在github ...

  4. Linux下Keepalived+LVS-DR模式配置高可用负载均衡集群

    一.环境说明:     操作系统:Centos-6.5_x86_64    keepalived软件安装在node2和node3机器上.     实际安装之前,先关闭keepalived节点(node ...

  5. 【leetcode】Remove Nth Node From End of List

    题目简述: Given a linked list, remove the nth node from the end of list and return its head. For example ...

  6. BFS_最短路径

    已知若干个城市的地图,求从一个城市到另一个城市的路径,要求路径中经过的城市最少. #include<iostream> #include<cstring> using name ...

  7. 如何卸载重装docker?

    http://blog.csdn.net/yangzhenping/article/details/43671843

  8. files list file for package 'xxx' is missing final newline

    #!/usr/bin/python # 8th November, 2009 # update manager failed, giving me the error: # 'files list f ...

  9. solr schema.xml文档节点配置

    首先,讲解一下/usr/local/solr/collection1/conf/schema.xml的配置,此文档功能类似于配置索引数据库. Field:类似于数据库字段的属性(此文统一使用用“字段” ...

  10. Codeforces663E Binary Table(FWT)

    题目 Source http://codeforces.com/contest/663/problem/E Description You are given a table consisting o ...