图论(费用流):BZOJ 4514 [Sdoi2016]数字配对
4514: [Sdoi2016]数字配对
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 820 Solved: 345
[Submit][Status][Discuss]
Description
Input
Output
一行一个数,最多进行多少次配对
Sample Input
2 4 8
2 200 7
-1 -2 1
Sample Output
HINT
n≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=;
const int maxm=;
long long INF=1000000000000000LL;
int cnt=,fir[maxn],nxt[maxm],to[maxm],ID[maxn],path[maxn],n;
long long a[maxn],b[maxn],c[maxn],cost[maxm],cap[maxm],dis[maxn],val[maxm]; void addedge(int a,int b,long long c,long long v){
nxt[++cnt]=fir[a];to[cnt]=b;cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
} int S,T;
long long Spfa(){
queue<int>q;
q.push(S);
for(int i=S;i<=T;i++)
dis[i]=-INF;dis[S]=;
while(!q.empty()){
int node=q.front();q.pop();
for(int i=fir[node];i;i=nxt[i])
if(cap[i]&&dis[node]+val[i]>dis[to[i]]){
dis[to[i]]=val[i]+dis[node];
path[to[i]]=i;
q.push(to[i]);
}
} return dis[T]==-INF?INF:dis[T];
} long long Aug(){
int p=T;
long long f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} long long MCMF(){
long long ret=,now=,c,d;
while((d=Spfa())!=INF){
c=Aug();
if(c*d+now<)
return ret-now/d;
else{ret+=c;now+=c*d;}
}
return ret;
} bool Get_ID(int x){
bool ret=true;
for(int i=,m=(int)sqrt(x);i<=m;i++)
if(x%i==){while(x%i==)x/=i,ret=!ret;}
if(x!=)ret=!ret;
return ret;
} bool IS_Prime(int x){
for(int i=,m=(int)sqrt(x);i<=m;i++)
if(x%i==)return false;
return true;
} int main(){
scanf("%d",&n);S=;T=n+;
for(int i=;i<=n;i++)scanf("%lld",&a[i]);
for(int i=;i<=n;i++)scanf("%lld",&b[i]);
for(int i=;i<=n;i++)scanf("%lld",&c[i]);
for(int i=;i<=n;i++)ID[i]=Get_ID(a[i]);
for(int i=;i<=n;i++){
if(!ID[i]){
addedge(S,i,b[i],);addedge(i,S,,);
for(int j=;j<=n;j++)
if(ID[j]){
long long x=a[i],y=a[j];if(x<y)swap(x,y);
if(x%y==&&IS_Prime(x/y)){
addedge(i,j,INF,c[i]*c[j]);
addedge(j,i,,-c[i]*c[j]);
}
}
}
else{addedge(i,T,b[i],);addedge(T,i,,);}
}
printf("%lld\n",MCMF());
return ;
}
图论(费用流):BZOJ 4514 [Sdoi2016]数字配对的更多相关文章
- 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.[SDOI2016]数字配对(费用流SPFA 二分图)
BZOJ 洛谷 \(Solution\) 很显然的建二分图后跑最大费用流,但有个问题是一个数是只能用一次的,这样二分图两部分都有这个数. 那么就用两倍的.如果\(i\)可以向\(j'\)连边,\(j\ ...
- 4514: [Sdoi2016]数字配对 费用流
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4514 思路 EK直接贪心做 <0的时候加上剩余返回 二分图a->b的时候 把b- ...
- 4514: [Sdoi2016]数字配对
Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对 ...
- 【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 的倍数,且 ...
- BZOJ4514[Sdoi2016]数字配对——最大费用最大流
题目描述 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci ...
- bzoj4514: [Sdoi2016]数字配对--费用流
看了一眼题目&数据范围,觉得应该是带下界的费用流 原来想拆点变成二分图,能配对的连边,跑二分图,可行性未知 后来看到另外一种解法.. 符合匹配要求的数要满足:质因子的个数相差为1,且两者可整除 ...
随机推荐
- Maven Integration for Eclipse 正确地址
m2eclipse has moved from sonatype to eclipse. The correct update site is http://download.eclipse.org ...
- Erlang - Download and Install for Linux
1. 下载 Erlang [huey@huey-K42JE erlang]$ wget http://www.erlang.org/download/otp_src_R16B03.tar.gz 2. ...
- WPF 媒体播放器(MediaElement)使用实例(转)
在WPF 中可以使用MediaElement 为应用程序添加媒体播放控件,以完成播放音频.视频功能.由于MediaElement 属于UIElement,所以它同时也支持鼠标及键盘的操作.本篇将使用M ...
- 查询可用的Nuget服务地址
解决访问Nuget源失败问题 查询IP址址 nslookup nuget.org 如失败,通过google 的dns服务器查询 nslookup nuget.org 8.8.8.8 将得到的Ip地址加 ...
- angularjs-ngModel 控制页面的宽度
js NiDialog.open({ windowClass: '', size:'elements', backdrop: 'static', keyboard: false, templateUr ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- iOS如何准确获取通知
iOS获取通知需要注意以下三个地方iOS 设备收到一条推送(APNs),用户点击推送通知打开应用时,应用程序根据状态不同进行处理需在 AppDelegate 中的以下两个方法中添加代码以获取apn内容 ...
- javascript——面向对象程序设计(4)
<script type="text/javascript"> //1.继承 //2.原型链 //3.借用构造函数 //4.组合继承 //5.原型式继承 //6.寄生式 ...
- JS判断浏览器是否支持某一个CSS3属性的方法
var div = document.createElement('div'); console.log(div.style.transition); //如果支持的话, 会输出 "&quo ...
- python中xrange和range的区别
这两个基本上都是在循环的时候用. for i in range(0, 100): print i for i in xrange(0, 100): print i 这两个输出的结果都是一样的,实际上有 ...