BZOJ4514: [Sdoi2016] Digit pairing

Source: Internet
Author: User
Tags time limit
BZOJ4514: [Sdoi2016] Digit pairingTime Limit:10 Sec Memory limit:128 MB submit:1101 solved:419 [submit][status][discuss]
The following:Divide all the numbers into factorization, divide two numbers if you want to be equal to a prime number, then their decomposition factorization must be a difference of 1, so we can use the number decomposition into the quality of the odd couple of numbers to build a bipartite graph, the odd One (s,i,b[i],0), an even number of the construction of a (i,t, b[i],0). Enumerate the points of the two sets of the bipartite graph, and then determine which of the odd and even different points is only one factor, if only one is a single (I,j,inf,c[i]*c[j]), then the model is completed. (From,to,flow,value) The purpose of the subject is to make the maximum flow and the cost is not less than 0, so that we use SPFA run the longest road, as far as possible to flow flow, the cost is first incremented and then decremented, until less than or equal to zero when we end the cost flow can be, The maximum flow is that we can match the number of the most, flow past a traffic equivalent to a pair of
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <
Algorithm> #include <queue> using namespace std;
#define LL Long Long const int n=505;
const int m=100005;
const int INF=1E9;
int n,tot[n],p[n][n],b[n],a[n];
LL C[n],d[n],v[m],w[m];
int to[m],nxt[m],from[m],lj[n],bef[n],cnt=1,s,t; void ins (int f,int t,ll ww,ll vv) {cnt++,from[cnt]=f,to[cnt]=t,nxt[cnt]=lj[f],lj[f]=cnt,w[cnt]=ww,v[cnt]=vv;} void add (int f,int t,ll ww,ll vv)
{ins (F,T,WW,VV), INS (T,F,0,-VV);}
	BOOL Check (int x,int y) {bool flag=false;
	int Len=max (Tot[x],tot[y]), Lx=1,ly=1; 
			while (Lx<=len&&ly<=len) {if (p[x][lx]!=p[y][ly]) {if (flag==true) return false;
			if (Tot[x]<tot[y]) ly++;
			else lx++;
		Flag=true;
	} else lx++,ly++;
} return true;
	} void Build () {s=0,t=n+1;
		for (int i=1;i<=n;i++) {if (tot[i]%2==1) Add (s,i,b[i],0);
	else Add (i,t,b[i],0); 
} for (int i=1;i<=n;i++) if (tot[i]%2==1) for (int j=1;j<=n;j++)	if (Tot[j]%2==0&&abs (Tot[i]-tot[j]) ==1) if (Check (i,j)) Add (I,j,inf,c[i]*c[j]);
} queue<int>q;
BOOL Inq[n];
	BOOL Spfa () {for (int i=s;i<=t;i++) D[I]=-1E15;
	d[s]=0;
	Q.push (S); while (!
		Q.empty ()) {int X=q.front ();
		Q.pop ();
		Inq[x]=false;
			for (int i=lj[x];i;i=nxt[i]) if (W[i]&&d[to[i]]<d[x]+v[i]) {d[to[i]]=d[x]+v[i];
			Bef[to[i]]=i;
				if (!inq[to[i]]) {Q.push (to[i]);
			Inq[to[i]]=true;
	}}} if (D[T]!=-1E15) return true;
return false;
	} ll Exflow () {ll ans=0,ret=0;
		while (SPFA ()) {LL flow=inf;
		for (int i=t;i!=s;i=from[bef[i]]) flow=min (Flow,w[bef[i]]);
		for (int i=t;i!=s;i=from[bef[i]]) W[bef[i]]-=flow,w[bef[i]^1]+=flow;
		if (ans+d[t]*flow<0) {ret+=ans/(-d[t]);
	else Ans+=d[t]*flow,ret+=flow;
} return ret;
	} int main () {scanf ("%d", &n);
		for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);
		int x=a[i];
		for (int j=2;j<=sqrt (x); j + +) if (x%j==0) while (x%j==0) p[i][++tot[i]]=j,x/=j; if (x>1) pI
	[++tot[i]]=x;
	} for (int i=1;i<=n;i++) scanf ("%d", &b[i]);
	for (int i=1;i<=n;i++) scanf ("%lld", &c[i]);
	Build ();
printf ("%lld", Exflow ());
  }

DescriptionThere are n kinds of numbers, the first number is AI, there are bi, the weights are 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. InputThe first line is 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

A number of rows, up to how many times paired with Sample Input 3
2 4 8
2 200 7
-1-2 1 Sample Output 4 HINT

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

Source

Acknowledgement Menci upload [Submit][status][discuss]

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.