zoj 2071 Technology Trader 最大权闭合子图
和上一题一样, 也是一个最大权闭合子图。不过建图好麻烦的感觉 写了好久。
源点和原材料连边, 权值为val。 汇点和产品连边, 权值为val。 产品与和它有关系的材料连边, 权值inf。 最后跑一边网络流, 满流的话是没利润的, 输出结果是利润和-最大流。
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
int head[maxn*], s, t, num, q[maxn*], dis[maxn], used[maxn], cnt1, cnt2, ans1[maxn], ans2[maxn];
map <string, int> vis;
struct
{
int val;
string name;
}a[maxn];
struct node
{
int to, nextt, c;
}e[maxn*];
void init() {
mem1(head);
num = cnt1 = cnt2 = ;
mem(used);
vis.clear();
}
void add(int u, int v, int c) {
e[num].to = v; e[num].nextt = head[u]; e[num].c = c; head[u] = num++;
e[num].to = u; e[num].nextt = head[v]; e[num].c = ; head[v] = num++;
}
int bfs() {
int u, v, st = , ed = ;
mem(dis);
dis[s] = ;
q[ed++] = s;
while(st<ed) {
u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
v = e[i].to;
if(e[i].c&&!dis[v]) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t)
return limit;
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[u] == dis[v]-) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
void dfs(int u) {
used[u] = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&!used[v]) {
dfs(v);
}
}
}
int main()
{
int T, n, q, val, x;
cin>>T;
while(T--) {
cin>>n;
init();
s = ;
int i, sum = ;
for(i = ; i<=n; i++) {
cin>>a[i].name>>a[i].val;
vis[a[i].name] = i;
add(s, i, a[i].val);
}
cin>>q;
t = n+q+;
string str;
while(q--) {
cin>>a[i].name>>a[i].val>>x;
sum += a[i].val;
add(i, t, a[i].val);
while(x--) {
cin>>str;
add(vis[str], i, inf);
}
i++;
}
int ans = sum - dinic();
dfs();
for(int i = head[s]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!used[v]) {
ans1[cnt1++] = v;
}
}
for(int i = head[t]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!used[v]) {
ans2[cnt2++] = v;
}
}
cout<<ans<<endl;
cout<<cnt2<<endl;
for(int i = ; i<cnt2; i++) {
cout<<a[ans2[i]].name<<endl;
}
cout<<cnt1<<endl;
for(int i = ; i<cnt1; i++)
cout<<a[ans1[i]].name<<endl;
}
}
zoj 2071 Technology Trader 最大权闭合子图的更多相关文章
- [HOJ2634] How to earn more 最大权闭合子图
Xiao Ming is an expert in computer science and technology, so he can get a lot of projects every mon ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- HDU 3879 Base Station(最大权闭合子图)
经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...
- [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...
- HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...
- HDU5855 Less Time, More profit(最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...
- HDU5772 String problem(最大权闭合子图)
题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...
- SCU3109 Space flight(最大权闭合子图)
嗯,裸的最大权闭合子图. #include<cstdio> #include<cstring> #include<queue> #include<algori ...
- hiho 第119周 最大权闭合子图
描述 周末,小Hi和小Ho所在的班级决定举行一些班级建设活动. 根据周内的调查结果,小Hi和小Ho一共列出了N项不同的活动(编号1..N),第i项活动能够产生a[i]的活跃值. 班级一共有M名学生(编 ...
随机推荐
- iOS - SQLite Database 操作数据库
iOS - SQLite Database 操作数据库 Sqlite 能被用在ios上做数据处理用,只要你懂得一点sql 就很容易使用sqlite 1:创建一个简单的View based appl ...
- windows如何获取Win10 Win8 Win8.1版本
GetVersionEx 在win8 win8.1 win10 之后已经无法使用,如果非要使用的话需要让exe嵌入manifest,mainfest如下.这个文件需要已utf-8存储. <?xm ...
- QF——iOS中的数据库操作:SQLite数据库,第三方封装库FMDB,CoreData
SQLite数据库: SQLite是轻量级的数据库,适合应用在移动设备和小型设备上,它的优点是轻量,可移植性强.但它的缺点是它的API是用C写的,不是面向对象的.整体来说,操作起来比较麻烦.所以,一般 ...
- spring 源码之IOC 类图
Spring IoC容器是spring框架的核心和基础,IoC容器负责spring Bean的生命周期,是spring框架实现其他扩展功能的基础.容器的继承结构比较复杂,这里画出了spring IoC ...
- sql按in中集合排序
1.SELECT * from tbLabelResRelation WHERE lId in(32,18,27,19) order by FIND_IN_SET(lId ,'32,18,27,19' ...
- Android padding和margin的区别
如: Padding 为内边框,指该控件内部内容,如文本/图片距离该控件的边距 Margin 为外边框,指该控件距离边父控件的边距 如: 当按钮分别设置以上两个属性时,得到的效果是不一样的. andr ...
- 动态Script标签 解决跨域问题
动态Script 解决跨域问题 1.动态创建scriptcreateScript : function(src){ var varScript = document.createElement(&q ...
- codeforces 659C . Tanya and Toys 二分
题目链接 将给出的已经有了的排序, 在前面加上0, 后面加上1e9+1. 然后对相邻的两项判断. 如果相邻两项之间的数的和小于m, 那么全都选上, m减去相应的值. 如果大于m, 那么二分判断最多能选 ...
- hdu 4059 The Boss on Mars 容斥
题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...
- c++ containers
顺序容器 array(C++11) vector string deque forward_list(C++11) list 容器适配器 stack queue priority_queue 关联容器 ...