Clever King

Description:

In order to increase the happiness index of people’s lives, King Y has decided to develop the manufacturing industry vigorously. There are total n kinds of products that King can choose to produce, different products can improve the happiness index of poeple’s lives in different degrees, of course, the production of goods needs raw materials, different products need different ore or other products as raw materials. There are total m mines, and each mine can exploit different ore, Therefore, there are m types of ores, the cost of each mining for each mine is different, king Y want to maximize the income, the calculation method of income is:∑increased happiness index - ∑mining costs.
If you choose to exploit a mine, there will be an unlimited number of this kind of ore. What’s more, if you produce one product, the happiness index will definitely increase, no matter how many you produce.

Input:

The first line of the input has an integer T(1<=T<=50), which represents the number of test cases.
In each test case, the first line of the input contains two integers n(1<=n<=200)–the number of the products and m(1<=m<=200)–the number of mines. The second line contains n integers, val[i] indicates the happiness index that number i product can increase. The third line contains m integers, cost[i] indicates the mining cost of number i mine. The next n lines, each line describes the type of raw material needed for the number i product, in each line, the first two integers n1(1<=n1<=m)–the number of ores that this product needs, n2(1<=n2<=n)–the number of products that this product needs, the next n1 + n2 integers indicate the id of ore and product that this product needs. it guarantees that ∑n1+∑n2<=2000.

Output:

Each test case output an integer that indicates the maximum value ∑val[i]-∑cost[i].

Ignore extra spaces at the end of each line of output

sample input


2
3 3
600 200 400
100 200 300
1 2 1 2 3
1 0 2
1 0 3
3 4
600 400 200
100 200 300 1000
2 1 1 2 3
1 0 1
1 0 1

Sample output

600
900

answer

Template questions, just run the maximum weight closed subgraph

With AC code

#include<bits/stdc++.h>

using namespace std;

const int maxn=505;
struct node
{
    int to,next,flow;
}e[maxn*maxn*2];
int tot=1,ans,n,m,s,t,p[maxn],cur[maxn],co[maxn],d[maxn],final[maxn];
void link(int x,int y,int z)
{
    e[++tot].to=y,e[tot].next=final[x],e[tot].flow=z,final[x]=tot;
    e[++tot].to=x,e[tot].next=final[y],e[tot].flow=0,final[y]=tot;
}
int dg(int x,int flow){
    if(x==t) return flow;
    int use=0;
    for(int i=cur[x];i;i=e[i].next){
        cur[x]=i;
        if(e[i].flow>0 && d[e[i].to]+1==d[x]){
            int c=dg(e[i].to,min(flow-use,e[i].flow));
            use+=c,e[i].flow-=c,e[i^1].flow+=c;
            if(flow==use) return use;
        }
    }
    cur[x]=final[x];
    if(!(--co[d[x]])) d[0]=t;
    ++co[++d[x]];
    return use;
}
int main(){
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        long long pp=0;
        int x,n1,n2;
        tot=1;ans=0;n=0;m=0;
        memset(p,0,sizeof(p));
        memset(cur,0,sizeof(cur));
        memset(co,0,sizeof(co));
        memset(d,0,sizeof(d));
        memset(final,0,sizeof(final));
        scanf("%d %d",&n,&m);
        int ans=n+m;
        s = ans+1;
        t = ans+2;
        for(int i=1;i<=ans;i++)
        {
            scanf("%d",&x);
            if(i<=n)
            {
                pp+=x;
                link(s,i,x);
            }
            else
            {
                link(i,t,x);
            }
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&n1,&n2);
            for(int j=1;j<=n1;j++)
            {
                scanf("%d",&x);
                link(i,n+x,1<<30);
            }
            for(int j=1;j<=n2;j++)
            {
                scanf("%d",&x);
                link(i,x,1<<30);
            }
        }
        co[0] = t;
        for(;d[0]<t;)
            pp-=dg(s,1<<30);
        printf("%lld\n",pp);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324666767&siteId=291194637