价值即等价于给每一个点系数$p_{i}=\pm 1$,使得$\forall (x,y)\in E,p_{x}=p_{y}$的最大的$\sum_{i=1}^{n}p_{i}b_{i}$

如果没有删除(当然可以直接求绝对值),考虑网络流建图:将$b_{i}$分为正负两类,$S$向正的连$2b_{i}$的边,负的向$T$连$-2b_{i}$的边,将图中直接相连的两点连上流量为$\infty$的边,那么若两点不同向,则必须有一个被割掉

考虑删除,可以看作这个这个点不要求正负,同时其也不会帮助连边,因此将一个点拆开,并建$(i,i+n)$一条$a_{i}+|b_{i}|$的边表示删去这个点所付出的代价

同时对于一条边,连$(x+n,y)$和$(y+n,x)$,此时若$(x,x+n)$被删掉,则无法通过$x$走到$y+n$,即实现不帮助连边

最终答案即$\sum_{i=1}^{n}|b_{i}|-最小割$,时间复杂度为$o(n^{3

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 605
4 #define oo 0x3f3f3f3f
5 struct ji{
6 int nex,to,len;
7 }edge[N<<2];
8 queue<int>q;
9 int E,n,m,x,y,ans,head[N],a[N],work[N],d[N];
10 void add(int x,int y,int z){
11 edge[E].nex=head[x];
12 edge[E].to=y;
13 edge[E].len=z;
14 head[x]=E++;
15 if (E&1)add(y,x,0);
16 }
17 bool bfs(){
18 memset(d,oo,sizeof(d));
19 q.push(0);
20 d[0]=0;
21 while (!q.empty()){
22 int k=q.front();
23 q.pop();
24 for(int i=head[k];i!=-1;i=edge[i].nex)
25 if ((edge[i].len)&&(d[edge[i].to]==oo)){
26 d[edge[i].to]=d[k]+1;
27 q.push(edge[i].to);
28 }
29 }
30 return d[2*n+1]!=oo;
31 }
32 int dfs(int k,int s){
33 if (k>2*n)return s;
34 for(int &i=work[k];i!=-1;i=edge[i].nex)
35 if ((edge[i].len)&&(d[edge[i].to]==d[k]+1)){
36 int p=dfs(edge[i].to,min(s,edge[i].len));
37 if (p){
38 edge[i].len-=p;
39 edge[i^1].len+=p;
40 return p;
41 }
42 }
43 return 0;
44 }
45 int dinic(){
46 int k,ans=0;
47 while (bfs()){
48 memcpy(work,head,sizeof(head));
49 while (k=dfs(0,oo))ans+=k;
50 }
51 return ans;
52 }
53 int main(){
54 scanf("%d%d",&n,&m);
55 for(int i=1;i<=n;i++)scanf("%d",&a[i]);
56 memset(head,-1,sizeof(head));
57 for(int i=1;i<=n;i++){
58 scanf("%d",&x);
59 ans+=abs(x);
60 add(i,i+n,abs(x)+a[i]);
61 if (x>=0)add(0,i,2*x);
62 else add(i+n,2*n+1,-2*x);
63 }
64 for(int i=1;i<=m;i++){
65 scanf("%d%d",&x,&y);
66 add(x+n,y,oo);
67 add(y+n,x,oo);
68 }
69 printf("%d",ans-dinic());
70 }

[atARC107F]Sum of Abs的更多相关文章

  1. LeetCode_3 sum closet

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  2. Minimum Sum(思维)

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted ...

  3. leetcode16—3 Sum Closet

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  4. Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: ...

  5. ABS函数 去掉金额字段值为负数问题

    )) from OrderDetail

  6. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  7. Leetcode 16. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. 45 个非常有用的 Oracle 查询语句

    ​ 这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快 ...

  9. Color Transfer between Images code实现

    上计算机视觉课老师布置的作业实现论文:Color Transfer between Images 基本思路是: 1.给定srcImg和targetImg 2.将RGB空间转为Lab空间 3.根据论文中 ...

随机推荐

  1. 洛谷4631 [APIO2018] Circle selection 选圆圈 (KD树)

    qwq纪念AC450 一开始想这个题想复杂了. 首先,正解的做法是比较麻烦的. qwqq 那么就不如来一点暴力的东西,看到平面上点的距离的题,不难想到\(KD-Tree\) 我们用类似平面最近点对那个 ...

  2. python 类方法 静态方法

    属性: 公有属性  (属于类,每个类一份) 普通属性  (属于对象,每个对象一份) 私有属性    (属于对象,跟普通属性相似,只是不能通过对象直接访问) 方法:(按作用) 构造方法 析构函数 方法: ...

  3. SingleR如何使用自定义的参考集

    在我之前的帖子单细胞分析实录(7): 差异表达分析/细胞类型注释里面,我已经介绍了如何使用SingleR给单细胞数据做注释,当时只讲了SingleR配套的参考集.这次就讲讲如何使用自己定义/找到的基因 ...

  4. 灵光一闪!帮你使用Vue,搞定无法解决的“动态挂载”

    在一些特殊场景下,使用组件的时机无法确定,或者无法在Vue的template中确定要我们要使用的组件,这时就需要动态的挂载组件,或者使用运行时编译动态创建组件并挂载. 今天我们将带大家从实际项目出发, ...

  5. UltraSoft - Beta - Scrum Meeting 9

    Date: May 25th, 2020. Scrum 情况汇报 进度情况 组员 负责 今日进度 q2l PM.后端 记录Scrum Meeting Liuzh 前端 用户忘记密码界面初稿完成 Kkk ...

  6. Spring Security:Authorization 授权(二)

    Authorization 授权 在更简单的应用程序中,身份验证可能就足够了:用户进行身份验证后,便可以访问应用程序的每个部分. 但是大多数应用程序都有权限(或角色)的概念.想象一下:有权访问你的面向 ...

  7. Verilog设计技巧实例及实现

    Verilog设计技巧实例及实现 1 引言 最近在刷HDLBits的过程中学习了一些Verilog的设计技巧,在这里予以整理.部分操作可能降低代码的可读性和Debug的难度,请大家根据实际情况进行使用 ...

  8. sort命令的学习与实践

    一.用man sort 查看sort的帮助文档 *sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [rocrocket@ro ...

  9. 神经网络 感知机 Perceptron python实现

    import numpy as np import matplotlib.pyplot as plt import math def create_data(w1=3,w2=-7,b=4,seed=1 ...

  10. hdu 1198 Farm Irrigation(并查集)

    题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...