1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9+7;
const int MAXN = 55100,MAXM = 4*MAXN;
struct Edge{
int to,nex;
int cap,flow;
}edge[MAXM*2];
int fir[MAXN],cur[MAXN],ecnt = 2;
void addedge(int a,int b,int c){
edge[ecnt] = (Edge){b,fir[a],c,0};
fir[a] = ecnt++;
edge[ecnt] = (Edge){a,fir[b],0,0};
fir[b] = ecnt++;
}
int S,T;
int dis[MAXN];
queue<int> q;
bool bfs(){
memset(dis,0,sizeof(dis));
memcpy(cur,fir,sizeof(cur));
while(!q.empty()) q.pop();
dis[S] = 1;
q.push(S);
while(!q.empty()){
int nown = q.front();q.pop();
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to;
if(edge[nowe].cap > edge[nowe].flow && dis[v] == 0){
dis[v] = dis[nown] + 1;
q.push(v);
if(v == T) return 1;
}
}
}
return 0;
}
int dfs(int nown,int limit = inf){
if(limit == 0 || nown == T) return limit;
for(int &nowe = cur[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to;
if(edge[nowe].cap > edge[nowe].flow && dis[v] == dis[nown] + 1){
int f = dfs(v,min(limit,edge[nowe].cap - edge[nowe].flow));
if(f){
edge[nowe].flow+=f;
edge[nowe^1].flow -= f;
return f;
}
}
}
return 0;
}
int dinic(){
int ans = 0,f = 0;
while(bfs()){
while(true){
f = dfs(S);
if(f == 0) break;
else ans+=f;
}
}
return ans;
}
int n,m,a[MAXN];
int u[MAXN],v[MAXN],w[MAXN];
void init(){
scanf("%d %d",&n,&m);
for(int i = 1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i = 1;i<=m;i++){
scanf("%d %d %d",&u[i],&v[i],&w[i]);
}
}
void solve(){//边在前[1,m],点在后 [m+1,m+n]
S = n+m+1,T = n+m+2;
for(int i = 1;i<=n;i++)addedge(m+i,T,a[i]);
for(int i = 1;i<=m;i++){
addedge(S,i,w[i]);
addedge(i,m+u[i],inf),addedge(i,m+v[i],inf);
}
int ans = -dinic();
for(int i = 1;i<=m;i++){
ans += w[i];
}
printf("%d\n",ans);
}
int main(){
init();
solve();
return 0;
}
|