3158: 千钧一发

题目:传送门

题解:

   这是一道很好的题啊...极力推荐

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

   那很明显就是最小割的经典题目啊?!

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

   耐心一点,从题目的要求再入手:

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

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

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

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

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

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

   因此得证。

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

   st向a值为奇数的相连,a值为偶数的向ed相连,容量都为b值;这样子所形成的两个子集里面的点一定都是符合要求的。

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

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

   有什么用呢?自己画几个图便很容易理解:

   这时候我们跑最小割的话,割出来的边就是损失价值的最小值

   用sum-最小割就是答案啊

代码:

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define qread(x) x=read()
using namespace std;
typedef long long LL;
const LL inf=;
LL n,st,ed,sum;
LL A[],B[];
inline LL read()
{
LL f=,x=;char ch;
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return f*x;
}
struct node
{
LL x,y,c,next,other;
}a[];LL len,last[];
void ins(LL x,LL y,LL c)
{
int k1,k2;
k1=++len;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=last[x];last[x]=len; k2=++len;
a[len].x=y;a[len].y=x;a[len].c=;
a[len].next=last[y];last[y]=len; a[k1].other=k2;
a[k2].other=k1;
}
LL list[],h[],head,tail;
bool bt_h()
{
memset(h,,sizeof(h));h[st]=;
list[]=st;head=;tail=;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]== && a[k].c)
{
h[y]=h[x]+;
list[tail++]=y;
}
}
head++;
}
if(h[ed])return true;
return false;
}
LL find_flow(LL x,LL flow)
{
if(x==ed)return flow;
LL s=,t;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==h[x]+ && a[k].c> && flow>s)
{
s+=t=find_flow(y,min(a[k].c,flow-s));
a[k].c-=t;a[a[k].other].c+=t;
}
}
if(s==)h[x]=;
return s;
}
LL gcd(LL a,LL b)
{
return a==?b:gcd(b%a,a);
}
bool pd(LL x,LL y)
{
LL T=x*x+y*y,t=sqrt(T);
if(t*t!=T)return true;
if(gcd(x,y)>)return true;
return false;
}
int main()
{
sum=;
qread(n);
len=;memset(last,,sizeof(last));
for(int i=;i<=n;i++)qread(A[i]);
for(int i=;i<=n;i++){qread(B[i]);sum+=B[i];}
st=n+;ed=st+;
for(int i=;i<=n;i++)
{
if(A[i]%==)ins(st,i,B[i]);
else ins(i,ed,B[i]);
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if((A[i]%==) && (A[j]%==) && !pd(A[i],A[j]))
ins(i,j,inf);
LL ans=;
while(bt_h())ans+=find_flow(st,inf);
printf("%lld\n",sum-ans);
return ;
}

最新文章

  1. selenium高亮显示操作步骤方法
  2. Copy List with Random Pointer
  3. AC自动机算法详解
  4. DOM4J解析xml案例
  5. IAR中 C语言位定义
  6. Functor仿函数
  7. tensorflow dropout函数应用
  8. ASP.NET Core Razor中处理Ajax请求
  9. 在Bootstrap开发框架的前端视图中使用@RenderPage实现页面内容模块化的隔离,减少复杂度
  10. loadView
  11. github学习步骤
  12. Docker MySQL5.5镜像
  13. Codeforces B - Berland National Library
  14. NOIP模拟6
  15. 【转】阿里巴巴技术专家杨晓明:基于Hadoop技术进行地理空间分析
  16. ucenter搭建
  17. json.parse()使用过程中,肯能会出现的问题(Excel表中数据也存在类似问题)
  18. pig(转载)
  19. Azure本月最新活动,速度Mark!
  20. python学习笔记三:函数及变量作用域

热门文章

  1. 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
  2. BZOJ 1057: [ZJOI2007]棋盘制作 悬线法求最大子矩阵+dp
  3. Authentication in asp.net
  4. git相关整理
  5. BZOJ 3160 FFT+Manacher
  6. POJ 1275 差分约束
  7. SSRS故障排除
  8. NPOI 给导出Excel添加简单样式
  9. python 3.x 学习笔记9 (面向对象)
  10. 在sql server数据库的一个表中如何查询共有多少字段