题意,一个图,选一个子图,使得 v[i] 边权,a[i]点权
思路:
最小割经典模型,我们把边和源相连流量v[i],点和汇向连流量a[i],中间的依赖用inf相连,那么这样的图最小割的含义就是“不选的边和选了的点的和”,那么这个值最小答案自然最大, 就是答案了
代码:
#include<bits/stdc++.h>
#define PB push_back
#define X first
#define Y second
#define FIO std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef double LD;
typedef pair<int,int> pii;
const int maxn=5e5+10;
const ll inf=1e10+7;
const int MAXN=3100;
ll maze[MAXN][MAXN];
ll gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];
ll sap(int start,int end,int nodenum){
memset(cur,0,sizeof(cur));
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
ll u=pre[start]=start,maxflow=0,aug=-1;
gap[0]=nodenum;
while(dis[start]<nodenum){
loop:
for(int v=cur[u]; v<nodenum; v++)
if(maze[u][v] && dis[u]==dis[v]+1){
if(aug==-1 || aug>maze[u][v])aug=maze[u][v];
pre[v]=u;
u=cur[u]=v;
if(v==end){
maxflow+=aug;
for(u=pre[u]; v!=start; v=u,u=pre[u]){
maze[u][v]-=aug;
maze[v][u]+=aug;
}
aug=-1;
}
goto loop;
}
int mindis=nodenum-1;
for(int v=0; v<nodenum; v++)
if(maze[u][v]&&mindis>dis[v]){
cur[u]=v;
mindis=dis[v];
}
if((--gap[dis[u]])==0)break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
}
int n,m,x,y,z;
ll a[MAXN];
int main(){
FIO;
cin>>n>>m;
ll ans=0;
for(int i=1;i<=n;i++)cin>>a[i],maze[i][n+m+2]=a[i];
for(int i=1;i<=m;i++){
cin>>x>>y>>z;
ans+=z;
maze[n+i][x]=inf;
maze[n+i][y]=inf;
maze[0][n+i]=z;
}
cout<<ans-sap(0,n+m+2,n+m+3)<<endl;
return 0;
}