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. 【转】Deep Learning(深度学习)学习笔记整理系列之(一)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0  2013-0 ...

  2. sysbench压测mysql基本步骤

    MySQL数据库测试 select   1.先创建数据库test,再准备数据 time /usr/local/sysbench/bin/sysbench --test=oltp --num-threa ...

  3. Cannot find entry file index.android.js in any of the roots:[ Android ]

    Changed the version of react project to a lower one from here npm install -g rninit rninit init [Pro ...

  4. Status Code状态码详解对照表

    状态码 含义 100 客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应.服务器必须在 ...

  5. 1、安装electron

    安装electron安装并非一帆风顺,我有FQ哈,所以网络方面我就不说了,你们不行的话,可以用cnpm,我说的是另一个问题 我是这样解决的,用以下命令就好了 sudo npm install -g e ...

  6. Java的Servlet、Filter、Interceptor、Listener

    写在前面: 使用Spring-Boot时,嵌入式Servlet容器可以通过扫描注解(@ServletComponentScan)的方式注册Servlet.Filter和Servlet规范的所有监听器( ...

  7. 关于Drupal中使用hook_schema建立数据库报错PDOException: SQLSTATE[42000]的解决办法

    报错信息如下:PDOException: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too l ...

  8. SSD-Tensorflow 从工程角度进行配置

    目录 SSD-Tensorflow 工程角度配置 Download from the github 数据集转化tfrecords格式 训练模型(pre-train) 训练方案一 训练方案二 训练方案3 ...

  9. 创建 shiny 应用程序

    R 本身是一个优秀的数据分析和数据可视化平台.然而,我们通常不会将 R 和分析脚本提供给客户,让客户自己运行.数据分析的结果不仅可以在 HTML 网页.PDF 文档或 Word 文档中显示,还可以呈现 ...

  10. Windows服务程序_测试01

    1. #include <stdio.h> #include <Windows.h> #include <tchar.h> #include <process ...