Petya and Graph

http://codeforces.com/contest/1082/problem/G

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

The weight of the ii-th vertex is aiai.

The weight of the ii-th edge is wiwi.

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 nn and mm (1≤n≤103,0≤m≤1031≤n≤103,0≤m≤103) - the number of vertices and edges in the graph, respectively.

The next line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) - the weights of the vertices of the graph.

The following mm lines contain edges: the ii-e edge is defined by a triple of integers vi,ui,wivi,ui,wi (1≤vi,ui≤n,1≤wi≤109,vi≠ui1≤vi,ui≤n,1≤wi≤109,vi≠ui). This triple means that between the vertices vivi and uiui there is an edge of weight wiwi. 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
  1. 4 5
  2. 1 5 2 2
  3. 1 3 4
  4. 1 4 4
  5. 3 4 5
  6. 3 2 2
  7. 4 2 2
output
  1. 8
input
  1. 3 3
  2. 9 7 8
  3. 1 2 1
  4. 2 3 2
  5. 1 3 3
output
  1. 0
Note

In the first test example, the optimal subgraph consists of the vertices 1,3,41,3,4 and has weight 4+4+5−(1+2+2)=84+4+5−(1+2+2)=8. In the second test case, the optimal subgraph is empty.

最大权闭合子图

  1. #include<iostream>
  2. #include<cstring>
  3. #include<string>
  4. #include<cmath>
  5. #include<cstdio>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<vector>
  9. #include<set>
  10. #define maxn 200005
  11. #define MAXN 200005
  12. #define mem(a,b) memset(a,b,sizeof(a))
  13. const int N=;
  14. const int M=;
  15. const long long INF=0x3f3f3f3f3f3f3f3f;
  16. using namespace std;
  17. int n;
  18. struct Edge{
  19. int v,next;
  20. long long cap,flow;
  21. }edge[MAXN*];
  22. int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
  23. int cnt=;
  24. void isap_init()
  25. {
  26. cnt=;
  27. memset(pre,-,sizeof(pre));
  28. }
  29. void isap_add(int u,int v,long long w)
  30. {
  31. edge[cnt].v=v;
  32. edge[cnt].cap=w;
  33. edge[cnt].flow=;
  34. edge[cnt].next=pre[u];
  35. pre[u]=cnt++;
  36. }
  37. void add(int u,int v,long long w){
  38. isap_add(u,v,w);
  39. isap_add(v,u,);
  40. }
  41. bool bfs(int s,int t)
  42. {
  43. memset(dep,-,sizeof(dep));
  44. memset(gap,,sizeof(gap));
  45. gap[]=;
  46. dep[t]=;
  47. queue<int>q;
  48. while(!q.empty())
  49. q.pop();
  50. q.push(t);
  51. while(!q.empty())
  52. {
  53. int u=q.front();
  54. q.pop();
  55. for(int i=pre[u];i!=-;i=edge[i].next)
  56. {
  57. int v=edge[i].v;
  58. if(dep[v]==-&&edge[i^].cap>edge[i^].flow)
  59. {
  60. dep[v]=dep[u]+;
  61. gap[dep[v]]++;
  62. q.push(v);
  63. }
  64. }
  65. }
  66. return dep[s]!=-;
  67. }
  68. long long isap(int s,int t)
  69. {
  70. if(!bfs(s,t))
  71. return ;
  72. memcpy(cur,pre,sizeof(pre));
  73. int u=s;
  74. path[u]=-;
  75. long long ans=;
  76. while(dep[s]<n)
  77. {
  78. if(u==t)
  79. {
  80. long long f=INF;
  81. for(int i=path[u];i!=-;i=path[edge[i^].v])
  82. f=min(f,edge[i].cap-edge[i].flow);
  83. for(int i=path[u];i!=-;i=path[edge[i^].v])
  84. {
  85. edge[i].flow+=f;
  86. edge[i^].flow-=f;
  87. }
  88. ans+=f;
  89. u=s;
  90. continue;
  91. }
  92. bool flag=false;
  93. int v;
  94. for(int i=cur[u];i!=-;i=edge[i].next)
  95. {
  96. v=edge[i].v;
  97. if(dep[v]+==dep[u]&&edge[i].cap-edge[i].flow)
  98. {
  99. cur[u]=path[v]=i;
  100. flag=true;
  101. break;
  102. }
  103. }
  104. if(flag)
  105. {
  106. u=v;
  107. continue;
  108. }
  109. int x=n;
  110. if(!(--gap[dep[u]]))return ans;
  111. for(int i=pre[u];i!=-;i=edge[i].next)
  112. {
  113. if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
  114. {
  115. x=dep[edge[i].v];
  116. cur[u]=i;
  117. }
  118. }
  119. dep[u]=x+;
  120. gap[dep[u]]++;
  121. if(u!=s)
  122. u=edge[path[u]^].v;
  123. }
  124. return ans;
  125. }
  126.  
  127. int main(){
  128. int m,s,t;
  129. cin>>n>>m;
  130. s=,t=n+m+;
  131. int a,b;
  132. long long c;
  133. isap_init();
  134. for(int i=;i<=n;i++){
  135. cin>>a;
  136. add(s,i,a);
  137. }
  138. long long sum=;
  139. for(int i=;i<=m;i++){
  140. cin>>a>>b>>c;
  141. sum+=c;
  142. add(a,i+n,INF);
  143. add(b,i+n,INF);
  144. add(i+n,t,c);
  145. }
  146. n=t+;
  147. cout<<sum-isap(s,t)<<endl;
  148. }

