一道稍微有点变形的题
对每条边,新增一个点代表这条边,该边点与边的两个端点分别连有向边,容量为无穷大,此时就转化为了一个最大权闭合图的模型,该边点的权值为正,而两个端点的权值则为负,按照普通建图方法建图即可。
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <queue> #include <map> #include <set> #define MAXN 55555 #define MAXM 555555 #define INF 1000000007 using namespace std; struct node { int ver; // vertex int cap; // capacity int flow; // current flow in this arc int next, rev; }edge[MAXM]; int dist[MAXN], numbs[MAXN], src, des, n; int head[MAXN], e; void add(int x, int y, int c) { //e记录边的总数 edge[e].ver = y; edge[e].cap = c; edge[e].flow = 0; edge[e].rev = e + 1; //反向边在edge中的下标位置 edge[e].next = head[x]; //记录以x为起点的上一条边在edge中的下标位置 head[x] = e++; //以x为起点的边的位置 //反向边 edge[e].ver = x; edge[e].cap = 0; //反向边的初始容量为0 edge[e].flow = 0; edge[e].rev = e - 1; edge[e].next = head[y]; head[y] = e++; } void rev_BFS() { int Q[MAXN], qhead = 0, qtail = 0; for(int i = 1; i <= n; ++i) { dist[i] = MAXN; numbs[i] = 0; } Q[qtail++] = des; dist[des] = 0; numbs[0] = 1; while(qhead != qtail) { int v = Q[qhead++]; for(int i = head[v]; i != -1; i = edge[i].next) { if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue; dist[edge[i].ver] = dist[v] + 1; ++numbs[dist[edge[i].ver]]; Q[qtail++] = edge[i].ver; } } } void init() { e = 0; memset(head, -1, sizeof(head)); } int maxflow() { int u; int totalflow = 0; int Curhead[MAXN], revpath[MAXN]; for(int i = 1; i <= n; ++i)Curhead[i] = head[i]; u = src; while(dist[src] < n) { if(u == des) // find an augmenting path { int augflow = INF; for(int i = src; i != des; i = edge[Curhead[i]].ver) augflow = min(augflow, edge[Curhead[i]].cap); for(int i = src; i != des; i = edge[Curhead[i]].ver) { edge[Curhead[i]].cap -= augflow; edge[edge[Curhead[i]].rev].cap += augflow; edge[Curhead[i]].flow += augflow; edge[edge[Curhead[i]].rev].flow -= augflow; } totalflow += augflow; u = src; } int i; for(i = Curhead[u]; i != -1; i = edge[i].next) if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break; if(i != -1) // find an admissible arc, then Advance { Curhead[u] = i; revpath[edge[i].ver] = edge[i].rev; u = edge[i].ver; } else // no admissible arc, then relabel this vertex { if(0 == (--numbs[dist[u]]))break; // GAP cut, Important! Curhead[u] = head[u]; int mindist = n; for(int j = head[u]; j != -1; j = edge[j].next) if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]); dist[u] = mindist + 1; ++numbs[dist[u]]; if(u != src) u = edge[revpath[u]].ver; // Backtrack } } return totalflow; } int nt, m; int main() { while(scanf("%d%d", &nt, &m) != EOF) { init(); src = nt + m + 1; des = nt + m + 2; n = des; int u, v, w; for(int i = 1; i <= nt; i++) { scanf("%d", &w); add(i, des, w); } int sum = 0; for(int i = 1; i <= m; i++) { scanf("%d%d%d", &u, &v, &w); sum += w; add(src, nt + i, w); add(nt + i, u, INF); add(nt + i, v, INF); } rev_BFS(); printf("%d\n", sum - maxflow()); } return 0; }