Bzoj 4514: [Sdoi2016] Digital pairing (two-point + cost stream) __ Network streaming

Source: Internet
Author: User
Tags time limit

4514: [Sdoi2016] Digital pairing Time Limit:10 Sec Memory limit:128 MB
submit:1191 solved:446
[Submit] [Status] [Discuss] Description has n kinds of numbers, the first number is AI, there are bi, the weight is CI. If two digital AI, AJ satisfied, AI is a multiple of AJ, and Ai/aj is a prime, then these two numbers can be paired, and get CIXCJ value. A number can only participate in a single pairing, and may not participate in the pairing. If the sum of the values obtained is not less than 0, ask for the maximum number of pairs. Input First line an integer n. The second row n integers a1, A2 、......、 an. The third row n integers b1, B2 、......、 bn. The four rows n integers c1, C2 、......、 CN. Output

A number of lines, up to how many times pairing 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

Acknowledgements Menci Upload [submit][status][discuss]

Solving: two points + cost flow

#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <
cstdio> #include <queue> #define N 160003 #define M 203 #define LL-long using namespace std;
Const LL INF=1E18;
int A[m],b[m],c[m],n,pd[10000003],tot,ans;
int point[n],next[n],v[n],remain[n];
int point1[n],next1[n],v1[n],remain1[n];
LL Val[n],val1[n],cost,dis[n];
int last[n],can[n],s,t,ck,prime[10000000];
	void Add (int x,int y,int z,ll k) {tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; val[tot]=k; tot++; Next[tot]=point[y]; Point[y]=tot; V[tot]=x; remain[tot]=0;
Val[tot]=-k;
cout<<x<< "" <<y<< "" <<z<< "" <<k<<endl;}
	int addflow (int s,int t) {int now=t; int ans=1000000000;
		while (now!=s) {ans=min (Ans,remain[last[now]);
	NOW=V[LAST[NOW]^1];
	} now=t;
		while (now!=s) {Remain[last[now]]-=ans;
		Remain[last[now]^1]+=ans;
	NOW=V[LAST[NOW]^1];
return ans; BOOL SPFA (int s,int t) {for (int i=1;i<=t;i++) dis[i]=-inf,can[i]=0; dis[s]=0;
	Can[s]=1; Queue<int> p;
	P.push (s);
	if (ans==6279443)//printf ("!\n");
		while (!p.empty ()) {int Now=p.front (); P.pop (); for (int i=point[now];i!=-1;i=next[i]) if (Dis[v[i]]<dis[now]+val[i]&&remain[i]) {Dis[v[i]]=dis[now]+va
		 	L[i];
		 	Last[v[i]]=i; 
		 		if (!can[v[i]]) {can[v[i]]=1;
			 P.push (V[i]);
	}} can[now]=0;
	} if (Dis[t]==-inf) return false;
	int Mx=addflow (S,T);
	ANS+=MX;
	cout<<dis[t]<< "" <<mx<<endl;
	cost+= (LL) mx* (LL) dis[t];
	if (cost<0) return false;
return true;
	BOOL Check (int x) {ans=0; cost=0;
	memcpy (point) (point,point1,sizeof);
	memcpy (Next,next1,sizeof (next));
	memcpy (v,v1,sizeof (v));
	memcpy (remain,remain1,sizeof (remain));
	memcpy (Val,val1,sizeof (Val)); remain[ck]=0;
	Remain[ck^1]=x;
	while (SPFA (s,t));
	cout<<ans<< "" <<cost<<endl;
	if (x==ans&&cost>=0) return true;
else return false; VoID Calc () {pd[0]=1; pd[1]=1;
		for (int i=2;i<=10000000;i++) {if (!pd[i]) {prime[++prime[0]]=i;
			for (int j=1;j<=prime[0];j++) {if (prime[j]*i>10000000) break;
			Pd[i*prime[j]]=1;
		if (i%prime[j]==0) break;
	the bool pri (int x) {for (int i=2;i*i<=x;i++) if (x%i==0) is return false;
return true;
	int main () {freopen ("pair.in", "R", stdin);
	Freopen ("Pair.out", "w", stdout); scanf ("%d", &n);
	int sum=0;
	for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
	for (int i=1;i<=n;i++) scanf ("%d", &b[i]), sum+=b[i];
	for (int i=1;i<=n;i++) scanf ("%d", &c[i]);
	Tot=-1;
	memset (point) (point,-1,sizeof);
	Calc (); S=1;
	t=n*2+2;
		for (int i=1;i<=n;i++) {Add (s,i+1,b[i],0);
		Add (i+n+1,t,b[i],0);
		 	for (int j=1;j<=n;j++) if (i!=j&&a[i]%a[j]==0) {int t=a[i]/a[j];
		 		if (t<=10000000) {if (!pd[t]) Add (I+1,j+n+1,b[i), (LL) c[i]* (LL) c[j]), add (J+1,i+n+1,b[j), (LL) c[i]* (LL) c[j]); cout<<c[i]<< "" <<c[j]<< "" << (LL) c[i]* (LL) c[j]<<endl;
			 Continue
		 if (PRI (t)) Add (I+1,j+n+1,b[i], (LL) c[i]* (LL) c[j]), add (J+1,i+n+1,b[j), (LL) c[i]* (LL) c[j]);
	} add (t,t+1,0,0);
	t++;
	Ck=tot;
	memcpy (point) (point1,point,sizeof);
	memcpy (next1,next,sizeof (NEXT1));
	memcpy (v1,v,sizeof (v1));
	memcpy (remain1,remain,sizeof (remain));
	memcpy (Val1,val,sizeof (Val)); int l=0; int r=sum;
	int ans=0;
	while (l<=r) {int mid= (L+R)/2;
		cout<<mid<<endl;
		if (check (mid)) ans=mid,l=mid+1;
	else r=mid-1;
printf ("%d\n", ANS/2); }



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.