描述

ensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utopia where humans and other beings such as fairies, youkai(phantoms), and gods live peacefully together. Shameimaru Aya is a crow tengu with the ability to manipulate wind who has been in Gensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns the Bunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautiful danmaku(barrange) or cute girls living in Gensokyo. She is the biggest connoisseur of rumors about the girls of Gensokyo among the tengu. Her intelligence gathering abilities are the best in Gensokyo!

During the coming n days, Aya is planning to take many photos of m cute girls living in Gensokyo to write Bunbunmaru News daily and record at least Gx photos of girl x in total in the Bunkachou. At the k-th day, there are Ck targets, Tk1Tk2, ..., TkCk. The number of photos of target Tki that Aya takes should be in range [LkiRki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use her last spell card to attack Aya. What's more, Aya cannot take more than Dk photos at the k-th day. Under these constraints, the more photos, the better.

Aya is not good at solving this complex problem. So she comes to you, an earthling, for help.

Input

There are about 40 cases. Process to the end of file.

Each case begins with two integers 1 <= n <= 365, 1 <= m <= 1000. Then m integers, G1G2, ..., Gm in range [0, 10000]. Then n days. Each day begins with two integer 1 <= C <= 100, 0 <= D <= 30000. Then C different targets. Each target is described by three integers, 0 <= Tm, 0 <= L <= R <= 100.

Output

For each case, first output the number of photos Aya can take, -1 if it's impossible to satisfy her needing. If there is a best strategy, output the number of photos of each girl Aya should take at each day on separate lines. The output must be in the same order as the input. If there are more than one best strategy, any one will be OK.

Output a blank line after each case.

Sample Input

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 3 9
1 3 9
2 3 9 2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 0 3
1 3 6
2 6 9 2 3
12 12 12
3 15
0 3 9
1 3 9
2 3 9
3 21
0 0 3
1 3 6
2 6 12

Sample Output

36
6
6
6
6
6
6 36
9
6
3
3
6
9 -1

题意:

大致是说一个男生要个其他女生拍照,总共拍N天,总共有M个女生,

接下来一行是说M个女生,每个女生至少要拍照的数量

然后每天都有自己的拍照计划,给多少女生拍照,今天拍照的总数量

接下来一行是对于女生x,这一天拍照的上限L和下限R

题解:

有源汇上下界最大流,先建图,首先源点和每天连线,上届是这一天拍照总数,下界是0,每一天再和当天拍照的女生建边,上下界是L,R,每个女生和汇点建边,上下界是INf,和给这个女生拍照的最少数量。

然后转换为无源汇上下界最大流,建立超级源点和汇点,跑最大流看是否满流,是则,再跑一遍,求出最大流,否输出-1

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
#define ll long long
#define MAXN 30000
using namespace std;
int n,m;//点数、边数
int sp,tp;//原点、汇点
struct node
{
int v,next;
ll cap;
}mp[MAXN*10];
int pre[MAXN],dis[MAXN],cur[MAXN];//cur为当前弧优化,dis存储分层图中每个点的层数(即到原点的最短距离),pre建邻接表
int cnt=0;
void init()//不要忘记初始化
{
cnt=0;
memset(pre,-1,sizeof(pre));
}
void add(int u,int v,int w)//加边
{
mp[cnt].v=v;
mp[cnt].cap=w;
mp[cnt].next=pre[u];
pre[u]=cnt++;
mp[cnt].v=u;
mp[cnt].cap=0;
mp[cnt].next=pre[v];
pre[v]=cnt++;
}
bool bfs()//建分层图
{
memset(dis,-1,sizeof(dis));
queue<int>q;
while(!q.empty())
q.pop();
q.push(sp);
dis[sp]=0;
int u,v;
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=pre[u];i!=-1;i=mp[i].next)
{
v=mp[i].v;
if(dis[v]==-1&&mp[i].cap>0)
{
dis[v]=dis[u]+1;
q.push(v);
if(v==tp)
break;
}
}
}
return dis[tp]!=-1;
}
ll dfs(int u,ll cap)//寻找增广路
{
if(u==tp||cap==0)
return cap;
ll res=0,f;
for(int &i=cur[u];i!=-1;i=mp[i].next)
{
int v=mp[i].v;
if(dis[v]==dis[u]+1&&(f=dfs(v,min(cap-res,mp[i].cap)))>0)
{
mp[i].cap-=f;
mp[i^1].cap+=f;
res+=f;
if(res==cap)
return cap;
}
}
if(!res)
dis[u]=-1;
return res;
}
ll dinic()
{
ll ans=0;
while(bfs())
{
for(int i=0;i<=tp;i++)
cur[i]=pre[i];
ans+=dfs(sp,inf);
}
return ans;
}
int d[MAXN],a[MAXN];
int g[400][1100];
int st[400][1100];
int main() {
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
memset(d,0, sizeof(d));
int sum=0;
int x;
for (int i = 1; i <=m ; ++i) {
scanf("%d",&x);
add(n+i,n+m+1,inf);
d[n+i]-=x;
d[n+m+1]+=x;
}
int b;
for (int i = 1; i <=n ; ++i) {
scanf("%d%d",&a[i],&b);
add(0,i,b);
int l,r;
for (int j = 0; j <a[i] ; ++j) {
scanf("%d%d%d",&x,&l,&r);
add(i,n+x+1,r-l);
d[i]-=l;
d[n+x+1]+=l;
st[i][j]=l;
g[i][j]=(cnt-2);
}
}
add(n+m+1,0,inf); for (int i = 0 ; i <=n+m+1 ; ++i) {
if(d[i]>0)
{
add(n+m+2,i,d[i]);
sum+=d[i];
}
if(d[i]<0)
{
add(i,n+m+3,-d[i]);
}
}
sp=n+m+2;tp=n+m+3;
if(sum!=dinic())
{
printf("-1\n");
}
else
{
sp=0,tp=n+m+1;
printf("%lld\n",dinic());
for (int i = 1; i <=n ; ++i) {
for (int j = 0; j <a[i] ; ++j) {
printf("%lld\n",mp[g[i][j]+1].cap+(ll)st[i][j]);
}
} }
printf("\n");
}
return 0;
}
//zoj3229
//loj116

  

