Bzoj 3438 small m of crop maximal right closed sub-graph __ac Road

Source: Internet
Author: User

There are two ways to build a graph, where the maximum weight closed sub graph is used.

First of all, based on the idea of the maximum right closed sub graph, but the topic has two farming, and the network flow of S and t have to represent the right side and negative side right, so consider removing a field, we assume that the seeds will be planted in a field and then continue to move the seeds to B field.

The closed chart is basically this way:


Red is the seed, the right to point can be negative, green is the combination of B field, the right to point is positive, Brown is a field of combination, the right to point for the negative

1. For each seed, the price is b[i]-a[i] can be positive and negative, according to the situation with s or T, Edge right for |b[i]-a[i]|

The combination of the 2.A with T, the weight is c[a], all the seeds that make up a way to him, side right for INF

3.s and B combination, weights are c[b], with all the composition of the seeds on both sides, the edge right for INF

#include <cstdio> #include <cstring> #include <iostream> #define MAXN 2000021 #define INF 0X3FFFFFF
F using namespace Std;  
int HEAD[MAXN],N,M,TOT,H[MAXN],LAST[MAXN],ANS,S,T,Q[MAXN]; struct Edge{int v,next,w;}  
E[MAXN];  
    void Adde (int a,int b,int c) {e[tot].v=b,e[tot].next=head[a],e[tot].w=c;head[a]=tot++;  
e[tot].v=a,e[tot].next=head[b],e[tot].w=0;head[b]=tot++;  
    BOOL BFs () {for (int i=s;i<=t;i++) h[i]=-1;  
    int l=0,r=1;q[l]=s;h[s]=0;  
        while (l<r) {int u=q[l++];  
                for (int v,i=head[u];i!=-1;i=e[i].next) {if (H[V=E[I].V]==-1&AMP;&AMP;E[I].W) {h[v]=h[u]+1;  
            Q[r++]=v;  
}} return h[t]!=-1; int dfs (int u,int f) {if u==t| |!  
    f) return F;  
    int used=0,w;  
        for (int v,i=last[u];i!=-1;i=e[i].next) {last[u]=i; if (h[v=e[i].v]!=h[u]+1| |!  
        E[I].W) continue;  
        W=min (e[i].w,f-used); W=dfs (V,W);  
        E[i].w-=w,e[i^1].w+=w,used+=w;  
    if (used==f) return used;  
    } if (!used) h[u]=-1;  
return used;  
        } void Dinic () {while (BFS ()) {for (int i=s;i<=t;i++) last[i]=head[i];  
    Ans-=dfs (S,inf);
} int A[MAXN],B[MAXN];
	void Build () {scanf ("%d", &n);
	for (int i=1;i<=n;i++) scanf ("%d", a+i), ans+=a[i];
	for (int i=1;i<=n;i++) scanf ("%d", &b[i]), b[i]-=a[i];
	scanf ("%d", &m); t=n+2*m+1;
		for (int i=1;i<=n;i++) {if (b[i]>0) {Ans+=b[i],adde (s,i,b[i]);
	}else Adde (I,t,-b[i]);
		for (int id,k,c1,c2,i=1;i<=m;i++) {scanf ("%d%d%d", &AMP;K,&AMP;C1,&AMP;C2);
			for (int x,j=1;j<=k;j++) {scanf ("%d", &x);
		Adde (N+i,x,inf); Adde (X,n+m+i,inf);
		} ans+=c1,ans+=c2;
	Adde (S,N+I,C2), Adde (N+M+I,T,C1); 
    int main () {memset (head,-1,sizeof head);
	Build ();
    Dinic ();
    printf ("%d", ans);  
return 0;   }


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.