BZOJ 3158 千钧一发/BZOJ 3275 Number

Posted on 2016-09-28 16:17  ziliuziliu  阅读(124)  评论(0编辑  收藏  举报

2333333

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#define maxn 10050
#define maxv 10050
#define maxe 000500
#define inf 1061109567
using namespace std;
struct edge
{
    long long v,f,nxt;
}e[maxe];
long long n,p=0,qq=0,a[maxn],w[maxn],ans=0,s,t,nume=1,g[maxv];
long long dis[maxv];
bool vis[maxv];
queue <long long> q;
void addedge(long long u,long long v,long long f)
{
    e[++nume].v=v;
    e[nume].f=f;
    e[nume].nxt=g[u];
    g[u]=nume;
    e[++nume].v=u;
    e[nume].f=0;
    e[nume].nxt=g[v];
    g[v]=nume;
}
long long gcd(long long a,long long b)
{
    if (b==0) return a;
    return gcd(b,a%b);
}
bool check(long long x,long long y)
{
    x=a[x];y=a[y];
    long long ret=x*x+y*y,ret1=ret;
    ret=sqrt(ret);
    if ((ret*ret!=ret1) || (gcd(x,y)!=1)) return true;
    return false;
}
bool bfs()
{
    for (long long i=s;i<=t;i++) {dis[i]=inf;vis[i]=false;}
    q.push(s);vis[s]=true;dis[s]=0;
    while (!q.empty())
    {
        long long head=q.front();q.pop();
        for (long long i=g[head];i;i=e[i].nxt)
        {
            long long v=e[i].v;
            if ((e[i].f) && (dis[v]>dis[head]+1))
            {
                dis[v]=dis[head]+1;
                if (!vis[v]) {vis[v]=true;q.push(v);}
            }
        }
    }
    if (dis[t]==inf) return false;
    return true;
}
long long dinic(long long x,long long low)
{
    if (x==t) return low;
    long long ret=0;
    for (long long i=g[x];low && i;i=e[i].nxt)
    {
        long long v=e[i].v;
        if ((e[i].f) && (dis[v]==dis[x]+1))
        {
            long long dd=dinic(v,min(low,e[i].f));
            ret+=dd;low-=dd;
            e[i].f-=dd;e[i^1].f+=dd;
        }
    }
    if (!ret) dis[x]=inf;
    return ret;
}
int main()
{
    scanf("%lld",&n);
    for (long long i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        if (a[i]%2==1) p++;
        else qq++;
    }
    for (long long i=1;i<=n;i++)
    {
        w[i]=a[i];
        ans+=w[i];
    }
    s=0;t=n+1;
    for (long long i=1;i<=n;i++)
    {
        if (a[i]%2) addedge(s,i,w[i]);
        else addedge(i,t,w[i]);
    }
    for (long long i=1;i<=n;i++)
        for (long long j=1;j<=n;j++)
        {
            if ((a[i]%2) && (!(a[j]%2)) && (!check(i,j)))
                addedge(i,j,inf);
        }
    long long max_flow=0;
    while (bfs())
        max_flow+=dinic(s,inf);
    printf("%lld\n",ans-max_flow);
    return 0;
}