2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 F.Clever King(最大权闭合子图)_2018 acm-icpc 中国大学生程序设计竞赛线上赛 clever king-程序员宅基地

技术标签: acm  网络流  

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

思路:
最大权闭合子图,闭合图的概念为:一些点的集合,集合中的点的出边都在这个集合内。如果这些点有权值,则可以找最大权闭合子图
这种模型明显适用于点之间有依赖关系(先后顺序?)的图(DAG等等)
明显是权中有正有负所以我们源点连正权的点,容量为正权,汇点连负权点,容量为父权的绝对值,然后有依赖关系的连边,容量为INF
然后答案是正权之和-最小割(最大流)
证明请看https://wenku.baidu.com/view/986baf00b52acfc789ebc9a9.html
accode

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int MX = 500+4;
const int MS = 500*500+32;
string s[31];
int n,m;
int cost1[MX];
int cost2[MX];
template<class T>
struct Max_Flow
{
    int n;
    int Q[MX],sign;
    int head[MX],level[MX],cur[MX],pre[MX];
    int nxt[MS],pnt[MS],E;
    T cap[MS];
    void init(int n)
    {
        E = 0;
        this->n = n+1;
        fill(head,head+this->n,-1);
    }
    void add(int from, int to,T c,T rw = 0){
        pnt[E] = to;
        cap[E] = c;
        nxt[E] = head[from];
        head[from] = E++;
        pnt[E] = from;
        cap[E] = rw;
        nxt[E] =head[to];
        head[to] = E++;
    }
    bool BFS(int s,int t)
    {
        sign = t;
        std::fill(level,level+n,-1);
        int *front = Q;
        int *tail = Q;
        *tail++ = t;
        level[t] = 0;
        while(front < tail &&level[s] == -1){
            int u = *front++;
            for(int e = head[u];e!=-1;e = nxt[e]){
                if(cap[e^1] > 0&&level[pnt[e]]<0){
                    level[pnt[e]] = level[u]+1;
                    *tail++ = pnt[e];
                }
            }
        }
        return level[s] != -1;
    }
    void Push(int t,T &flow){
        T mi =INF;
        int p = pre[t];
        for(int p = pre[t];p!=-1;p = pre[pnt[p^1]]){
            mi = std::min(mi,cap[p]);
        }
        for(int p = pre[t];p!=-1;p = pre[pnt[p^1]]){
            cap[p] -= mi;
            if(!cap[p]){
                sign = pnt[p^1];
            }
            cap[p^1] += mi;
        }
        flow += mi;
    }
    void DFS(int u,int t, T &flow){
        if(u==t){
            Push(t,flow);
            return ;
        }
        for(int &e = cur[u];e != -1;e = nxt[e]){
             if(cap[e]>0&&level[u]-1==level[pnt[e]]){
                pre[pnt[e]] = e;
                DFS(pnt[e],t,flow);
                if(level[sign] >level[u]){
                    return;
                }
                sign = t;
             }
        }
    }
    T Dinic(int s,int t){
        pre[s] = -1;
        T flow = 0;
        while(BFS(s,t)){
            std::copy(head,head+n,cur);
            DFS(s,t,flow);
        }
        return flow;
    }
};
Max_Flow<LL>F;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
    scanf("%d%d",&n,&m);
        F.init(n+m+2);
        LL sum = 0;
        for(int i = 1;i<=n;i++){
            scanf("%d",&cost1[i]);
            sum += cost1[i];
            F.add(0,i,cost1[i]);
        }
        for(int i = 1;i<=m;i++){
            scanf("%d",&cost2[i]);
            F.add(i+n,n+m+2,cost2[i]);
        }
        for(int i = 1;i<=n;i++){
            int c1,c2;
            scanf("%d%d",&c1,&c2);
            for(int j = 0;j<c1;j++){
                int x;
                scanf("%d",&x);
                F.add(i,x+n,INF);
            }
            for(int j = 0;j<c2;j++){
                int x;
                scanf("%d",&x);
                F.add(i,x,INF);
            }
        }
        LL ans = F.Dinic(0,n+m+2);
     //   cout<<ans<<endl;
        printf("%ld\n",sum-ans);

    }
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/w571523631/article/details/80238770

智能推荐

[点击百度快照]基于MATLAB GUI的成绩查询系统_matlabgui中点击查询出结果-程序员宅基地

文章浏览阅读123次。该课题为基于MATLAB GUI的成绩查询系统,可导入,增删,查询,修改学生成绩,并且可以统计各个学科分数听课,最高分,最低分,中位数等信息。是个不错的课设题目。_matlabgui中点击查询出结果

C++实现快速排序(源代码)_c++快速排序源码-程序员宅基地

文章浏览阅读637次。原文链接:https://blog.csdn.net/liuchen1206/article/details/6954074_c++快速排序源码

【手册】CDH6.3.2及hadoop生态圈工具安装部署手册(附带安装包)_hadoop cdh 6-程序员宅基地

