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

智能推荐

数据结构|数组为什么这么快?-程序员宅基地

文章浏览阅读2.9k次,点赞8次,收藏10次。我相信在很多地方,大家在进行数据结构的比较的时候,说到数组,第一反应就是—快,但是为什么快呢?数组到底快在哪里呢?不知道大家是否有思考过这个问题,这篇文章,我就讲讲我对数组的一些看法,抛砖引玉,希望大家多多交流!文章目录数组到底哪里快?查找快吗?为什么数组能支持随机访问呢?答案:举例分析:我们要分析的第一个问题是:数组到底哪里快?查找快吗?可能有的同学第一反应利用数组进行查找的话,时间...

基于Matlab图像融合总结_图像融合代码matlab-程序员宅基地

文章浏览阅读273次。图像融合是一种将多幅图像信息合并成一幅新图像的技术,可以获得比单独图像更多的信息。在Matlab中,有多种方法可以实现图像融合,包括像素级融合、变换域融合和深度学习融合等。本文将总结这些方法,并提供相应的源代码。在实际应用中,根据图像融合的具体需求,选择合适的方法进行处理。这些方法可以互相结合,实现更细致、更精确的图像融合效果。希望本文提供的源代码和示例能够对您在Matlab中进行图像融合的工作有所帮助。基于Matlab图像融合总结。_图像融合代码matlab

算法题:平均数为k的最长连续子数组-程序员宅基地

文章浏览阅读1k次。平均数为k的最长连续子数组_平均数为k的最长连续子数组

HMC使用手册_台达hmc07-n511h52使用手册-程序员宅基地

文章浏览阅读1k次。今天偶然间想起了之前发生的一次运行事件,在这次故障中,物理服务器宕机,远程终端无法救急。于是就需要用到HMC(硬件管理控制台)进行底层的操作,之前对于这个工具的使用确实不多,所以今天就特意在网上找了一篇关于HMC使用的手册,这里给大家分享一下,如果大家想要看网页原版,下面是网址:HMC使用手册..._台达hmc07-n511h52使用手册

设置定时任务的几种方式_制定定时任务的方法-程序员宅基地

文章浏览阅读471次。◦ 前言 ◦ 解决方案 普通线程sleep的方式实现定时任务 Timer实现定时任务 ScheduledExecutorService实现定时任务 Handler实现定时任务 AlarmManager实现精确定时操作前言项目中总是会因为各种需求添加各种定时任务,所以就打..._制定定时任务的方法

mysql支持的平台和操作系统_MySQL支持的操作系统列表_MySQL-程序员宅基地

文章浏览阅读461次。我们使用GNU Autoconf,因此将MySQL移植到所有使用Posix线程和C++编译器的现代系统是可能的。(要求服务器支持线程。如果只是编译客户端代码,则只需要C++编译器)。我们主要在Linux(SuSE和Red Hat)、FreeBSD和Sun Solaris(版本8和9)上使用并开发本软件。已经报告MySQL可以在下列操作系统/线程包的组合上成功地进行编译。注意,对于很多操作系统,原生..._mysql可以运行在哪些平台

随便推点

Java 中应用Dijkstra算法求解最短路径_路由最短路径代码java-程序员宅基地

文章浏览阅读476次。Dijkstra算法是一个经典的解决最短路径问题的算法,在路由算法、导航系统等领域都有广泛的应用。它通过逐步选择距离起始节点最近的节点,并更新其邻接节点的最短距离,最终得到起始节点到其他所有节点的最短路径。然后,在一个循环中,每次选择距离最小且未加入最短路径集合的节点,将其加入最短路径集合,并更新其邻接节点的最短路径长度。它遍历所有未加入最短路径集合(shortestPathTreeSet)的节点,查找距离最小且未加入最短路径集合的节点,并返回其索引。数组来追踪起始节点到其他节点的最短路径长度,_路由最短路径代码java

Mybatis-plus解决selectOne查询多个会报错的问题_mybatisplus selectone-程序员宅基地

文章浏览阅读2w次,点赞13次,收藏36次。123_mybatisplus selectone

【android】android12蓝牙框架_android 蓝牙框架-程序员宅基地

文章浏览阅读1.6k次,点赞15次,收藏22次。参考:hl=zh-cnhl=zh-cnhl=zh-cn。_android 蓝牙框架

玩转X-CTR100 l STM32F4 l PS2无线手柄-4WD智能小车-程序员宅基地

文章浏览阅读388次。我造轮子,你造车,创客一起造起来!更多塔克创新资讯【塔克社区 www.xtark.cn 】【塔克博客 www.cnblogs.com/xtark/ 】 前面已介绍X-CTR100控制器解码PS2无线手柄,本文继续前文,使用PS2无线手柄,实现4WD智能小车的控制,实现两种控制模式,方向模式和坦克模式。 例程-PS2无线手柄-4WD智能小车(方向模式) 使用4个..._stm32-4wd智能小车摘要

(深度学习)基于残差卷积——resnet的水稻病害识别_resnet152-程序员宅基地

文章浏览阅读1.6k次,点赞20次,收藏34次。利用残差卷积神经网络Resnet152实现水稻病害识别,附带数据集和预训练模型下载链接,代码框架可套用于其它分类任务。_resnet152

Esp8266 进阶之路34 【外设篇③】乐鑫esp8266 NONOS SDK 3.0编程使用 SPI 驱动基于Max7219芯片的八位数码管,显示日期信息。(附带Demo)_8266时钟代码 数码管-程序员宅基地

文章浏览阅读7k次,点赞5次,收藏28次。本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途。如有不对之处,请留言,本人及时更改。 1、 Esp8266之 搭建开发环境,开始一个“hellow world”串口打印。 2、 Esp8266之 利用GPIO开始使用按钮点亮你的“第一盏灯”。 3、 Esp8266之 利用 "软件定时器 " 定时0.5秒闪烁点亮一盏LED。4 、Es..._8266时钟代码 数码管