Http://www.rqnoj.cn/Problem_556.html
Description
New technologies are impacting the mobile phone communication market. This is both an opportunity and a challenge for major operators. Thu group's CS & T communication company had to make too much preparations on the eve of the next-generation communication technology battle. Only one site was chosen, it is necessary to complete the preliminary market research, site survey, optimization and other projects.
After the preliminary market survey and site survey, the company obtained a total of N addresses that can be used as communication signal transfer stations. Due to the geographical location differences between these addresses, the cost required for building communication transfer stations in different places is also different. Fortunately, after the preliminary investigation, these data are known: the cost required to establish the I-th communication station is Pi (1 ≤ I ≤ n ).
In addition, the company found all the expected user groups, m in total. Information on the I user group is summarized as AI, Bi and CI: These users use the transit station AI to communicate with the transfer station Bi, and the company can benefit from CI. (1 ≤ I ≤ m, 1 ≤ AI, Bi ≤ n)
Thu group's CS & T companies can choose to establish some transfer stations (investment costs) to provide services for some users and obtain benefits (sum of benefits ). So how can we choose the final transfer station to maximize the company's net profit? (Net profit = sum of benefits-sum of input costs)
[Data scale] of 80% of data: n ≤ 200, m ≤.
In 100% of the data, n is less than or equal to 5 000, M is less than or equal to 50 100, 0 is less than or equal to CI is less than or equal to, and 0 is less than or equal to Pi is less than or equal.
[Time limit] 2 S
Input Format
The first line of the input file contains two positive integers n and M.
There are n integers in the second row to describe the establishment cost of each communication station, which are P1, P2 ,...,
Pn.
The following m rows describe the information of the I + (I + 2) Three AI, Bi, and CI user groups.
For the meanings of all variables, see the topic description.
Output Format
Your program only needs to output an integer to the output file, indicating the maximum net profit the company can get.
Sample Input
5 5
1 2 3 4 5
1 2 3
2 3 4
1 3 3
1 4 2
4 5 3
Sample output
4
The biggest closed weight graph is interesting. For details, see the application of the least cut model in the informatics competition. Author: Hu botao
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <queue>#include <algorithm>#include <vector>#include <cstring>#include <stack>#include <cctype>#include <utility>#include <map>#include <string>#include <climits>#include <set>#include <string>#include <sstream>using std::priority_queue;using std::vector;using std::swap;using std::stack;using std::sort;using std::max;using std::min;using std::pair;using std::map;using std::string;using std::cin;using std::cout;using std::set;using std::queue;using std::string;using std::istringstream;using std::getline;const int MAXN(55010);const int MAXE(MAXN+(50010 << 1));const int INFI((INT_MAX-1) >> 1);int first[MAXN];int v[MAXE << 1], lf[MAXE << 1], next[MAXE << 1];int rear;int N;int S, T;void init(){memset(first+1, -1, sizeof(first[0])*N);rear = 0;}void insert(int tu, int tv, int tc){v[rear] = tv;lf[rear] = tc;next[rear] = first[tu];first[tu] = rear++;v[rear] = tu;lf[rear] = 0;next[rear] = first[tv];first[tv] = rear++;}int level[MAXN];bool bfs(){memset(level+1, -1, sizeof(level[0])*N);level[S] = 0;queue<int> que;que.push(S);while(!que.empty()){int cur = que.front();que.pop();for(int i = first[cur]; i != -1; i = next[i])if(lf[i]){int tv = v[i];if(level[tv] == -1){level[tv] = level[cur]+1;que.push(tv);}}}return level[T] != -1;}int dfs(int cur, int limit){if(cur == T)return limit;int tf = 0;for(int i = first[cur]; i != -1; i = next[i])if(lf[i]){int tv = v[i];if(level[tv] == level[cur]+1){int temp = dfs(tv, min(limit-tf, lf[i]));tf += temp;lf[i] -= temp;lf[i^1] += temp;}}if(tf == 0)level[cur] = -1;return tf;}int dinic(){int ret = 0;while(bfs()){ret += dfs(S, INFI);}return ret;}int main(){int n, m;while(~scanf("%d%d", &n, &m)){N = n+m+2;S = N-1;T = N;int temp;init();for(int i = 1; i <= n; ++i){scanf("%d", &temp);insert(i, T, temp);}int limit = N-2;int a, b, c;int ans = 0;for(int i = n+1; i <= limit; ++i){scanf("%d%d%d", &a, &b, &c);ans += c;insert(S, i, c);insert(i, a, INFI);insert(i, b, INFI);}ans -= dinic();printf("%d\n", ans);}return 0;}