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 的倍数,且 ...
随机推荐
- 简单手机端头部设置 及css代码
<html> <head> <title>今日报表</title> <meta http-equiv="Content-Type&quo ...
- erlang中的图片下载
问题如题,这是在一个群里问的一个的问题.其实就是http的Server的上传下载的功能. ibrowse:start().ibrowse:send_req("http://img1.gti ...
- Java学习之路 第四篇 oop和class (面向对象和类)
本人水平有限,创作本文是为了记录学习和帮助初学者学习,欢迎指正和补充 一.面向对象编程的设计概述 很多同学都在学校学了电脑的编程,现在的书籍大部分都是oop面向对象编程,一个很抽象的的名字,比较难以理 ...
- PeekMessage究竟做了什么?
1.UI线程 2.工作线程 把Delphi里TThread的WaitFor函数转化成C++代码,就会是下面这个样子. BOOL TThread::WaitFor(HANDLE hThread) { M ...
- access变转换为mysql表工具
1.一个是国外软件,名字叫Access2MySQL,下载地址:http://www.pc6.com/softview/SoftView_7187.html 2.第二款软件是月光博客写的一个小软件:DB ...
- GS踢玩家下线功能
GS踢玩家下线功能 //key:userId, val:nChannelId (当前在线用户) std::map<int, int> m_mapOnLineUserByUid; ///&l ...
- EasyPlayer windows RTSP播放器OCX插件使用说明
鉴于大家对于EasyPlayer插件的使用还不太熟悉,特此写一篇插件的使用文档,供大家参考:EasyPlayer插件有两种,一种是基于IE的ActiveX控件,一种是基于FireFox(也支持多浏览器 ...
- 使用服务端的临时密钥,不依赖阿里js的putFIle--》阿里oss
<!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <title> ...
- 【题解】CF997C Sky Full of Stars
[题解]CF997C Sky Full of Stars 为什么我的容斥原理入门题是这道题????????? \(Part-1\)正向考虑 直接考虑不合法合法的方案吧 所以我们设行有\(i\),列有\ ...
- 实例化Flask的参数和对app的配置
Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...