有 n 种数字,第 i 种数字是 ai、有 bi 个,权值是 ci。
若两个数字 ai、aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数,
那么这两个数字可以配对,并获得 ci×cj 的价值。
一个数字只能参与一次配对,可以不参与配对。
在获得的价值总和不小于 0 的前提下,求最多进行多少次配对。
标签:queue man tin i++ inline txt include ack mod
题目链接:
一行一个数,最多进行多少次配对
n≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=210; const LL inf=1e18; int n; LL A[maxn],B[maxn],C[maxn]; struct Edge { int from,to; LL cap,flow,cost; }; int m,s,t,inq[2*maxn],p[2*maxn]; LL d[2*maxn],a[2*maxn]; std::vector<Edge> edge; std::vector<int> G[2*maxn]; inline void add_edge(int from,int to,LL cap,LL cost) { edge.push_back((Edge){from,to,cap,0,cost}); edge.push_back((Edge){to,from,0,0,-cost}); m=edge.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool bellmanford(LL &flow,LL &cost) { for(int i=s;i<=t;i++)d[i]=-inf; memset(inq,0,sizeof(inq)); d[s]=0;inq[s]=1;p[s]=0;a[s]=inf; queue<int>qu; qu.push(s); while(!qu.empty()) { int fr=qu.front();qu.pop();inq[fr]=0; int len=G[fr].size(); for(int i=0;i<len;i++) { Edge& e=edge[G[fr][i]]; if(e.cap>e.flow&&d[e.to]<d[fr]+e.cost) { d[e.to]=d[fr]+e.cost; p[e.to]=G[fr][i]; a[e.to]=min(a[fr],e.cap-e.flow); if(!inq[e.to]){qu.push(e.to);inq[e.to]=1;} } } } if(d[t]<=-inf)return false; if(cost+d[t]*a[t]<0) { LL tep=cost/(-d[t]); flow+=tep; return false; } flow+=a[t]; cost+=d[t]*a[t]; int u=t; while(u!=s) { edge[p[u]].flow+=a[t]; edge[p[u]^1].flow-=a[t]; u=edge[p[u]].from; } return true; } inline LL mincostflow() { LL flow=0; LL cost=0; while(bellmanford(flow,cost)); return flow; } inline LL pow_mod(LL x,LL y,LL mod) { LL s=1,base=x; while(y) { if(y&1)s=s*base%mod; base=base*base%mod; y>>=1; } return s; } inline int isprime(LL num) { if(num<=1)return 0; if(num==2)return 1; if(num%2==0)return 0; for(int i=1;i<=50;i++) { LL x=rand()%num; if(x==0)x++; if(pow_mod(x,num-1,num)!=1)return 0; } return 1; } int main() { //freopen("in.txt","r",stdin); scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%lld",&A[i]); for(int i=1;i<=n;i++)scanf("%lld",&B[i]); for(int i=1;i<=n;i++)scanf("%lld",&C[i]); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(A[i]/A[j]<=1||A[i]%A[j])continue; if(isprime(A[i]/A[j])) { add_edge(j,i+n,100000000,(LL)C[i]*C[j]); add_edge(i,j+n,100000000,(LL)C[i]*C[j]); } } } s=0,t=2*n+1; for(int i=1;i<=n;i++)add_edge(s,i,B[i],0),add_edge(n+i,t,B[i],0); printf("%lld\n",mincostflow()/2); return 0; }
标签:queue man tin i++ inline txt include ack mod
原文地址:http://www.cnblogs.com/zhangchengc919/p/6136045.html