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
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
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.

最大权闭合子图

 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define maxn 200005
#define MAXN 200005
#define mem(a,b) memset(a,b,sizeof(a))
const int N=;
const int M=;
const long long INF=0x3f3f3f3f3f3f3f3f;
using namespace std;
int n;
struct Edge{
int v,next;
long long cap,flow;
}edge[MAXN*];
int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
int cnt=;
void isap_init()
{
cnt=;
memset(pre,-,sizeof(pre));
}
void isap_add(int u,int v,long long w)
{
edge[cnt].v=v;
edge[cnt].cap=w;
edge[cnt].flow=;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void add(int u,int v,long long w){
isap_add(u,v,w);
isap_add(v,u,);
}
bool bfs(int s,int t)
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=;
dep[t]=;
queue<int>q;
while(!q.empty())
q.pop();
q.push(t);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=pre[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(dep[v]==-&&edge[i^].cap>edge[i^].flow)
{
dep[v]=dep[u]+;
gap[dep[v]]++;
q.push(v);
}
}
}
return dep[s]!=-;
}
long long isap(int s,int t)
{
if(!bfs(s,t))
return ;
memcpy(cur,pre,sizeof(pre));
int u=s;
path[u]=-;
long long ans=;
while(dep[s]<n)
{
if(u==t)
{
long long f=INF;
for(int i=path[u];i!=-;i=path[edge[i^].v])
f=min(f,edge[i].cap-edge[i].flow);
for(int i=path[u];i!=-;i=path[edge[i^].v])
{
edge[i].flow+=f;
edge[i^].flow-=f;
}
ans+=f;
u=s;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(dep[v]+==dep[u]&&edge[i].cap-edge[i].flow)
{
cur[u]=path[v]=i;
flag=true;
break;
}
}
if(flag)
{
u=v;
continue;
}
int x=n;
if(!(--gap[dep[u]]))return ans;
for(int i=pre[u];i!=-;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
{
x=dep[edge[i].v];
cur[u]=i;
}
}
dep[u]=x+;
gap[dep[u]]++;
if(u!=s)
u=edge[path[u]^].v;
}
return ans;
} int main(){
int m,s,t;
cin>>n>>m;
s=,t=n+m+;
int a,b;
long long c;
isap_init();
for(int i=;i<=n;i++){
cin>>a;
add(s,i,a);
}
long long sum=;
for(int i=;i<=m;i++){
cin>>a>>b>>c;
sum+=c;
add(a,i+n,INF);
add(b,i+n,INF);
add(i+n,t,c);
}
n=t+;
cout<<sum-isap(s,t)<<endl;
}

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. Discuz论坛管理的问题汇总

    Discuz论坛在Linux上搭建成功了, 不得不说, 其功能是非常强大的, 可以满足已知的绝大多数的需求. 搭建完成后也有一些问题, 在这里汇总一下, 以便将来查阅. 1. 显示未处理用户信息, 但 ...

  2. 1061 Dating (20 分)

    1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh ...

  3. pyplot 绘图与可视化

    1. 基本使用 #!/usr/bin/env python # coding=utf-8 import matplotlib.pyplot as plt from numpy.random impor ...

  4. 1.scrapyd部署相关问题

    部署scrapy爬虫项目到6800上 启动scrapyd 出现问题 1: scrapyd-deloy -l  未找到相关命令 scrapyd-deploy -l 可以看到当前部署的爬虫项目,但是当我输 ...

  5. jap 事务

    事物传播行为介绍: @Transactional(propagation=Propagation.REQUIRED) :如果有事务, 那么加入事务, 没有的话新建一个(默认情况下) @Transact ...

  6. C# WEB.API 接收并解析保存base64格式的图片

    using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...

  7. IP 别名和辅助 IP 地址

    https://blog.csdn.net/xiewen99/article/details/54729112?utm_source=itdadao

  8. Windows 域用户

    Windows 2000 组及说明 分类: Windows 2000 的组分为Security 和 Distribution 两种. Security 类型是Windows 2000 唯一用于赋予权限 ...

  9. Spring Boot实践——用外部配置填充Bean属性的几种方法

    引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...

  10. 子元素margin-top后,跟父元素一起下沉

    在一个<div>元素中嵌套一个子div,同时设置子div的margin-top,结果,父元素和子元素一起下沉,留出来个空白区域. 原因就是:        一个盒子如果没有上补白(padd ...