计蒜客某赛F(最大权封闭子图)

题目链接:https://nanti.jisuanke.com/t/26172

裸最大权封闭子图,然而忘了qaq复习复习。。

这题题意可以作为最大权封闭子图的理解,就是有若干产品,每个产品有对应价值 ,有若干原料,每个原料有对应费用,产品需要先获得相应原料和产品才可获得,即有一定依赖关系,然后就是要最大利润了。。

解决方案:源点指向所有产品,容量为价值,所有原料指向汇点,容量为费用,对每个产品,相其依赖连边,容量为无穷,答案为价值之和-最大流

其实也很容易理解,就是用已有的价值去和费用抵消,而由于源点的流量限制,最大流顶多就把价值给全部抵消,即不选该产品,若流量有余,则为盈利部分。。答案要算的其实是剩余流量。。





/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 200005
#define nm 200005
#define pi 3.1415926535897931
using namespace std;
const int inf=1000000005;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}


struct edge{int t,v;edge*next,*rev;}e[nm],*h[NM],*o=e,*p[NM],*tmp[NM];
void _add(int x,int y,int v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
void add(int x,int y,int v){_add(x,y,v);_add(y,x,0);h[x]->rev=h[y];h[y]->rev=h[x];}
int n,m,_x,_y;
ll s;


int d[NM],cnt[NM];
ll maxflow(){
	int tot=n+1;ll flow=0;edge*j;
	mem(p);mem(cnt);mem(d);mem(tmp);
	inc(i,0,n)tmp[i]=h[i];
	cnt[0]=tot;
	for(int x=0,s=inf;d[x]<tot;){
		for(j=tmp[x];j;j=j->next)if(j->v&&d[x]==d[j->t]+1)break;
		if(j){
			s=min(s,j->v);p[j->t]=tmp[x]=j;
			if((x=j->t)==n){
				for(;p[x];x=p[x]->rev->t)p[x]->v-=s,p[x]->rev->v+=s;
				flow+=s;s=inf;
			}
		}else{
			if(!--cnt[d[x]])break;d[x]=tot;
			link(x)if(j->v&&d[x]>d[j->t]+1)d[x]=d[j->t]+1,tmp[x]=j;
			cnt[d[x]]++;
			if(p[x])x=p[x]->rev->t;
		}
	}
	return flow;
}


int main(){
	int _=read();
	while(_--){
		n=read();m=read();mem(e);mem(h);o=e;s=0;
		inc(i,1,n){_x=read();s+=_x;add(0,i,_x);}
		inc(i,1,m)add(i+n,n+m+1,read());
		inc(i,1,n){
			_x=read();_y=read();
			while(_x--)add(i,read()+n,inf);
			while(_y--)add(i,read(),inf);
		}
		n=n+m+1;
		printf("%lld\n",s-maxflow());
	}
	return 0;
}





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].

忽略每行输出的末尾多余空格

样例输入
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
样例输出
600
900
题目来源

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值