传送门
这个题是一个最大权封闭子图,具体建图可以看hzwer
然后关于最大权封闭子图的解法可以看这两篇
这篇详细:http://www.cnblogs.com/wuyiqi/archive/2012/03/12/2391960.html
这篇清晰:http://blog.csdn.net/qq_33229466/article/details/51018916
然后应该就懂了。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#define ll long long
using namespace std;
inline int read(){
int x=0;char ch=' ';int f=1;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int inf=0x3f3f3f3f;
struct edge{
int to,next,cap,flow;
}e[1000001];
int n,m,s,t,tot=-1;
int head[55002];
inline void addedge(int x,int y,int l){
e[++tot].to=y;e[tot].next=head[x];e[tot].cap=l;e[tot].flow=0;head[x]=tot;
e[++tot].to=x;e[tot].next=head[y];e[tot].cap=0;e[tot].flow=0;head[y]=tot;
}
int dis[55002];
inline bool bfs(){
memset(dis,-1,sizeof(dis));
queue<int> q;
q.push(s);
dis[s]=0;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=head[x];i!=-1;i=e[i].next){
int u=e[i].to;
if(dis[u]==-1&&e[i].flow<e[i].cap){
dis[u]=dis[x]+1;
q.push(u);
}
}
}
return dis[t]!=-1;
}
inline int dfs(int x,int a){
if(x==t||a==0)return a;
int flow=0,f;
for(int i=head[x];i!=-1;i=e[i].next){
int u=e[i].to;
if(e[i].flow<e[i].cap&&dis[u]==dis[x]+1&&(f=dfs(u,min(a,e[i].cap-e[i].flow)))>0){
e[i].flow+=f;
e[i^1].flow-=f;
flow+=f;
a-=f;
if(a==0)break;
}
}
if(!flow)dis[x]=0;
return flow;
}
inline ll dinic(){
ll flow=0;
while(bfs()){
flow+=dfs(s,inf);
}
return flow;
}
ll sum;
int main(){
memset(head,-1,sizeof(head));
n=read();m=read();s=0;t=n+m+1;
for(int i=1;i<=n;i++){
int p=read();
addedge(i,t,p);
}
for(int i=1;i<=m;i++){
int a=read(),b=read(),c=read();sum+=c;
addedge(s,n+i,c);addedge(n+i,a,inf);addedge(n+i,b,inf);
}
printf("%lld",sum-dinic());
return 0;
}