Modeling:
最大权闭合子图。
对于每一个项目,将它与两个中转站连INF边,表示他们之间的关系。
从s与每个项目连边,流量为项目的获利,表示不选它会损失的代价。
每个中转站与t连边,表示选它会花费的代价。
2007 can be seen minimal cut paper
No current arc plus optimization, has been TLE. . .
Dinic attached template (the current arc optimization)
#include<cstdio>
#include<cstring>
#include<iostream>
#define For(aa,bb,cc) for(int aa=(bb);aa<=(int)(cc);++aa)
using namespace std;
const int maxn=1e6+10,inf=2147483647;
int n,m,s,t;
int be[maxn],ne[maxn],to[maxn],flow[maxn],e;
int dis[maxn],cur[maxn];
inline void add_edge(int x,int y,int z){
to[++e]=y,ne[e]=be[x],be[x]=e,flow[e]=z;
to[++e]=x,ne[e]=be[y],be[y]=e,flow[e]=0;
}
int bfs(){
For(i,1,n+m+2) dis[i]=-1;
int q[maxn],l=0,r=0;
q[++r]=s,dis[s]=0;
while(l<r){
int k=q[++l];
for(int i=be[k];i;i=ne[i]){
int u=to[i];
if(flow[i] && dis[u]==-1){
dis[u]=dis[k]+1;
q[++r]=u;
}
}
}
return dis[t]!=-1;
}
int dfs(int node,int f){
if(t==node) return f;
int res;
for(int &i=cur[node];i;i=ne[i]){
int u=to[i];
if(flow[i] && dis[u]==dis[node]+1){
if((res=dfs(u,min(f,flow[i])))){
flow[i]-=res;
flow[i^1]+=res;
return res;
}
}
}
return 0;
}
int Dinic(){
int ans=0,tmp;
while(bfs()){
memcpy(cur,be,sizeof be);
while((tmp=dfs(s,inf))) ans+=tmp;
}
return ans;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int x,y,z,res=0;
e=1;
scanf("%d%d",&n,&m);
s=n+m+1,t=n+m+2;
For(i,1,n) scanf("%d",&x),add_edge(i,t,x);
For(i,1,m){
scanf("%d%d%d",&x,&y,&z);
add_edge(i+n,x,inf);
add_edge(i+n,y,inf);
add_edge(s,i+n,z);
res+=z;
}
printf("%d\n",res-Dinic());
return 0;
}