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

智能推荐

Java 进阶二知识--重拾者AIMING-程序员宅基地

文章浏览阅读441次。多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。4、编写构造方法,参数列表和用到的参数类型一致,并且在其中通过参数列表读取 @Parameters方法中的数据,为成员变量赋值。:线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈、指针、程序计数器等),但代码区是共享的,即不同的线程可以执行同样的函数。用户空间、内核空间、栈空间、堆空间、代码段、BSS段、DATA段。

手把手完成智慧路灯的开发,完成设备上云【华为云IoT】_当光照值小于预设值时,调用云平台相应api,实现灯的开启;当光照值大于等于预设-程序员宅基地

文章浏览阅读4.8k次,点赞5次,收藏32次。通过智慧路灯的项目,介绍了一个物联网设备如何上云,云平台如何配置,数据可视化大屏如何对接显示的整个流程。 如果手上没有智慧路灯的硬件,也可以通过文中介绍的MQTT客户端软件,模拟设备数据进行数据上传,一样可以完成云端的所有操作。 有了可视化大屏,就可以实时查看设备的状态,本次的例子里设计的界面比较简单,如果想设计酷炫,可以查看官方的模板,在新建大屏的时候可以选择模板进行创建。_当光照值小于预设值时,调用云平台相应api,实现灯的开启;当光照值大于等于预设

Model Predictive Control-程序员宅基地

文章浏览阅读2.7k次。大量的预测控制权威性文献都无一例外地指出, 预测控制最大的吸引力在于它具有显式处理约束的能力, 这种能力来自其基于模型对系统未来动态行为的预测, 通过把约束加到未来的输入、输出或状态变量上, 可以把约束显式表示在一个在线求解的二次规划或非线性规划问题中.预测算法的三要素:内部(预测)模型、滚动优化、反馈控制。1.基于模型的预测在MPC算法中,需要一个描述对象动态行为的模型,这个模型的作用是预..._model predictive con

混合开发 Hybird Ionic Angular Cordova web 跨平台 MD-程序员宅基地

文章浏览阅读201次。Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 [email protected]混合开发 Hybird Ionic Angular Cordova web 跨平台 MD..._> ionic integrations enable cordova [info] downloading integration cordova [

javaWeb基础之Servlet的三种实现方式以及两种配置方式_servlet需要重写什么方法-程序员宅基地

文章浏览阅读4.5k次。一、Servlet的三种实现方式Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容。1、Servlet的第一种创建方式:继承HttpServlet(最优) 重写doGet(HttpServletRequest request, HttpS..._servlet需要重写什么方法

strlen函数以及string类使用心得_string strlen-程序员宅基地

文章浏览阅读3.1k次。复习一下strlen函数其实,给strlen函数之后它就会向下偏移统计个数,遇到当前位置字符为'\0'才会停下来。如果没有的话就有可能接着往下走下去,甚至会超过开辟空间的区域指向一片未开辟空间赋值的空间。所以服务端这边接收的buffer要比需要接收的数据大小大一点才不会在strlen的时候出现问题,因为数组里面的数据都占满了,最后一个'\0'的位置没留下来,粗心大意。函数原型..._string strlen

随便推点

linux上C++开发——1. C++包管理工具-程序员宅基地

文章浏览阅读1.4k次。C++包管理工具_c++包管理工具

LVS负载均衡服务器搭建_lvs搭建-程序员宅基地

文章浏览阅读2k次。现在LVS已经是Linux标准内核的一部分,在Linux2.4内核以前,使用LVS时必须重新编译内核以支持LVS功能模块,但是从Linux2.4内核心之后,已经完全内置了LVS的各个功能模块,无需给内核打任何补丁,可以直接使用LVS提供的各种功能。使用LVS技术要达到的目标是:通过LVS提供的负载均衡技术和Linux操作系统实现一个高性能,高可用的服务器群集,它具有良好的可靠性、可扩展性和可操作性。从而以低廉的成本实现最优的服务性能。......_lvs搭建

数据库谓词-程序员宅基地

文章浏览阅读2.1k次,点赞3次,收藏5次。谓词:属于函数的一种,但其返回值是真值(true/false/unknown)判断是否存在满足某种条件的记录,存在返回TRUE、不存在返回FALSE。比较多用到的几种谓词:LIKEBETWEENIS NULL/IS NOT NULLINEXISTSLIKE谓词——字符串的部分一直查询(模糊查询)--MySQL--DDL:创建表CREATE TABLE SampleLike..._数据库 连接谓词是什么

论文学习笔记-MobileNet v3_mobilenetv3扩张尺寸-程序员宅基地

文章浏览阅读9.3k次,点赞5次,收藏36次。『写在前面』新一代MobileNet,性能全面提升。作者机构:Andrew Howard等,Google。文章标题:《Searching for MobileNetV3》原文链接:https://arxiv.org/abs/1905.02244v2相关repo:摘要结合网络设计和NAS技术提出新一代MobileNets; 发布了两种网络结构:MobileNetV3..._mobilenetv3扩张尺寸

2022(软考高级)信息系统项目管理师认证招生简章_山东省信息系统项目管理专业院校-程序员宅基地

文章浏览阅读331次。信息系统项目管理师是全国计算机技术与软件专业技术资格(水平)考试(简称软考)项目之一,是由国家人力资源和社会保障部、工业和信息化部共同组织的国家级考试,既属于国家职业资格考试,又是职称资格考试。信息系统项目管理师,属于软考三个级别中的“高级”。从1999年开始实施系统集成项目管理工程师/信息系统项目管理师,到目前为止,累计报名人数超过了300万人次,累计合格人数接近50万人。【报考要求】不设学历与资历条件、年龄以及专业等限制,考生可根据自己的技术水平选择合适的级别合适的资格进行报考。凡遵守中华人_山东省信息系统项目管理专业院校

python2.7实战教程_实战 - 廖雪峰 Python 2.7 中文教程-程序员宅基地

文章浏览阅读82次。看完了教程,是不是有这么一种感觉:看的时候觉得很简单,照着教程敲代码也没啥大问题。于是准备开始独立写代码,就发现不知道从哪开始下手了。这种情况是完全正常的。好比学写作文,学的时候觉得简单,写的时候就无从下笔了。虽然这个教程是面向小白的零基础Python教程,但是我们的目标不是学到60分,而是学到90分。所以,用Python写一个真正的Web App吧!目标我们设定的实战目标是一个Blog网站,包含..._python快速编程入门第二版2.7.1实训案例

推荐文章

热门文章

相关标签