文章浏览阅读2k次。大数据测试环境CDH6.3.2安装部署手册一、前期准备1、服务器3台,系统要求centos7,服务器配置24核心+64G内存+2.7T磁盘2、CDH6.3.2相关资源,目前在线下载已收费,只能采用离线安装3、CM6.3.1相关资源,目前在线下载已收费,只能采用离线安装4、mysql驱动,jdk安装包5、集群规划6、Flink1.12目前官网没有提供,官网只提供了flink1.9版本的集成,如需使用需要自己编译。内存磁盘CPUcmcdhMysqlHiveImpalaK_hadoop cdh 6

你的第一篇SCI是怎么发的呢?-程序员宅基地

文章浏览阅读1.9k次,点赞5次,收藏22次。来源:https://www.zhihu.com/question/337008253编辑:深度学习与计算机视觉声明:仅做学术分享,侵删又快到毕业季,有..._机器视觉怎么发sci

Webmin 远程代码执行 (CVE-2022-0824)漏洞搭建复现+攻击检测原理(流量分析)详细-程序员宅基地

文章浏览阅读2.9k次。在 Webmin v1.984 中,影响文件管理器模块,任何没有文件管理器模块访问权限的经过身份验证的低权限用户都可以与文件管理器功能交互,例如从远程 URL 下载文件和更改文件权限 (chmod)。通过在文件管理器中链接这些功能,可以通过精心制作的 .cgi 文件实现远程代码执行。_cve-2022-0824

远程项目和本地项目合并的解决方案-idea或本地git测试都行_同类项目合并方案-程序员宅基地

文章浏览阅读1.5k次。原文地址https://blog.csdn.net/kiddd_fu/article/details/78247290终极解决方案出现(non-fast-forward)的根本原因是repository已经存在项目且不是你本人提交(我知道是大概率你提交的,但是git只认地址),你commit的项目和远程repo不一样。这时该怎么办呢?很简单,把远端项目拉回本地:git pull然而pu..._同类项目合并方案

随便推点

java scriptengine,使用Java ScriptEngine(Groovy),如何使它更具性能?-程序员宅基地

文章浏览阅读1.2k次。I am using ScriptEngine in my app to evaluate some client code in my application.The problem is it's not performant enough and I need to take measures to improve the time of execution.Currently, it ca..._scriptenginemanager 性能优化

docker项目运行环境搭建(JDK8、mysql8、tomcat9、redis5、nginx1.14)_docker jdk8-程序员宅基地

文章浏览阅读436次。1、在/home/mysql目录下新建两个文件夹,一个叫data另一个叫conf。_docker jdk8

临床试验中edc录入_一文了解EDC临床试验数据采集系统-程序员宅基地

文章浏览阅读7.1k次。不知道大家是不是和我一样,初入行业的时候并不懂啥是EDC。后来我觉得,当时不懂也正常,毕竟EDC这种东西在国内出现也没多久。说起EDC要先从CRF(Case Report Form),即临床试验病例报告表说起。啥是CRF呢?在药物的临床试验项目中,不管你protocol写得多牛比,总得需要一项一项地去收集受试者的试验信息,比如受试者今天吃了多少药?有没有不良反应? 血液中各项指标是多少?因此我们需..._edc录入

Windows环境中同时安装Oracle9i 和10g_大胖黑马(授权发布)-程序员宅基地

文章浏览阅读172次。Windows环境中同时安装Oracle9i 和10g                               原创者:大胖黑马(授权发布)简单说一下在windows的同一用户下,安装Oracle的9i、10g 的方法1、安装版本需要从低到高。也就是说先安装9i的数据库,然后安装10g的数据库2、安装目录分开。3、在低版本的数据库安装完成后,最好通过..._oracle 9i 和oracle 10g同时使用

OpenCV-Python官方教程-30- 支持向量机(support vector machines, SVM)_python opencv 计算向量-程序员宅基地

文章浏览阅读305次。使用SVM进行手写数据OCR在 kNN 中我们直接使用像素的灰度值作为特征向量。这次我们要使用方向梯度直方图Histogram of Oriented Gradients (HOG)作为特征向量。在计算 HOG 前我们使用图片的二阶矩对其进行抗扭斜(deskew)处理,也就是把歪了的图片摆正。所以我们首先要定义一个函数 deskew(),它可以对一个图像进行抗扭斜处理。下面就是 deskew() 函数:..._python opencv 计算向量

CUDA入门3.1——使用CUDA实现鱼眼转全景图(OpenCV环节)_鱼眼图像展开成全景图-程序员宅基地

文章浏览阅读2.7k次。思路1,通过某种方法获取图片数据,并且了解数据结构。 2,通过某种数学公式将鱼眼画面处理成全景图。 3,通过CUDA并行运算实现鱼眼转全景图功能。 本篇主要讲述OpenCV获取图片以及指针的使用,与CUDA无关。获取图片数据OpenCV环境配置1 下载OpenCVOpenCV 下载驿站(百度云盘下载,同步更新)2 配置OpenCV开发环境配置的方法网上很多,可以查找。我用的是 OpenCV环境_鱼眼图像展开成全景图

推荐文章

热门文章

相关标签