ZOJ 3910 Market
Market
Time Limit: 2 Seconds Memory Limit: 65536 KB
There's a fruit market in Byteland. The salesmen there only sell apples.
There are n salesmen in the fruit market and the i-th salesman will sell at most wi apples. Every salesman has an immediate manager pi except one salesman who is the boss of the market. A salesmanA is said to be the superior of another salesman B if at least one of the followings is true:
- Salesman A is the immediate manager of salesman B.
- Salesman B has an immediate manager salesman C such that salesman A is the superior of salesman C.
The market will not have a managerial cycle. That is, there will not exist a salesman who is the superior of his/her own immediate manager.
We will call salesman x a subordinate of another salesman y, if either y is an immediate manager of x, or the immediate manager of x is a subordinate to salesman y. In particular, subordinates of the boss are all other salesmen of the market. Let the degree of the boss be 0. Then if the degree of i-th salesman is k, the immediate subordinates of i-th salesman will have degree k + 1.
Today, m buyers come to market for apples. The i-th buyer will buy at most ci apples only from the xi-th salesman and his subordinates whose degree is no larger than xi-th salesman's degree plus di.
The boss wants to know how many apples can be sold in salesmen's best effort (i.e. the maximum number).
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers n and m (1 ≤ n, m ≤ 10000) — the number of salesmen and the number of buyers.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 105). Every wi denotes the number of apples that i-th salesman can sell.
The next line contains n integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th salesman. If pi is -1, that means that the i-th salesman does not have an immediate manager.
Each of the next m lines contains three integers ci, xi and di (1 ≤ ci ≤ 105, 1 ≤ xi ≤ n, 0 ≤ di ≤ n) — the information of i-th buyer.
It is guaranteed that the total number of salesmen in the input doesn't exceed 105, and the total number of buyers also doesn't exceed 105. The number of test cases in the input doesn't exceed 500.
Output
For each test case, output a single integer denoting the maximum number of apples can be sold.
Sample Input
1
4 2
1 2 3 4
-1 1 2 3
3 2 1
5 1 1
Sample Output
6
Author: LIN, Xi
Source: ZOJ Monthly, October 2015
解题:网络流,关键是,由于点多,导致边更多,直接建图,爆内存。。。
感谢Claris的帮助,在花费了一两天的时间,终于搞定了
#include <bits/stdc++.h>
using namespace std;
const int N = ,M = ,INF = 0x3f3f3f3f;
int n,m;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[];
int cur[M],h[M],gap[M],head[M],S,T,tot;
void addedge(int u,int v,int flow){
if(!u || !v) return;
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
int sap(int u,int low){
if(u == T) return low;
int ret = ;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && h[u] == h[e[i].to] + ){
int a = sap(e[i].to,min(low,e[i].flow));
e[i].flow -= a;
e[i^].flow += a;
low -= a;
ret += a;
if(!low) return ret;
}
}
if(!(--gap[h[u]])) h[S] = T;
gap[++h[u]]++;
cur[u] = head[u];
return ret;
}
namespace REDUCE {
int cnt,tot,leaf[N],dep[N],root[N],l[M],r[M],head[N];
int hd[N],num;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[N];
struct QU {
int to,L,R,next;
QU(int to = ,int L = ,int R = ,int nxt = -) {
this->to = to;
this->L = L;
this->R = R;
this->next = nxt;
}
} Q[N];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void addask(int u,int v,int L,int R) {
Q[num] = QU(v,L,R,hd[u]);
hd[u] = num++;
}
int merge(int x,int y,int L,int R) {
if(!x) return y;
if(!y) return x;
int z = ++cnt;
if(L == R) {
addedge(z,x,INF);
addedge(z,y,INF);
return z;
}
int mid = (L + R)>>;
addedge(z,l[z] = merge(l[x],l[y],L,mid),INF);
addedge(z,r[z] = merge(r[x],r[y],mid + ,R),INF);
return z;
}
int build(int L,int R,int pos) {
int x = ++cnt;
if(L == R) return x;
int mid = (L + R)>>;
if(pos <= mid) addedge(x,l[x] = build(L,mid,pos),INF);
if(pos > mid) addedge(x,r[x] = build(mid+,R,pos),INF);
return x;
}
void ask(int root,int L,int R,int lt,int rt,int node) {
if(!root) return;
if(lt <= L && rt >= R) {
addedge(node,root,INF);
return;
}
int mid = (L + R)>>;
if(lt <= mid && l[root]) ask(l[root],L,mid,lt,rt,node);
if(rt > mid && r[root]) ask(r[root],mid + ,R,lt,rt,node);
}
void dfs(int u,int depth) {
dep[u] = depth;
root[u] = build(,n,dep[u]);
leaf[u] = cnt;
for(int i = head[u]; ~i; i = e[i].next)
dfs(e[i].to,depth + );
}
void dfs(int u) {
for(int i = head[u]; ~i; i = e[i].next) {
dfs(e[i].to);
root[u] = merge(root[u],root[e[i].to],,n);
}
for(int i = hd[u]; ~i; i = Q[i].next)
ask(root[u],,n,Q[i].L,min(n,Q[i].R),Q[i].to);
}
void init() {
memset(head,-,sizeof head);
num = tot = cnt = ;
memset(hd,-,sizeof hd);
memset(l,,sizeof l);
memset(r,,sizeof r);
}
}
int have[N],need[N],hs[N];
int main() {
int kase,u,v,w,rt;
scanf("%d",&kase);
memset(head,-,sizeof head);
while(kase--) {
scanf("%d%d",&n,&m); tot = ;
REDUCE::init();
for(int i = ; i <= n; ++i)
scanf("%d",have + i);
for(int i = ; i <= n; ++i) {
scanf("%d",&u);
if(~u) REDUCE::add(u,i);
else rt = i;
}
REDUCE::dfs(rt,);
for(int i = ; i <= m; ++i) {
scanf("%d%d%d",need + i,&u,&v);
REDUCE::addask(u,hs[i] = ++REDUCE::cnt,REDUCE::dep[u],v + REDUCE::dep[u]);
}
REDUCE::dfs(rt);
S = ++REDUCE::cnt;
T = ++REDUCE::cnt;
for(int i = ; i <= m; ++i)
addedge(S,hs[i],need[i]);
for(int i = ; i <= n; ++i)
addedge(REDUCE::leaf[i],T,have[i]);
int maxflow = ;
gap[] = T;
while(h[S] < T) maxflow += sap(S,INF);
printf("%d\n",maxflow);
for(int i = ; i <= T; i++) {
h[i] = gap[i]=;
head[i] = -;
}
}
return ;
}
ZOJ 3910 Market的更多相关文章
- ZOJ 3910 Market ZOJ Monthly, October 2015 - H
Market Time Limit: 2 Seconds Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- LYDSY模拟赛day2 Market
/* orz claris,这个题的解法非常巧妙,首先是时间问题,其实这个问题只要离线处理一下就可以了,把物品和询问都按照时间排序,然后看一下能不能满足.然后,因为容量<=10^9,显然是不可能 ...
随机推荐
- Hdu 5358 First One (尺取法+枚举)
题目链接: Hdu 5358 First One 题目描述: 数组a有n个元素,S[i,j]定义为a[i]+a[i+1]+.....+a[j],问:这个死东西等于多少? 解题思路: 二分肯定超,这个题 ...
- PHP获取今天内的时间 今天开始和结束的时间戳
$t = time(); $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y", ...
- 升序 Collections.sort(list) 降序 Collections.reserve(list) 随机 Collections.shuffle(list)
package Day28ketangzuoye; import java.util.ArrayList; import java.util.Collections; import java.util ...
- 总结用CoreText绘制文本时遇到的问题以及解决办法
关于CoreText不做解释.用的人自然知道这个是干什么的. 功能非常强大,可以绘制文本,图片等. 这次用的Xcode7.0的版本.所以之前很多方法,现在不能用.也不是不能用,就是有黄色警告很不爽. ...
- zoj3699Dakar Rally
链接 开两个队列 一个维护价格从大到小用来每次更新买油的价格 让每次都加满 如果当前价格比队列里的某价格低的话就更新 另开以优先队列维护价格由小到大 来更新此时用的油是什么油价的 并减掉 #inclu ...
- 【C++】异常简述(三):补充之如何看待C++异常
C++异常的使用,我相信在上文总结的已经比较完整了,本文主要对C++异常这块进行额外的补充. 即使C++将异常纳入标准已经很多年了,但是直到现在都能看到很多坚持不显式使用异常.(包括本人在内,在写的代 ...
- mongodb用户权限管理(二)
数据库 分配用户权限 有了创建语法,和参数说明,接下来开始实践. 注意,还有一点,账号是跟着数据库绑定的,在那个库里授权,就在那个库里验证(auth) 否则会失败 创建 账号管理授权权限 的账号 &g ...
- iOS截取特定的字符串(正则匹配)
有时候我们会有需求从一个字符串中截取其他的字符串,根据情况的不同,我们来分析几种方法~~ 一. 固定长度字符串中截取固定位置长度的字符串 // 这是比较简单的一种情况:比如截取手机号的后4位 let ...
- Laravel 的 API 认证系统 Passport 三部曲(二、passport的具体使用)
GQ1994 关注 2018.04.20 09:31 字数 1152 阅读 1316评论 0喜欢 1 参考链接 Laravel 的 API 认证系统 Passport 三部曲(一.passport安装 ...
- iview构建 初始化的时候不要装ESlint 太烦人了
iview构建 Node.js Vue(全局安装) npm install -g vue-cli npm install vue-cli 问题:发现如果不全局安装VUE-cli,\n在它初始化的时候, ...