bzoj4514 "SDOI2016" digit pairing

Source: Internet
Author: User

4514: [Sdoi2016] digit pairing time limit: ten Sec Memory Limit: MB
Submit: 730 Solved: 311
[Submit] [Status] [Discuss] Description There are n kinds of numbers, the first number is AI, there are bi, the weight is CI. If two digital AI, AJ satisfies, AI is a multiple of AJ, and Ai/aj is a prime number, then these two numbers can be paired and gain CIXCJ value. A number can only participate in a single pairing and may not participate in pairing. The maximum number of pairs to be paired, provided that the sum of the values obtained is not less than 0. Input first line an integer n. The second row n integers a1, A2 、......、 an. The third row n integers b1, B2 、......、 bn. row n integers c1, C2 、......、 CN. Output

Number of rows, maximum number of pairs

Sample Input3
2 4 8
2 200 7
-1-2 1Sample Output4HINT

N≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5

Source

Acknowledgement Menci Upload




Cost Flow the application

For all numbers, it can be divided into two categories, the decomposition of factorization after the exponent and the odd and even. The pairing relationship can only be between two classes, thus constituting a model of a binary graph. You can run the cost stream after building the map.

There is also a limit, the sum of the costs is not less than 0, that is, each step to make the cost as large as possible. So we take all the costs back, and then after each SPFA to determine whether the cost is greater than 0, so that the cost can be limited.

Note ans-= (Cost/dis[t]) on line 81st of the program.




#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath > #include <algorithm> #include <queue> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #define LL long long#define MAXN 210#define maxm 30000#define inf 1000000000000000000llusing namespace St D;int N,s,t,cnt=1,tot,totx,toty,ans;int A[maxn],b[maxn],head[maxn],p[maxn],pri[32005],fx[maxn],fy[maxn];ll C[MAXN ],dis[maxn];bool inq[maxn],vst[32005];struct edge_type{int next,from,to,v;ll c;} E[maxm];inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline bool Judge (int x,int y) {if (!x| |! Y) return false;if (x<y) swap (x, y), if (x%y!=0) return false;x/=y; F (I,1,tot) {if (pri[i]>=x) break;if (x%pri[i]==0) return false;} return true;} inline void Add_edge (int x,int y,int v,ll c) {e[++cnt]= (edge_type) {head[x],x,y,V,-c};head[x]=cnt;e[++cnt]= (Edge_type) {head[y],y,x,0,c};head[y]=cnt;} inline bool SPFA () {queue<int> Q;memset (inq,false,sizeof (INQ)); F (i,1,t) Dis[i]=inf;dis[s]=0;inq[s]=true;q.push (s), while (!q.empty ()) {int X=q.front (); Q.pop (); Inq[x]=false;for ( int i=head[x];i;i=e[i].next) {int y=e[i].to;if (E[I].V&AMP;&AMP;DIS[Y]&GT;DIS[X]+E[I].C) {Dis[y]=dis[x]+e[i].c;p[y] =i;if (!inq[y]) Q.push (y), Inq[y]=true;}}} return dis[t]!=inf;} inline void Mcf () {ll cost=0;while (SPFA ()) {int tmp=1000000000;for (int i=p[t];i;i=p[e[i].from]) tmp=min (TMP,E[I].V); (cost+dis[t]*tmp<=0) {cost+=dis[t]*tmp;ans+=tmp;for (int i=p[t];i;i=p[e[i].from]) e[i].v-=tmp,e[i^1].v+=tmp;} else{ans-= (Cost/dis[t]); return;}} int main () {n=read (); s=n+1;t=n+2; F (I,1,n) a[i]=read (); F (I,1,n) b[i]=read (); F (I,1,n) c[i]=read (); F (i,2,32000) {if (!vst[i]) pri[++tot]=i; F (J,1,tot) {if (i*pri[j]>32000) break;vst[i*pri[j]]=true;if (i%pri[j]==0) break;}} F (i,1,n) {int tmp=a[i],num=0; F (J,1,tot) {while (tmp%pri[j]==0) tmp/=pri[j],num++;if (tmp==1) break;}if (num&1) Fx[++totx]=i;else fy[++toty]=i;} F (i,1,totx) f (j,1,toty) if (judge (A[fx[i]],a[fy[j])) Add_edge (Fx[i],fy[j],1000000000,c[fx[i]]*c[fy[j]]); F (I,1,totx) Add_edge (s,fx[i],b[fx[i]],0); F (I,1,toty) Add_edge (fy[i],t,b[fy[i]],0), MCF ();p rintf ("%d\n", ans);}


bzoj4514 "SDOI2016" digit pairing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.