可以看做一些物品中某些互相排斥求最大价值。如果这是个二分图的话,就很容易用最小割了。
观察其给出的条件间是否有什么联系。如果两个数都是偶数,显然满足条件二;而若都是奇数,则满足条件一,因为式子列出来发现一定不能写成完全平方数。那么这就是个二分图了。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } #define N 1010 #define inf 1000000000 #define S 0 #define T 1001 int n,p[N],a[N],b[N],ans=0,t=-1; int d[N],cur[N],q[N]; struct data{int to,nxt,cap,flow; }edge[N*N<<1]; void addedge(int x,int y,int z) { t++;edge[t].to=y,edge[t].nxt=p[x],edge[t].cap=z,edge[t].flow=0,p[x]=t; t++;edge[t].to=x,edge[t].nxt=p[y],edge[t].cap=0,edge[t].flow=0,p[y]=t; } bool bfs() { memset(d,255,sizeof(d));d[S]=0; int head=0,tail=1;q[1]=S; do { int x=q[++head]; for (int i=p[x];~i;i=edge[i].nxt) if (d[edge[i].to]==-1&&edge[i].flow<edge[i].cap) { d[edge[i].to]=d[x]+1; q[++tail]=edge[i].to; } }while (head<tail); return ~d[T]; } int work(int k,int f) { if (k==T) return f; int used=0; for (int i=cur[k];~i;i=edge[i].nxt) if (d[k]+1==d[edge[i].to]) { int w=work(edge[i].to,min(f-used,edge[i].cap-edge[i].flow)); edge[i].flow+=w,edge[i^1].flow-=w; if (edge[i].flow<edge[i].cap) cur[k]=i; used+=w;if (used==f) return f; } if (used==0) d[k]=-1; return used; } void dinic() { while (bfs()) { memcpy(cur,p,sizeof(p)); ans-=work(S,inf); } } int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int main() { #ifndef ONLINE_JUDGE freopen("bzoj3158.in","r",stdin); freopen("bzoj3158.out","w",stdout); const char LL[]="%I64d "; #else const char LL[]="%lld "; #endif n=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<=n;i++) ans+=b[i]=read(); memset(p,255,sizeof(p)); for (int i=1;i<=n;i++) if (a[i]&1) for (int j=1;j<=n;j++) if (!(a[j]&1)&&((long long)sqrt(1ll*a[i]*a[i]+1ll*a[j]*a[j]))*((long long)sqrt(1ll*a[i]*a[i]+1ll*a[j]*a[j]))==1ll*a[i]*a[i]+1ll*a[j]*a[j]&&gcd(a[i],a[j])==1) addedge(i,j,inf); for (int i=1;i<=n;i++) if (a[i]&1) addedge(S,i,b[i]); else addedge(i,T,b[i]); dinic(); cout<<ans; return 0; }