2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227

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

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

ACM-ICPC Asia Training League   宁夏理工学院

题解:

  最大权闭合子图,跑最小割裸题。

  源点向产品连边,权值为产品的幸福值;矿石向汇点连边,权值为矿石需要的花费;

  然后产品向需要的矿石连边,权值为inf,产品向需要的子产品连边,权值同理也为inf,然后跑最小割。

  最后答案为  所有产品幸福值的和  减去  最小割。(割掉源点向产品的边表示不生产此产品,割掉矿石向汇点的边表示使用此矿石)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = ;
int n, m, S, T;
int dep[N], cur[N];
int head[N];
struct Edge{
int v, c, nex;
Edge(int _v=,int _c=,int _nex=):v(_v),c(_c),nex(_nex){}
};
vector<Edge>E;
void add(int u,int v,int c){E.push_back(Edge(v,c,head[u]));head[u]=E.size()-;}
bool bfs() {
queue<int> q;
memset(dep, -, sizeof(dep));
q.push(S); dep[S] = ;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = E[i].nex) {
int v = E[i].v;
if(E[i].c && dep[v] == -) {
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return dep[T] != -;
}
int dfs(int u, int flow) {
if(u == T) return flow;
int w, used=;
for(int i = head[u]; ~i; i = E[i].nex) {
int v = E[i].v;
if(dep[v] == dep[u] + ) {
w = flow - used;
w = dfs(v, min(w, E[i].c));
E[i].c -= w; E[i^].c += w;
if(v) cur[u] = i;
used += w;
if(used == flow) return flow;
}
}
if(!used) dep[u] = -;
return used;
}
ll dinic() {
ll ans = ;
while(bfs()) {
for(int i = ; i <= T;i++)
cur[i] = head[i];
ans += dfs(S, inf);
}
return ans;
}
int main() {
int t, i, j, k, x, n1, n2;
ll s = ;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
E.clear();
s = ;
S = n+m+; T = n+m+;
for(i = ; i <= n; ++i) {//产品
scanf("%d", &x);
add(S, i, x); add(i, S, );
s += x;
}
for(i = ; i <= m; ++i) {//矿石
scanf("%d", &x);
add(i+n, T, x); add(T, i+n, );
}
for(i = ; i <= n; ++i) {
scanf("%d %d", &n1, &n2);
while(n1--) {//矿石
scanf("%d", &x);
add(i, x+n, inf); add(x+n, i, );
}
while(n2--) {//产品
scanf("%d", &x);
add(i, x, inf); add(x, i, );
}
}
ll ans = dinic();
s = s - ans;
printf("%lld\n", s);
}
return ;
}

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 F题 Clever King(最小割)的更多相关文章

  1. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)

    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...

  2. 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)

    若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...

  3. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 I. Reversion Count (java大数)

    Description: There is a positive integer X, X's reversion count is Y. For example, X=123, Y=321; X=1 ...

  4. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 D Merchandise (斜率优化)

    Description: The elderly aunts always like to look for bargains and preferential merchandise. Now th ...

  5. 2017年中国大学生程序设计竞赛-中南地区赛暨第八届湘潭市大学生计算机程序设计大赛题解&源码(A.高斯消元,D,模拟,E,前缀和,F,LCS,H,Prim算法,I,胡搞,J,树状数组)

    A------------------------------------------------------------------------------------ 题目链接:http://20 ...

  6. 第 46 届 ICPC 国际大学生程序设计竞赛亚洲区域赛(沈阳)

    有时候,很简单的模板题,可能有人没有做出来,(特指 I ),到时候一定要把所有的题目全部看一遍 目录 B 题解 E F 题解 H I 题解&代码 J B 输入样例 3 2 1 2 1 2 3 ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  8. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. jquery 关于使用 append 追加 元素后 事件无法触发

    当在使用js或jQuery创建元素时,用 on(事件,function(){代码}) 或者 事件(function(){代码 })绑定事件时 在使用append添加元素后 由于是在页面加载完成之后进行 ...

  2. JavaScript unshift()函数移入数据到数组第一位

    你不仅可以 shift(移出)数组中的第一个元素,你也可以 unshift(移入)一个元素到数组的头部. .unshift() 函数用起来就像 .push() 函数一样, 但不是在数组的末尾添加元素, ...

  3. C# 小软件部分(一)

    自己在空闲时分整合.编写了一款小软件程序,命名为魔法兔子,希望大家可以提出意见和指导,此篇文章主要为软件的部分截图和介绍. 软件详情: 1.首先是登录,注册界面. 可以注册自己的账号,后台是腾讯云服务 ...

  4. [HTML5] Canvas绘制简单形状

    使用canvas来进行绘画,它像很多其他dom对象一样,有很多属性和方法,操作这些方法,实现绘画 获取canvas对象,调用document.getElementById()方法 调用canvas对象 ...

  5. 【转】JUC下面线程池介绍

    介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...

  6. 浅谈TCP/IP(new 常见面试问题)

    1. TCP/IP重传机制,如何保证消息读到一个完整内容再反序列化 2. TCP四次回收比三次握手多了什么操作,什么时候会进入Time_await状态 3.

  7. vue 面试时需要准备的知识点

    前端火热的框架层出不穷,作为码农的我们,依旧需要去学习,去探索新的问题,学习新技术,其实就是为了写一手好的,自认为是高质量的代码.今天主要分享一下前端最火的框架vue,也是我比较喜欢的框架. vue上 ...

  8. Python入门与基本概念

    简介:本学习笔记基于ubuntu,Ubuntu已经内置了python2.7,python2.7既包含老版本的属性,有含有新版本的一些特性,用于对3.x版本的过渡,可以拿来入门学习,入门之后可以再学习p ...

  9. Myeclipse使用过程配置汇总

    1.下载安装及破解方法 myeclipse2014专业版下载地址链接:https://pan.baidu.com/s/1i62YOGt 密码:nlqj    : 下载后安装到最后一步先不要打开软件,如 ...

  10. leetCode题解寻找最短字符路径

    1.题目描述 2.分析 最简单的方案,对每一个字符,向两边寻找. 3.代码 vector<int> shortestToChar(string S, char C) { vector< ...