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,显然是不可能 ...
随机推荐
- CF446C [DZY loves Fibonacci]
Description Transmission Gate 你需要维护一个长度为\(n \leq 300000\) 的数列,兹词两个操作: 1.给一个区间加上一个fibonacci数列,规定\(f[0 ...
- 51nod 1213 二维曼哈顿距离最小生成树
1213 二维曼哈顿距离最小生成树 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间 ...
- java的构造方法 this 重载
this1.隐含的局部变量在方法中指向调用该方法的对象()使用:当成员变量与局部变量同名的时候,通过this说明哪一个是成员变量.(this指向的是成员变量) 2.作为当前类的构造方法名存在作用:在构 ...
- commons-lang常用工具类StringEscapeUtils使用--转
https://my.oschina.net/ydsakyclguozi/blog/341496 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是 ...
- [转]无废话SharePoint入门教程二[SharePoint发展、工具及术语]
本文转自:http://www.cnblogs.com/iamlilinfeng/p/3186919.html 一.前言 1.由于上一篇文章的标题命名失误,此篇标题写给百度搜索”什么是SharePoi ...
- 死磕 java魔法类之Unsafe解析
问题 (1)Unsafe是什么? (2)Unsafe只有CAS的功能吗? (3)Unsafe为什么是不安全的? (4)怎么使用Unsafe? 简介 本章是java并发包专题的第一章,但是第一篇写的却不 ...
- reveal.js让程序员做ppt也享受快乐
前言 程序员除了会写的一手漂亮的代码,也要求做出风格优雅的PPT,诸如向领导汇报工作.向小组成员反馈项目进展自己的工作等等.就本人而言,做ppt还要去找模板,还需要设计风格,内心是焦灼的.于是乎,我搜 ...
- 聊5块钱P2V
上一秒还在写代码,下一秒就跑机房干活. 这台机器产制石器时代,重启一次后再就启动不了了.这个故障处理的方式我们以后再谈. 今天聊聊啥是P2V,国人总喜欢弄些稀奇古怪的定义来证明自己技术很牛X,就跟当年 ...
- CentOS7.5搭建LAMP环境
导言 LAMP环境搭建,网上可以搜到很多的结果.为什么我还要整理一下呢,是因为有些网上给出的解决办法可能仅适用于某些特定的环境下,并不一定适用于所有出现问题的情况. 当然我写本篇的目的也不是保证所有的 ...
- scala基础篇-03 if与for
一.Scala中的if是表达式** 1.定义方式 2.例子 二.for 的用法 1.定义方式: for{ x <- xs y=x+ ) }yield y 2.例子: