BZOJ3158: 千钧一发

【传送门:BZOJ3158


简要题意:

  给出n个机器,每个机器有a[i]基础值和b[i]价值

  选出一部分机器使得这些机器里面两两至少满足以下两种条件之一:

  1.a[i]2+a[j]2!=T2(T为正整数)

  2.gcd(a[i],a[j])>1

  求出能达到要求的最大价值


题解:

  神最小割

  要求一个最大价值,那么我们可以转换成求损失的价值最小

  但是这里两个子集的分化并不明显

  对于第二个要求,如果两点的a值都为偶数,那么肯定满足

  那如果两个数都为奇数的话,也必定满足要求一,证明如下:

  1、一个奇数的平方%4为1,一个偶数的平方%4为0

  2、两个奇数的平方和%4为2

  3、如果两个奇数的平方和是一个奇数的平方,那么%4应该为1,不符合

  4、如果两个奇数的平方和是一个偶数的平方,那么%4应该为0,不符合

  这样子思考的话,两个子集的分化就较为明显了:

  st向a值为奇数的相连,a值为偶数的向ed相连,容量都为b值;

  这样子所形成的两个子集里面的点一定都是符合要求的。

  最后一步,也是最关键的一步:

  两个子集之间两两匹配,如果当前匹配的两个点是不符合要求的,就将这两个点相连,容量为无限大。

  跑最小割,割出来的边就是损失价值的最小值 用sum-最小割就是答案

  by Cherish_OI

  注意要加long long


参考代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long LL;
struct node
{
    int x,y,next,other;LL c;
}a[2100000];int len,last[3100];
void ins(int x,int y,LL c)
{
    int k1=++len,k2=++len;
    a[k1].x=x;a[k1].y=y;a[k1].c=c;
    a[k1].next=last[x];last[x]=k1;
    a[k2].x=y;a[k2].y=x;a[k2].c=0;
    a[k2].next=last[y];last[y]=k2;
    a[k1].other=k2;
    a[k2].other=k1;
}
int h[3100],list[3100],st,ed;
bool bt_h()
{
    memset(h,0,sizeof(h));h[st]=1;
    list[1]=st;
    int head=1,tail=2;
    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;
            }
        }
        head++;
    }
    if(h[ed]==0) return false;
    else return true;
}
LL findflow(int x,LL f)
{
    if(x==ed) return f;
    int s=0,t;
    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)&&f>s)
        {
            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;
}
LL gcd(LL a,LL b)
{
    if(a==0) return b;
    else return gcd(b%a,a);
}
LL A[3100],B[3100];
bool check(LL x,LL y)
{
    LL c=sqrt(x*x+y*y);
    if(c*c!=x*x+y*y) return false;
    if(gcd(x,y)>1) return false;
    return true;
}
int main()
{
    int n;
    scanf("%d",&n);
    st=0;ed=n+1;
    len=0;memset(last,0,sizeof(last));
    LL sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&A[i]);
        sum+=A[i];
        if(A[i]%2==0) ins(st,i,A[i]);
        else ins(i,ed,A[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(check(A[i],A[j])==true&&(A[i]%2==0)&&(A[j]%2==1))
            {
                ins(i,j,999999999);
            }
        }
    }
    while(bt_h()==true) sum-=findflow(st,999999999);
    printf("%lld\n",sum);
    return 0;
}

 

posted @ 2018-03-27 20:34  Star_Feel  阅读(199)  评论(0编辑  收藏  举报