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. 前端基础(CSS)

    CSS 语法 .clearfix:after{ content: ""; display: block; clear: both; } 解决 float 块之后的塌陷(后面增加了一 ...

  2. ruby中的预定义变量(Predifined Variables)

  3. Win7系统(台式机)设置系统的窗口背景色(豆沙绿色)

    Win7系统(台式机)设置系统的窗口背景色(豆沙绿色) 1,桌面->右键->个性化->窗口颜色->高级外观设置->项目选择(窗口).颜色1(L)选择(其它)将色调改为:8 ...

  4. Python3.6(windows系统)解决编码问题

    Python3.6(windows系统)解决编码问题 1,py文件代码: import urllib.request url = "http://www.douban.com/" ...

  5. 课堂练习——Hash 20162305

    课堂练习--Hash 20162305 课堂练习要求 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合: ...

  6. Pandas数据分析python环境说明文档

    1. 要求windows系统 2. pycharm编程环境并要求配置好python3.x环境 pycharm可在官网下载,下面是链接. https://www.jetbrains.com/zh/pyc ...

  7. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 解决方法

    参考:CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 环境 主系统 OS X,虚拟机,Ubuntu 14.04 64bit. 问题描述 ...

  8. "CMAKE_CXX_COMPILER-NOTFOUND"

    CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CM ...

  9. [osg][osgearth]osg的分页加载,代码和结构图

    DatabasePager加载数据的过程: 多线程 使用DatabasePager加载数据的流程: 左侧的图框表示数据的检索和输入, 中间的白色框表示用于数据存储的内存空间,而右边的图框表示存储数据的 ...

  10. JVM史上最佳入门指南

    提到Java虚拟机(JVM),可能大部分人的第一印象是“难”,但当让我们真正走入“JVM世界”的时候,会发现其实问题并不像我们想象中的那么复杂.唯一真正令我们恐惧的,其实是恐惧本身.而作为整个JVM系 ...