SDOI 2016 数字配对
题目大意:给定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 数字配对的更多相关文章
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
- 图论(费用流):BZOJ 4514 [Sdoi2016]数字配对
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 820 Solved: 345[Submit][Status ...
- BZOJ 4514: [Sdoi2016]数字配对 [费用流 数论]
4514: [Sdoi2016]数字配对 题意: 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数 ...
- [SDOI2016 Round1] 数字配对
COGS 2221. [SDOI2016 Round1] 数字配对 http://www.cogs.pro/cogs/problem/problem.php?pid=2221 ★★★ 输入文件:m ...
- 【BZOJ4514】【SDOI2016】数字配对 [费用流]
数字配对 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 有 n 种数字,第 i 种数字是 ...
- 【bzoj4514】: [Sdoi2016]数字配对 图论-费用流
[bzoj4514]: [Sdoi2016]数字配对 好像正常的做法是建二分图? 我的是拆点然后 S->i cap=b[i] cost=0 i'->T cap=b[i] cost=0 然后 ...
- BZOJ 4514: [Sdoi2016]数字配对
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1606 Solved: 608[Submit][Statu ...
- SDOI 2016 Round1 Day1
储能表 /* 引自zyz大佬的数学思想 */ #include<cstdio> #include<iostream> using namespace std; typedef ...
- 【BZOJ4514】[Sdoi2016]数字配对 费用流
[BZOJ4514][Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ...
随机推荐
- WPF时间格式化
日期格式化示例: <TextBox Name="txtCreateTime" HorizontalAlignment="Left" Width=&quo ...
- 金融计算器app的下载情况
下图就是一个多月来的下载量情况~话不多说,直接上图哈. 数据来源:百度开发者平台
- js获取url以及截取后边所带参数
var shopId = ""; function GetRequest() { var url = location.search; //获取url中"?"符 ...
- Eclipse启动时发生An internal error occurred during: "Initializing Java Tooling".错误的解决方法
问题描述: Eclipse启动时发生An internal error occurred during: "Initializing JavaTooling".错误的解决方法 解决 ...
- HTML导航栏
先看效果(两种,1:自己写样式,写交互,2.用jQueryUI 的menu),如下图 第一种: 第二种: 第一种样式: 然后就开始准备了,单村用js和css也可以写出来,不过既然有jq ...
- 各大IT技术博客排行榜
cnblogs 积分排名前3000名 http://www.cnblogs.com/ 左侧有推荐博客排行 cppblog http://www.cppblog.com/AllBloggers.aspx ...
- android---shape.xml属性
solid:填充android:color指定填充的颜色 gradient:渐变android:startColor和android:endColor分别为起始和结束颜色, 另外渐变默认的模式为and ...
- 在php中使用strace、gdb、tcpdump调试工具
[转] http://www.syyong.com/php/Using-strace-GDB-and-tcpdump-debugging-tools-in-PHP.html 在php中我们最常使用调试 ...
- mysql 数据库可以非本地访问
GRANT ALL PRIVILEGES ON 数据库名.* TO root@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
- Ruby Gem命令详解
转自:http://www.jianshu.com/p/728184da1699 Gem介绍: Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.o ...