bzoj 3158: 千钧一发

题意:

这里写图片描述

题解:

对称图的最小割裸题。
显然这个图是对称的,所以就暴力枚举建边,然后 B[i] -最大流/2就是答案。
code:

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
int A[1010],B[1010],n;
const int inf=1<<29;
struct node{
    int x,y,next,c,other;
}a[6400000];int last[100000],len=0;
int h[100000],s[100000],st,ed;
void ins(int x,int y,int c)
{
    int k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
    int 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;
}
bool bt_h()
{
    memset(h,0,sizeof(h));
    int l=1,r=2;s[l]=st;h[st]=1;
    while(l!=r)
    {
        int x=s[l];
        for(int i=last[x];i;i=a[i].next)
        {
            int y=a[i].y;
            if(h[y]==0&&a[i].c>0) h[y]=h[x]+1,s[r++]=y;
        }
        l++;
    }
    return h[ed]!=0;
}
int findflow(int x,int f)
{
    if(x==ed) return f;
    int t,ans=0;
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(h[x]+1==h[y]&&a[i].c>0&&ans<f)
        {
            ans+=(t=findflow(y,min(a[i].c,f-ans)));
            a[i].c-=t;a[a[i].other].c+=t;
        }
    }
    if(ans==0) h[x]=0;
    return ans;
}
LL ans=0;
int gcd(int a,int b) {return a==0?b:gcd(b%a,a);}
bool solve(LL a,LL b)
{
    LL tmp=sqrt(a*a+b*b);
    return tmp*tmp!=a*a+b*b;
}
bool check(int a,int b)
{
    if(gcd(a,b)!=1) return true;
    if(solve((LL)a,(LL)b)) return true;
    return false;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&A[i]);
    for(int i=1;i<=n;i++) scanf("%d",&B[i]),ans+=(LL)B[i];
    st=0;ed=2*n+1;
    for(int i=1;i<=n;i++) ins(st,i,B[i]),ins(i+n,ed,B[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            if(i==j) continue;
            if(!check(A[i],A[j])) ins(i,j+n,inf);
        }
    LL ANS=0;
    while(bt_h()) ANS+=(LL)findflow(st,inf);
    ANS/=2;
    printf("%lld\n",ans-ANS);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值