Petya and Graph(最小割,最大权闭合子图)的更多相关文章

  1. 【POJ 2987】Firing (最小割-最大权闭合子图)

    裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...

  2. 洛谷 - P1361 - 小M的作物 - 最小割 - 最大权闭合子图

    第一次做最小割,不是很理解. https://www.luogu.org/problemnew/show/P1361 要把东西分进两类里,好像可以应用最小割的模板,其中一类A作为源点,另一类B作为汇点 ...

  3. [模拟赛FJOI Easy Round #2][T3 skill] (最小割+最大权闭合子图(文理分科模型))

    [题目描述] 天上红绯在游戏中扮演敏剑,对于高攻击低防御的职业来说,爆发力显得非常重要,为此,她准备学习n个技能,每个技能都有2个学习方向:物理攻击和魔法攻击.对于第i个技能,如果选择物理攻击方向,会 ...

  4. BZOJ.1497.[NOI2006]最大获利(最小割 最大权闭合子图Dinic)

    题目链接 //裸最大权闭合子图... #include<cstdio> #include<cctype> #include<algorithm> #define g ...

  5. CodeForces1082G Petya and Graph 最小割

    网络流裸题 \(s\)向点连边\((s, i, a[i])\) 给每个边建一个点 边\((u, v, w)\)抽象成\((u, E, inf)\)和\((v, E, inf)\)以及边\((E, t, ...

  6. HDU 3917 Road constructions(最小割---最大权闭合)

    题目地址:HDU 3917 这题简直神题意... 题目本身就非常难看懂不说..即使看懂了.也对这题意的逻辑感到无语...无论了.. 就依照那题意上说的做吧... 题意:给你n个城市,m个公司.若干条可 ...

  7. [BZOJ1565][NOI2009]植物大战僵尸-[网络流-最小割+最大点权闭合子图+拓扑排序]

    Description 传送门 Solution em本题知识点是用网络流求最大点权闭合子图. 闭合图定义:图中任何一个点u,若有边u->v,则v必定也在图中. 建图:运用最小割思想,将S向点权 ...

  8. 【BZOJ-3438】小M的作物 最小割 + 最大权闭合图

    3438: 小M的作物 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 825  Solved: 368[Submit][Status][Discuss ...

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

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

随机推荐

  1. 术语-服务:PaaS

    ylbtech-术语-服务:PaaS PaaS是Platform-as-a-Service的缩写,意思是平台即服务. 把服务器平台作为一种服务提供的商业模式.通过网络进行程序提供的服务称之为SaaS( ...

  2. JpGraph使用详解之中文乱码解决方法

    在前面的JpGraph使用详解这篇文章,已经对JpGraph的使用方法作了详细的交代,前面说好的,接下来解决中文乱码. JpGraph为什么会出现中文乱码 在JpGraph中默认是要把字符串转成utf ...

  3. 自己写的jQuery拖动滑块

    (function ($) { $.fn.bnSlide = function (options) { var defaults = { colorData: 0, //原始滑道的有效值 maxWid ...

  4. ie下 iframe在页面中显示白色背景 如何去掉的问题

    ie下:

  5. django中视图处理请求方式(FBV、CBV)

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...

  6. linux操作系统3 vi编辑器

    知识内容: 1.Linux基础命令复习 2.vi模式和基本操作 3.vi常用快捷键 一.Linux基础命令复习 目录(文件夹)操作 cd 进入目录 (tab自动补全) pwd 查看当前目录 mkdir ...

  7. django-template-forloop

    forloop.counter0   # 是每次循环的index 红色的div标签,居然可以这样写. ex:第一次循环的结果 <div class="item active" ...

  8. Hive启动异常

    [root@host ~]# hivewhich: no hbase in (/root/app/apache-maven-3.5.2/bin:/usr/local/sbin:/usr/local/b ...

  9. linux获取日志指定行数范围内的内容

    假如我要获取“浅浅岁月拂满爱人袖”到“落入凡尘伤情着我”之间的内容. 1.首先得到他们所在的行号: -n选项显示行号 但是有时候grep后显示的是“匹配到二进制文件(标准输入)”,说明搜索的字符串在某 ...

  10. IE6部分兼容问题

    border-style:dotted 点线 IE6不兼容 (除了solid以外,其它都有兼容问题,不完全一样) a IE6 不支持a以外的所有标签伪类,IE6以上版本支持所有标签的hover伪类. ...