Bzoj 3438: Small M's crop (min cut)

Source: Internet
Author: User

Orz the person cloud God ...

Put on the official puzzle ... Turn into the smallest cut and then build the chart to run the maximum flow on the line ...

------------------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const int MAXN = 3010, inf = int (2e9);struct Edge {int to, cap;Edge*next, *rev;} e[4008000], *pt = E, *HEAD[MAXN]; inline void Add (int u, int v, int w) {pt->to = V, pt->cap = w;pt->next = Head[u];Head[u] = pt++;}inline void Add_edge (int u, int v, int w) {Add (U, V, W), add (V, u, 0);Head[u]->rev = head[v];Head[v]->rev = Head[u];}EDGE*P[MAXN], *CUR[MAXN];int H[MAXN], CNT[MAXN], S, T, N;int Maxflow () {CLR (h, 0), CLR (CNT, 0), cnt[s] = N;Rep (i, N) cur[i] = Head[i];edge*e;int flow = 0;for (int x = S, A = inf; H[s] < N;) {For (e = head[x]; e; e = e->next)if (h[e->to] + 1 = = H[x] && e->cap) break;if (e) {cur[x] = p[e->to] = e;a = min (A, e->cap);x = e->to;if (x = = T) {For (; x! = S; x = p[x]->rev->to) {P[x]->cap-= A;P[x]->rev->cap + = A;}flow + = A;A = inf;}} else {if (!--Cnt[h[x]]) break;h[x] = N;For (e = head[x]; e = e->next) if (H[e->to] + 1 < h[x] && e->cap) {h[x] = h[e->to] + 1;cur[x] = e;}++cnt[h[x]];if (x! = S) x = p[x]->rev->to;}}return flow;}int main () {freopen ("test.in", "R", stdin);int ans = 0, n;CLR (head, 0);cin >> N;S = 0, t = n + 1, n = t + 1;Rep (i, N) {int v;scanf ("%d", &v);ans + = v;Add_edge (S, i + 1, v);}Rep (i, N) {int v;scanf ("%d", &v);ans + = v;Add_edge (i + 1, T, v);}cin >> N;While (n--) {int k, C1, C2, T;scanf ("%d%d%d", &k, &c1, &C2);ans + = c1 + C2;int u = n++, v = n++;Add_edge (S, U, C1), Add_edge (V, T, C2);While (k--) {scanf ("%d", &t);Add_edge (U, T, INF), Add_edge (T, V, INF);}}cout << ans-maxflow () << "\ n";return 0;} 

------------------------------------------------------------------------------------------

3438: small M's crop Time Limit:Ten Sec Memory Limit:+ MB
Submit:474 Solved:226
[Submit] [Status] [Discuss] Description


Background

Small M is also a special love to play MC child paper ...

Describe

Small m in MC opened two huge farmland A and B (you can think the capacity is infinite), now, small p has the seeds of n crops, each crop seed has 1 (that is, can plant a crop) (with 1...N number), now, the first I plant in a planted in a can gain AI benefits, The benefits of BI can be obtained by planting in B, and there is now a magical phenomenon in which some crops grow together in an arable land to gain additional benefits, and little m finds the rule that there is a m crop combination, and that the crops in the first I combination can be c1i in a For additional benefits, Common in B can get c2i extra income, so, small m quickly calculate the biggest benefit of planting, but he wants to test you, can you answer him this question?

Input

The first line includes an integer n

The second line consists of n integers, which represent the AI

The third line consists of n integers, representing the bi

Row four includes an integer m

The next m line, for the next line I: The first integer ki, indicates that the c1i,c2i crop combination has the Ki seed crop, the next two integers, followed by Ki An integer, representing the crop number in the combination. Output format

Output

Only one row, including an integer, represents the maximum benefit

Sample Input

3
421
232
1
23212

Sample Output
11

Sample explanation

A cultivated land species 1,2,b cultivated land species 3, income 4+2+3+2=11.

Data scope and conventions

For 100% data,1<=k< n<= 1000,0<><>
HINT

Source

Kpmcup#0 by Greens

Bzoj 3438: Small M's crop (min cut)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.