Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n n vertices and m m edges.

The weight of the i i -th vertex is a i  ai .

The weight of the i i -th edge is w i  wi .

A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.

The weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges.

Input

The first line contains two numbers n n and m m (1≤n≤10 3 ,0≤m≤10 3  1≤n≤103,0≤m≤103 ) - the number of vertices and edges in the graph, respectively.

The next line contains n n integers a 1 ,a 2 ,…,a n  a1,a2,…,an (1≤a i ≤10 9  1≤ai≤109 ) - the weights of the vertices of the graph.

The following m m lines contain edges: the i i -e edge is defined by a triple of integers v i ,u i ,w i  vi,ui,wi (1≤v i ,u i ≤n,1≤w i ≤10 9 ,v i ≠u i  1≤vi,ui≤n,1≤wi≤109,vi≠ui ). This triple means that between the vertices v i  vi and u i  ui there is an edge of weight w i  wi . It is guaranteed that the graph does not contain loops and multiple edges.

Output

Print one integer — the maximum weight of the subgraph of the given graph.

Examples

Input
4 5
1 5 2 2
1 3 4
1 4 4
3 4 5
3 2 2
4 2 2
Output
8
Input
3 3
9 7 8
1 2 1
2 3 2
1 3 3
Output
0

题意:让你选一些边,选边的前提是端点都被选了,求最大的边权和-点权和。

思路:这不是BZOJ原题嘛.jpg。比如BZOJ3438:小M的作物BZOJ3894:文理分科。

就是先得到所有的贡献sum,然后减去最小割。因为求最小割的过程,其实是一个取min的过程。

(福利题啊!CF损失50分的感觉

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
const ll inf=1e18;
int N,M,S,T,cnt=; ll ans,maxflow,cap[maxn];
int Laxt[maxn],To[maxn],Next[maxn],vd[maxn],dis[maxn];
void add(int u,int v,ll c)
{
Next[++cnt]=Laxt[u];Laxt[u]=cnt; To[cnt]=v; cap[cnt]=c;
Next[++cnt]=Laxt[v];Laxt[v]=cnt; To[cnt]=u; cap[cnt]=;
}
ll sap(int u,ll flow)
{
if(flow==||u==T) return flow;
ll tmp,delta=;
for(int i=Laxt[u];i;i=Next[i]){
if(dis[u]==dis[To[i]]+&&cap[i]>){
tmp=sap(To[i],min(cap[i],flow-delta));
delta+=tmp; cap[i]-=tmp; cap[i^]+=tmp;
if(delta==flow||dis[S]>T+) return delta;
}
}
vd[dis[u]]--;
if(vd[dis[u]]==) dis[S]=T+;
vd[++dis[u]]++;
return delta;
}
int main()
{
int u,v,x; ll c; scanf("%d%d",&N,&M);
S=; T=N+M+;
for(int i=;i<=N;i++){
scanf("%d",&x);
add(i,T,x);
}
for(int i=;i<=M;i++){
scanf("%d%d%lld",&u,&v,&c); ans+=c;
add(N+i,u,inf); add(N+i,v,inf); add(S,N+i,c);
}
while(dis[S]<=T) maxflow+=sap(S,inf);
printf("%lld\n",ans-maxflow);
return ;
}

CF1082G:G. Petya and Graph(裸的最大闭合权图)的更多相关文章

  1. Codeforces 1082 G - Petya and Graph

    G - Petya and Graph 思路: 最大权闭合子图 对于每条边,如果它选了,那么它连的的两个点也要选 边权为正,点权为负,那么就是求最大权闭合子图 代码: #pragma GCC opti ...

  2. CodeForces 1082 G Petya and Graph 最大权闭合子图。

    题目传送门 题意:现在有一个图,选择一条边,会把边的2个顶点也选起来,最后会的到一个边的集合 和一个点的集合 , 求边的集合 - 点的集合最大是多少. 题解:裸的最大权闭合子图. 代码: #inclu ...

  3. G. Petya and Graph(经典项目与项目消耗问题)(网络流)

    题:https://codeforces.com/contest/1082/problem/G 题意:给定有边权和点权的图,问你选一些边,然sum边-sum点最大(点权被多次用为公共点只会减一次) 分 ...

  4. Petya and Graph/最大权闭合子图、最小割

    原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...

  5. Petya and Graph(最小割,最大权闭合子图)

    Petya and Graph http://codeforces.com/contest/1082/problem/G time limit per test 2 seconds memory li ...

  6. NEERC 2016-2017 Probelm G. Game on Graph

    title: NEERC 2016-2017 Probelm G. Game on Graph data: 2018-3-3 22:25:40 tags: 博弈论 with draw 拓扑排序 cat ...

  7. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  8. Nebula Graph 技术总监陈恒:图数据库怎么和深度学习框架进行结合?

    引子 Nebula Graph 的技术总监在 09.24 - 09.30 期间同开源中国·高手问答的小伙伴们以「图数据库的设计和实践」为切入点展开讨论,包括:「图数据库的存储设计」.「图数据库的计算设 ...

  9. CF1082G Petya and Graph(最小割,最大权闭合子图)

    QWQ嘤嘤嘤 感觉是最水的一道\(G\)题了 顺便记录一下第一次在考场上做出来G qwqqq 题目大意就是说: 给你n个点,m条边,让你选出来一些边,最大化边权减点权 \(n\le 1000\) QW ...

随机推荐

  1. #C++初学记录(高精度运算)(加法)

    高精度运算 不管是int还是double亦或者long long ,这些定义变量都有数据范围的一定限制,在计算位数超过十几位的数,也就是超过他们自身的数据范围时,不能采用现有类型进行计算,只能自己通过 ...

  2. zw版【转发·台湾nvp系列Delphi例程】HALCON SetIcon2

    zw版[转发·台湾nvp系列Delphi例程]HALCON SetIcon2 procedure TForm1.Button1Click(Sender: TObject);var img : HUnt ...

  3. 《FontForge常见问题FAQ》字王翻译版

    <FontForge常见问题FAQ> 字王翻译版 原文: http://fontforge.github.io/en-US/faq/ 翻译: 字王·中国   blog: http://bl ...

  4. ng-深度学习-课程笔记-11: 卷积神经网络(Week1)

    1 边缘检测( edage detection ) 下图是垂直边缘检测的例子,实际上就是用一个卷积核进行卷积的过程. 这个例子告诉我们,卷积可以完成垂直方向的边缘检测.同理卷积也可以完成水平方向的边缘 ...

  5. python webdriver 登录163邮箱发邮件加附件, 外加数据和程序分离,配置文件的方式

    配置文件:UiObjectMapSendMap.ini用来存放配置信息 GetOptionSendMail.py 用来读取配信息 #encoding=utf-8from selenium.webdri ...

  6. 20145118 《Java程序设计》 实验报告一

    实验一 JAVA开发环境的熟悉(LINUX + ECLIPSE) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 3.统计自己的P ...

  7. Linux硬盘扩容(非LVM)

    环境说明: 虚拟机:Centos6 [root@elements ~]# cat /etc/redhat-release CentOS release 6.10 (Final) [root@eleme ...

  8. linux下如何kill tty终端

    答:一共有两个步骤,如下: 1.列出打开的终端 who 2.kill需要kill的tty终端 pkill -kill -t pts/2

  9. CCleaner如何禁用开机自动启动

    https://forum.piriform.com/topic/42073-ccleaner-starts-on-startup/ 在options-->setting里面选择开机不启动 在O ...

  10. Codeforces Round #408 (Div. 2) C. Bank Hacking

    http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...