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. spring基础-01

    IOC : inversion of 缩写, DI:dependency injection 即在调用者中注入被调用者的实例. AOP 面向切面编程,是代理模式的体现.spring默认使用JDK的动态 ...

  2. Bootstrap单按钮的下拉菜单

    简介 把任意一个按钮放入 .btn-group 中,然后加入适当的菜单标签,就可以让按钮作为菜单的触发器了. 插件依赖 按钮式下拉菜单依赖下拉菜单插件 ,因此需要将此插件包含在你所使用的 Bootst ...

  3. cocos进阶教程(5)回调函数和定时器的使用技巧

    cc.CallFunc.create(selector, data) selector:函数名(函数指针) data:参数 table类型 函数写法 function(node,args ) node ...

  4. 常用DOS命令总结

    本文主要参考:http://www.jb51.net/article/12360.htm http://blog.csdn.net/kofterry/article/details/5183110 常 ...

  5. springcloud16---zuul-filter

    package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...

  6. nvm命令行操作命令

    1,nvm nvm list 是查找本电脑上所有的node版本 - nvm list 查看已经安装的版本 - nvm list installed 查看已经安装的版本 - nvm list avail ...

  7. [kata]数值内3和5的倍数的总和求解

    这个题是这样的,方法参数接受一个数值,以3,5为基数,返回小于这个参数的3,5的倍数,加上3,5本身总和. 朋友段帅说头疼,估计是天气原因吧,好起来吧,还得战斗呢.

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

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

  9. HDU 3315 My Brute(二分图最佳匹配+尽量保持原先匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=3315 题意: 有S1到Sn这n个勇士要和X1到Xn这n个勇士决斗,初始时,Si的决斗对象是Xi. 如果Si赢了X ...

  10. MySQL 5.7.17 Windows安装和启动

    1.在官网http://dev.mysql.com/downloads/下载 MySQL Community Server 2.解压后是这个样子(5.7.18解压后没有my-default.ini文件 ...