bzoj 4514: 数字配对
题目大意
题解
我们打表观察规律发现一定能构成一张二分图
也就是不存在奇环
所以我们一般保证费用非负的最大流即可.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 256;
const int inf = 0x3f3f3f3f;
inline bool judge(int n){
if(n == 1) return false;
if(n == 2) return true;
for(int i = 2;i*i<=n;++i){
if(n % i == 0) return false;
}return true;
}
int a[maxn],b[maxn];ll c[maxn];
namespace gra{
struct Edge{
int to,next;
}G[21010];
int head[maxn],cnt;
void add(int u,int v){
//printf("%d\n",cnt);
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
}
inline void insert(int u,int v){
add(u,v);add(v,u);
}
int col[maxn];
#define v G[i].to
void dfs(int u,int c){
col[u] = c;
for(int i = head[u];i;i=G[i].next){
if(col[v] == -1) dfs(v,c^1);
}
}
#undef v
}
namespace net{
struct Edge{
int to,next,cap;
ll cost;
}G[21010];
int head[maxn],cnt=1;
void add(int u,int v,int c,ll co){
//printf("%d\n",cnt);
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
G[cnt].cap = c;
G[cnt].cost = co;
}
inline void insert(int u,int v,int c,ll co){
//printf("%d -> %d (%d,%lld)\n",u,v,c,co);
add(u,v,c,co);add(v,u,0,-co);
}
const int lim = maxn<<2;
int S = maxn-5,T = S+1,l,r,q[lim+10];
int flow[maxn],p[maxn];
ll dis[maxn],nw_dis;
int ans;
bool inq[maxn];
inline int find(int r,ll w){
int l = 0,ret = 0;
while(l <= r){
int mid = l+r >> 1;
if(mid*w+nw_dis >= 0) ret = mid,l = mid+1;
else r = mid-1;
}return ret;
}
#define v G[i].to
bool spfa(){
memset(dis,-0x3f,sizeof dis);
l = 0;r = -1;q[++r] = S;
dis[S] = 0;flow[S] = inf;
inq[S] = true;
while(l <= r){
int u = q[l % lim];++l;
for(int i = head[u];i;i=G[i].next){
if(dis[v] < dis[u] + G[i].cost && G[i].cap){
dis[v] = dis[u] + G[i].cost;
flow[v] = min(flow[u],G[i].cap);
p[v] = i;
if(!inq[v]){
q[++r % lim] = v;
inq[v] = true;
}
}
}inq[u] = false;
}if(dis[T] == dis[0]) return false;
if(dis[T] < 0) flow[T] = find(flow[T],dis[T]);
if(flow[T] == 0) return false;
ans += flow[T];
nw_dis += flow[T]*dis[T];
for(int u = T;u != S;u = G[p[u]^1].to)
G[p[u]].cap -= flow[T],G[p[u]^1].cap += flow[T];
return true;
}
#undef v
}
int main(){
int n;read(n);memset(gra::col,-1,sizeof gra::col);
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]);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(i == j) continue;
if(a[i] % a[j] == 0 && judge(a[i]/a[j])){
gra::insert(i,j);
}
}
}
for(int i=1;i<=n;++i) if(gra::col[i] == -1) gra::dfs(i,0);
for(int u=1;u<=n;++u){
if(gra::col[u] == 0){
net::insert(net::S,u,b[u],0);
for(int i = gra::head[u];i;i=gra::G[i].next){
net::insert(u,gra::G[i].to,inf,c[u]*c[gra::G[i].to]);
}
}else{
net::insert(u,net::T,b[u],0);
}
}while(net::spfa());
printf("%d\n",net::ans);
getchar();getchar();
return 0;
}
bzoj 4514: 数字配对的更多相关文章
- 图论(费用流):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 是一个质数 ...
- BZOJ 4514: [Sdoi2016]数字配对
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1606 Solved: 608[Submit][Statu ...
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
- SDOI 2016 数字配对
题目大意:给定n个数字以及每个数字的个数和权值,将满足条件的数字配对,使得总代价不小于0,且配对最多 最大费用最大流拆点,对于每个点,连一条由S到该点的边,容量为b,花费为0,再连一条到T的边 对于每 ...
- [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 然后 ...
- 【BZOJ4514】[Sdoi2016]数字配对 费用流
[BZOJ4514][Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ...
随机推荐
- jQuery--基础(实例)
jQuery的操作方法并不需要都记下来,但是使用什么功能需要什么样的方法,我们是一定会知道的.所以写实例来进行对功能方法的练习和熟练,是最快能够掌握jQuery的方法. <!DOCTYPE ht ...
- opencv3.3.1 opencv_contribut 3.3.1 git 20180117最新版的在ubuntu1604上的编译
过程: 1. git clone ... contribut 2. git clone ... opencv 3. git checkout -b v3.3.1 4 gi ...
- C#中方法中 ref 和 out的使用
案例1: static void Main() { , , , }; int numLargerThan10,numLargerThan100,numLargerThan1000 ; Proc(ary ...
- Intellj IDEA光标替insert状态,back键无法删除内容
Intellj IDEA光标为insert状态,无法删除内容导入项目后,发现打开java文件的光标是win系统下按了insert键后的那种宽的光标,并且还无法删除内容,且按删除(delete)键也只见 ...
- zookeeper启动流程简单梳理
等着測试童鞋完工,顺便里了下zookeeper的启动流程 zk3.4.6 启动脚本里面 nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_ ...
- linq to xml操作XML(转)
转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...
- C#的默认访问权限(转)
1.在namespace中的类.接口默认是internal类型的,也可以显示的定义为public类型,不允许是其他访问类型.2.在一个类里面,属性和方法默认是private的,可以显示的定义为publ ...
- 搭建vps***
快速搭建ShadowSocks wget --no-check-certificate -O shadowsocks.sh https://raw.githubusercontent.com/tedd ...
- linux卸载软件
rpm -q -a 查询当前系统安装的所有软件包 rpm -e 软件包名 参数e的作用是使rpm进入卸载模式,对名为某某某的软件报名进行卸载 rpm -e 软件包名 -nodeps 由于系统中各个软件 ...
- 解决webpack不能匹配post请求的问题
解决webpack不能匹配post请求的问题 webpack的dev-server只能匹配get请求,在本地做本地数据的时候会很不方便. 可以使用如下两种办法解决: 1.在webpack.config ...