1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #define M 200009
  7 #define inf 2139062143
  8 using namespace std;
  9 int cnt=1,n,m,S,T,d[M],q[2*M],f[M],head[M],next[10*M],u[10*M],v[10*M],a[M];
 10 int ans,sum;
 11 bool bfs()
 12 {
 13     memset(d,0,sizeof(d));
 14     int h=0,t=1;
 15     q[1]=S;
 16     d[S]=1;
 17     for(;h<t;)
 18       {
 19         h++;
 20         int p=q[h];
 21         for(int i=head[p];i;i=next[i])
 22           if(!d[u[i]]&&v[i])
 23             {
 24                 d[u[i]]=d[p]+1;
 25                 if(d[T])
 26                   return 1;
 27                 t++;
 28                 q[t]=u[i];
 29             }
 30       }
 31     return 0;
 32 }
 33 int dinic(int s,int f)
 34 {
 35     if(s==T)
 36       return f;
 37     int rest=f;
 38     for(int i=head[s];i&&rest;i=next[i])
 39       if(v[i]&&d[u[i]]==d[s]+1)
 40         {
 41             int now=dinic(u[i],min(rest,v[i]));
 42             if(!now)
 43               d[u[i]]=0;
 44             v[i]-=now;
 45             v[i^1]+=now;
 46             rest-=now;
 47         }
 48     return f-rest;  
 49 }
 50 void jia1(int a1,int a2,int a3)
 51 {
 52     cnt++;
 53     next[cnt]=head[a1];
 54     head[a1]=cnt;
 55     u[cnt]=a2;
 56     v[cnt]=a3;
 57     return;
 58 }
 59 void jia(int a1,int a2,int a3)
 60 {
 61     jia1(a1,a2,a3);
 62     jia1(a2,a1,0);
 63     return;
 64 }
 65 int gcd(int a1,int a2)
 66 {
 67     int a3=a1%a2;
 68     for(;a3;)
 69       {
 70         a1=a2;
 71         a2=a3;
 72         a3=a1%a2;
 73       }
 74     return a2;
 75 }
 76 bool jud(long long a1,long long a2)
 77 {
 78     long long a3=a1*a1+a2*a2,a4=sqrt(a3);
 79     if(a4*a4!=a3)
 80       return 0;
 81     if(gcd(a1,a2)>1)
 82       return 0;
 83     return 1;
 84 }
 85 int main()
 86 {
 87     scanf("%d",&n);
 88     T=2*n+1;
 89     S=0;
 90     for(int i=1;i<=n;i++)
 91       scanf("%d",&a[i]);
 92     for(int i=1;i<=n;i++)
 93       {
 94         int b1;
 95         scanf("%d",&b1);
 96         sum+=b1;
 97         jia(S,i,b1);
 98         jia(i+n,T,b1);
 99       }
100     for(int i=1;i<=n;i++)
101       for(int j=i+1;j<=n;j++)
102         {
103             if(jud(a[i],a[j]))
104               {
105                 jia(i,j+n,inf);
106                 jia(j,i+n,inf);
107               }
108         }
109     for(;bfs();)
110       ans+=dinic(S,0x7fffffff);
111     printf("%d\n",sum-(ans>>1));
112     return 0;
113 }

网络流同连连看。

posted on 2016-03-22 23:21  xiyuedong  阅读(116)  评论(0编辑  收藏  举报