题意:中文
思路:就是个最大权闭合图,最大利润就是总利润减去最大流
#include <queue>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=100010;
struct edge{
int to,cap,rev;
edge(int a,int b,int c){to=a;cap=b;rev=c;}
};
vector<edge>G[maxn];
int level[maxn],iter[maxn];
void add_edge(int from,int to,int cap){
G[from].push_back(edge(to,cap,G[to].size()));
G[to].push_back(edge(from,0,G[from].size()-1));
}
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int>que;level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
for(unsigned int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}
int dfs(int v,int t,int f){
if(v==t) return f;
for(int &i=iter[v];i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&level[v]<level[e.to]){
int d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t){
int flow=0;
while(1){
bfs(s);
if(level[t]<0) return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,inf))>0) flow+=f;
}
}
int A[maxn];
int main(){
int n,m,u,v,val,sum=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&A[i]),add_edge(0,i,A[i]);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&val);
sum+=val;
add_edge(u,i+n,inf);add_edge(v,i+n,inf);
add_edge(i+n,n+m+1,val);
}
int ans=max_flow(0,n+m+1);
printf("%d\n",sum-ans);
return 0;
}