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

时间:2023-03-09 09:27:46
Petya and Graph(最小割,最大权闭合子图)

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;
}