Click to open link
The maximum weight closure sub-diagram of the comparison template.
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <queue> const int MAXN = 55005;
const int MAXM = 1E6;
const int INF = 0X3F3F3F3F;
using namespace Std;
inline int read () {int x=0,t=1,c;
while (!isdigit (C=getchar ())) if (c== '-') t=-1;
while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar ();
return x*t;
} int head[maxn],cur[maxn],nx[maxm<<1],to[maxm<<1],flow[maxm<<1],ppp=0;
struct Dinic {int DIS[MAXN];
int S, t;
Long long ans;
void Init () {memset (head,-1, sizeof (head));
PPP = 0;
} void Addedge (int u, int v, int c) {to[ppp]=v;flow[ppp]=c;nx[ppp]=head[u];head[u]=ppp++;swap (u,v);
to[ppp]=v;flow[ppp]=0;nx[ppp]=head[u];head[u]=ppp++;
} bool BFS () {memset (DIS,-1, sizeof (DIS));
Dis[s] = 1;
Queue<int> Q;
Q.push (s); while (!
Q.empty ()) {int x = Q.front ();
Q.pop ();
for (int i = head[x]; ~i; i = Nx[i]) {if (Flow[i] && dis[to[i]] = =-1) {Dis[to[i]] = dis[x] + 1; Q.push (to[i]);
}}} return dis[t]! =-1;
} int DFS (int x, int maxflow) {if (x = = T | |!maxflow) {ans + = Maxflow;
return maxflow;
} int ret = 0, F; for (int &i = cur[x]; ~i; i = Nx[i]) {if (dis[to[i] = = Dis[x] + 1 && (f = DFS (To[i], min (Maxflow, flow[i)
))) {ret + = f;
Flow[i]-= f;
FLOW[I^1] + = f;
Maxflow-= f;
if (!maxflow) break;
}} return ret;
} Long long solve (int source, int tank) {s = source;
t = tank;
Ans = 0;
while (BFS ()) {memcpy (cur, head, sizeof (cur));
DFS (S, INF);
} return ans;
}}dinic;
int main () {int n = read (), M = Read ();
int s = 0, t = n + m + 1;
Dinic.init ();
for (int i = 1, p; I <= n; i++) {scanf ("%d", &p); Dinic.
Addedge (M + i, T, p);
A long long cnt = 0;
for (int i = 1; I <= m; i++) {int v1 = read (), V2 = Read (), C = Read ();
CNT + = C; Dinic.
Addedge (S, I, c); Dinic.
Addedge (i, M + v1, INF); Dinic.
Addedge (i, M + v2, INF); } printf ("%lld\ n ", Cnt-dinic.solve (s,t));
return 0; }