最裸的最大流,没啥好说的。。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int n, m, tot, S, T, sum, head[N << ], level[N << ], a[N << ], b[N << ]; struct node {
int u, v, w, nx;
} edge[M << ]; void add(int u, int v, int w) {
edge[tot].u = u; edge[tot].v = v; edge[tot].w = w;
edge[tot].nx = head[u]; head[u] = tot++;
} bool bfs() {
memset(level, , sizeof(level));
queue<int> que;
level[S] = ; que.push(S); while(!que.empty()) {
int u = que.front(); que.pop();
if(u == T) return true; for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].v;
if(level[v] || edge[i].w <= ) continue;
level[v] = level[u] + ;
que.push(v);
}
}
return false;
} int dfs(int u, int p) {
if(u == T) return p;
int ret = ;
for(int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].v, w = edge[i].w;
if(level[v] != level[u] + || w <= ) continue;
int f = dfs(v, min(p - ret, w));
ret += f;
edge[i].w -= f;
edge[i ^ ].w += f;
if(ret == p) break;
}
if(!ret) level[u] = ;
return ret;
} int Dinic() { int ans = ;
while(bfs()) ans += dfs(S, inf);
return ans;
} void init() {
tot = ;
sum = ;
memset(head, -, sizeof(head));
} int main(){
int cas; scanf("%d", &cas);
while(cas--) {
init();
scanf("%d%d", &n, &m); S = , T = n + m + ;
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
add(i, T, a[i]);
add(T, i, );
sum += a[i];
} for(int i = ; i <= m; i++) {
scanf("%d", &b[i]);
add(S, i + n, b[i]);
add(i + n, S, );
} for(int i = ; i <= m; i++) {
int k; scanf("%d", &k);
for(int j = ; j <= k; j++) {
int id; scanf("%d", &id);
add(i + n, id, inf);
add(id, i + n, );
}
} int ans = Dinic();
printf("%d\n", sum - ans);
}
return ;
} /*
*/

牛客练习赛19 D-托米去购物的更多相关文章

  1. 牛客练习赛 23 C 托米的位运算

    链接:https://www.nowcoder.com/acm/contest/156/C来源:牛客网 托米完成了1317的上一个任务,十分高兴,可是考验还没有结束 说话间1317给了托米 n 个自然 ...

  2. 牛客练习赛19 E和F(签到就走系列)托米的饮料+托米搭积木

    E题传送门:点我 F题传送门:点我 可爱的小托米得到了n瓶饮料. 但他不小心把开盖的工具弄丢了,所以他只能利用饮料瓶来开盖. 已知第i个瓶子的品牌为ai,且其能打开bi品牌的瓶子. 问有几瓶饮料托米无 ...

  3. 牛客练习赛19 -E-托米的饮料

    题目描述 好了,现在是小托米的故事啦~~~ 可爱的小托米得到了n瓶饮料. 但他不小心把开盖的工具弄丢了,所以他只能利用饮料瓶来开盖. 已知第i个瓶子的品牌为ai,且其能打开bi品牌的瓶子. 问有几瓶饮 ...

  4. 牛客练习赛23 F 托米的游戏

    https://www.nowcoder.com/acm/contest/156/F 树 概率 #include <cstdio> #include <cstdlib> #in ...

  5. 牛客练习赛19 C-托米航空公司

    思路:轮廓线dp,找bug找死我了. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  6. 牛客练习赛22 C 简单瞎搞题

    //位运算 // & 都是1 才是 1 // | 都是0 才是0 // ^ 不一样才是1 #include <iostream> #include <cstdio> # ...

  7. [堆+贪心]牛客练习赛40-B

    传送门:牛客练习赛40 题面: 小A手头有 n 份任务,他可以以任意顺序完成这些任务,只有完成当前的任务后,他才能做下一个任务 第 i 个任务需要花费  x_i 的时间,同时完成第 i 个任务的时间不 ...

  8. 牛客练习赛 29 E 位运算?位运算!(线段树)

    题目链接  牛客练习赛29E 对$20$位分别建立线段树.首先$1$和$2$可以合起来搞(左移右移其实是等效的) 用个lazy标记下.转移的时候加个中间变量. $3$和$4$其实就是区间$01$覆盖操 ...

  9. 牛客练习赛50 D tokitsukaze and Event (最短路,思维)

    牛客练习赛50 D tokitsukaze and Event 链接:https://ac.nowcoder.com/acm/contest/1080/D来源:牛客网 时间限制:C/C++ 1秒,其他 ...

随机推荐

  1. Httpclient与RestTemplate的比较(比httpClient更优雅的Restful URL访问)

    一.HttpClient (一)HttpClient 客户端 1.HttpClient 是 apache 的开源,需要引入两个包:httpclient-4.2.4.jar 和 httpcore-4.2 ...

  2. java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处.  http://www.cnblogs.com/king-xg/p/6370890.html 如果觉得对您有 ...

  3. kvm虚拟机

    ###查看虚拟机的状态 [root@fgeserver2 ~]# virsh list --all Id Name State------------------------------------- ...

  4. Global Vectors forWord Representation

    参考论文: GloVe: Global Vectors forWord Representation 参考博客:https://blog.csdn.net/coderTC/article/detail ...

  5. 排序构造 GYM 101149 F - The Weakest Sith

    题目链接:http://codeforces.com/gym/101149/my 题目大意:给你n个人,他们有成绩a,b,c.一个人如果两门课比另外一个人高,那么这个人就比那个人厉害.问,是否存在一个 ...

  6. css纯数字或字母换行

    #div { word-wrap:break-word; word-break:break-all; }

  7. MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields

    Different query operators in MongoDB treat null values differently. The examples on this page use th ...

  8. R1(下)—数据挖掘—关联规则理论介绍与R实现

    Apriori algorithm是关联规则里一项基本算法.是由Rakesh Agrawal和Ramakrishnan Srikant两位博士在1994年提出的关联规则挖掘算法.关联规则的目的就是在一 ...

  9. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  10. Date对象相关函数使用

    参考:http://www.w3school.com.cn/jsref/jsref_obj_date.asp