Shoot the Bullet(ZOJ3229)(有源汇上下界最大流)的更多相关文章

  1. zoj 3229 Shoot the Bullet(有源汇上下界最大流)

    Shoot the Bullethttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 Time Limit: 2 Second ...

  2. zoj3229 有源汇上下界最大流

    题意:有一个人每天给妹子拍照,每个妹子有最少拍照数,每天有最大拍照数,每天只能给某些特定的妹子拍照,求最大拍照数 题解:很容易看出来的有源汇上下界最大流,对于有源汇 的上下界最大流,我们按照无源汇的操 ...

  3. 【有源汇上下界最大流】ZOJ 3229 Shoot the Bullet

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229 题目大意: n天给m个女孩拍照(1<=n<= ...

  4. ZOJ 3229 Shoot the Bullet(有源汇上下界最大流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 题目大意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给 ...

  5. 【有源汇上下界费用流】BZOJ 3876 [Ahoi2014]支线剧情

    题目链接: http://www.lydsy.com:808/JudgeOnline/problem.php?id=3876 题目大意: 给定一张拓扑图(有向无环图),每条边有边权,每次只能从第一个点 ...

  6. POJ2396 Budget [有源汇上下界可行流]

    POJ2396 Budget 题意:n*m的非负整数矩阵,给出每行每列的和,以及一些约束关系x,y,>=<,val,表示格子(x,y)的值与val的关系,0代表整行/列都有这个关系,求判断 ...

  7. 有源汇上下界可行流(POJ2396)

    题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...

  8. BZOJ_2502_清理雪道_有源汇上下界最小流

    BZOJ_2502_清理雪道_有源汇上下界最小流 Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道), ...

  9. 【Loj117】有源汇上下界最小流(网络流)

    [Loj117]有源汇上下界最小流(网络流) 题面 Loj 题解 还是模板题. #include<iostream> #include<cstdio> #include< ...

随机推荐

  1. Angular 5.x 学习笔记(2) - 生命周期钩子 - 暂时搁浅

    Angular 5.x Lifecycle Hooks Learn Note Angular 5.x 生命周期钩子学习笔记 标签(空格分隔): Angular Note on cnblogs.com ...

  2. June 12th 2017 Week 24th Monday

    All the splendor in the world is not worth a good friend. 人世间所有的荣华富贵都比不上有一个好朋友. It's great to have a ...

  3. scrum 第二次冲刺

    scrum 第二次冲刺 1.本周工作 本周正式开始了开发工作.首先设计了类图,建好了数据库,将整个小组的分工传到了禅道上,我主要负责后台的挂号操作. 本周分工如下: 首先搭建好了ssm框架,其中遇到了 ...

  4. Vim中 ctags 跳转直接跳到第一个匹配行的问题

    意图 用ctags搜索代码时, 用 ctrl + ] 后,只有一个匹配项直接跳转,有多个则列出所有匹配项选择跳转 问题 在 vim 中使用 ctags 是一个很令人舒服的事情,但有时一些默认的配置和不 ...

  5. c++11之100行实现简单线程池

    代码从github上拷的,写了一些理解,如有错误请指正 Threadpool.h #ifndef THREAD_POOL_H #define THREAD_POOL_H #include <ve ...

  6. UESTC 574 High-level ancients

    分析: 无论父节点增加了多少,子节点的增量总比父节点多1. 这种差分的关系是保存不变的,我们可以一遍dfs根据结点深度得到在根结点的每个点的系数. 估且把一开始的结点深度称做c0吧,对于子树的修改就只 ...

  7. BZOJ3874:[AHOI2014&JSOI2014]宅男计划(爬山法)

    Description  [故事背景] 自从迷上了拼图,JYY就变成了个彻底的宅男.为了解决温饱问题,JYY 不得不依靠叫外卖来维持生计. [问题描述] 外卖店一共有N种食物,分别有1到N编号.第i种 ...

  8. sougou输入法无法正常输入汉字

    删除~/.config目录下的SougouPY SogouPY.users sogou-qimpanel文件夹,然后重启搜狗输入法即可

  9. 【luogu P1082 同余方程】 题解

    最近一直在学习数论,讲得很快,害怕落实的不好,所以做一道luogu的同余方程练练手. 关于x的同余方程 ax ≡ 1 mod m 那么x其实就是求a关于m的乘法逆元 ax + my = 1 对于这个不 ...

  10. 浅谈箭头函数和setTimeout中的this

    箭头函数会改变this的指向,这个大家看文档都看到过,可是有没有具体理解呢?我发现自己应该可能大概是......emmmm,然后我整理了一遍,加强一下概念吧顺带再讲一下setTimeout这个函数改写 ...