【bzoj4514】 Sdoi2016—数字配对
http://www.lydsy.com/JudgeOnline/problem.php?id=4514 (题目链接)
题意
n个数,每个数值为a[i],有b[i]个,权值为c[i]。若两个数能配对当且仅当a[i]|a[j]并且a[i]/a[j]是一个质数,并获得一个价值c[i]*c[j]。
Solution
这不是费用流板子吗,无脑连边跑费用流,结果就是Wa,调,Wa,调。。。没想到建图建错了,要建成二分图,左集的数质因子个数为奇数,右集的数质因子个数为偶数。那么显然两集中的点不可能存在边。
细节
该开LL的开LL。
代码
// bzoj4514 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #define LL long long #define inf 1ll<<60 #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; const int maxn=210,maxm=1000010; struct edge {int from,to,next;LL w,c;}e[maxm]; int head[maxm],p[maxm],vis[maxm],fa[maxm]; LL f[maxm],dis[maxm],ans; int cnt=1,es,et,n,m,res,a[maxn],b[maxn],c[maxn],col[maxn]; void link(int u,int v,LL w,LL c) { e[++cnt]=(edge){u,v,head[u],w,c};head[u]=cnt; e[++cnt]=(edge){v,u,head[v],0,-c};head[v]=cnt; } bool check(int k) { for (int i=1;i<=p[0];i++) { if (k%p[i]==0) return k==p[i]; if (sqrt(k)<=p[i]) break; } return 1; } bool SPFA() { queue<int> q; for (int i=1;i<=et;i++) dis[i]=-inf,fa[i]=f[i]=0; q.push(es);dis[es]=0;f[es]=inf; while (!q.empty()) { int x=q.front();q.pop();vis[x]=0; for (int i=head[x];i;i=e[i].next) if (e[i].w && dis[e[i].to]<dis[x]+e[i].c) { dis[e[i].to]=dis[x]+e[i].c; f[e[i].to]=min(e[i].w,f[x]); fa[e[i].to]=i; if (!vis[e[i].to]) q.push(e[i].to),vis[e[i].to]=1; } } LL F=f[et]; if (dis[et]<0) F=min(F,-ans/dis[et]); if (F==0 || dis[et]==-inf) return 0; ans+=dis[et]*F;res+=F; for (int i=fa[et];i;i=fa[e[i].from]) e[i].w-=F,e[i^1].w+=F; return 1; } int EK() { res=0; while (SPFA()); return res; } int main() { scanf("%d",&n); for (int i=2;i<=100000;i++) if (!vis[i]) { p[++p[0]]=i; for (int j=i+i;j<=100000;j+=i) vis[j]=1; } memset(vis,0,sizeof(vis)); es=2*n+1,et=es+1; for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=1;i<=n;i++) scanf("%d",&b[i]); for (int i=1;i<=n;i++) scanf("%d",&c[i]); for (int i=1;i<=n;i++) { int tmp=0,x=a[i]; for (int j=1;j<=p[0];j++) { while (x%p[j]==0) x/=p[j],tmp++; if (x==1) break; } if (tmp&1) link(es,i,b[i],0),col[i]=1; else link(i,et,b[i],0); } for (int i=1;i<=n;i++) if (col[i]) { for (int j=1;j<=n;j++) if ((a[i]!=a[j] && a[i]%a[j]==0 && check(a[i]/a[j])) ||(a[i]!=a[j] && a[j]%a[i]==0 && check(a[j]/a[i]))) link(i,j,inf,(LL)c[i]*c[j]); } printf("%d",EK()); return 0; }
This passage is made by MashiroSky.