bzoj3275: Number

很早nannan就安利了这题,一开始以为是DP/贪心,没想到是网络流

然后呢有一个很重要的性质,由于第一条限制,奇数和奇数不能构成勾股数,然后第二条性质也注定偶数和偶数不会互相影响。

所以就可以把奇偶分成两个集合。

然后最小割

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf=999999999;
const double eps=1e-8;

struct node
{
    int x,y,c,next,other;
}a[410000];int len,last[310000];
void ins(int x,int y,int c)
{
    int k1,k2;
    
    len++;k1=len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
    
    len++;k2=len;
    a[len].x=y;a[len].y=x;a[len].c=0;
    a[len].next=last[y];last[y]=len;
    
    a[k1].other=k2;
    a[k2].other=k1;
}


int st,ed,h[310000],list[310000];
bool bt_h()
{
    memset(h,0,sizeof(h));h[st]=1;
    int head=1,tail=2;list[1]=st;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(a[k].c>0&&h[y]==0)
            {
                h[y]=h[x]+1;
                list[tail]=y;
                tail++;
            }
        }
        head++;
    }
    if(h[ed]==0)return false;
    return true;
}
int findflow(int x,int f)
{
    if(x==ed)return f;
    int s=0;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(a[k].c>0&&h[y]==h[x]+1&&s<f)
        {
            int t=findflow(y,min(a[k].c,f-s));
            s+=t;a[k].c-=t;a[a[k].other].c+=t;
        }
    }
    if(s==0)h[x]=0;
    return s;
}

int ak[310000];
int gcd(int a,int b)
{
    if(a==0)return b;
    return gcd(b%a,a);
}
int main()
{
    int n,sum=0;
    scanf("%d",&n);
    st=n+1,ed=n+2;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&ak[i]);sum+=ak[i];
        if(ak[i]%2==1)ins(st,i,ak[i]);
        else           ins(i,ed,ak[i]);
        for(int j=1;j<i;j++)
        {
            double c=sqrt(double(ak[i]*ak[i])+double(ak[j]*ak[j]));
            if(gcd(ak[i],ak[j])==1&&fabs(c-double(int(c)))<=eps)
                if(ak[i]%2==1)
                    ins(i,j,inf);
                else 
                    ins(j,i,inf);
        }
    }//composition
    
    int ans=0;
    while(bt_h()==true)
    {
        ans+=findflow(st,999999999);
    }
    printf("%d\n",sum-ans);
    return 0;
}

 

posted @ 2018-02-27 21:25  AKCqhzdy  阅读(149)  评论(0编辑  收藏  举报