当前位置: 首页 > 编程日记 > 正文

Shoot the Bullet(ZOJ3229)(有源汇上下界最大流)

描述

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 92 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 92 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
636
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

转载于:https://www.cnblogs.com/-xiangyang/p/9786196.html

相关文章:

深入理解jQuery插件开发【转】

如果你看到这篇文章&#xff0c;我确信你毫无疑问会认为jQuery是一个使用简便的库。jQuery可能使用起来很简单&#xff0c;但是它仍然有一些奇怪的地方&#xff0c;对它基本功能和概念不熟悉的人可能会难以掌握。但是不用担心&#xff0c;我下面已经把代码划分成小部分&#xf…

L1-008 求整数段和 (C++)

给定两个整数A和B&#xff0c;输出从A到B的所有整数以及这些数的和。 输入格式&#xff1a; 输入在一行中给出2个整数A和B&#xff0c;其中−100≤A≤B≤100&#xff0c;其间以空格分隔。 输出格式&#xff1a; 首先顺序输出从A到B的所有整数&#xff0c;每5个数字占一行&a…

matlab 迭代 混沌与分形实验报告,实验四 函数的迭代混沌与分形.doc

实验四 函数的迭代混沌与分形.doc 实验四函数的迭代、混沌与分形实验目的1认识函数的迭代&#xff1b;2了解混沌和分形迭代在数值计算中占有很重要的地位,了解和掌握它是很有必要的本实验将讨论用NEWTON迭代求方程根的问题,以及迭代本身一些有趣的现象1基本理论11迭代的概念给定…

小霸王双核/四核手机最新参数曝光

2019独角兽企业重金招聘Python工程师标准>>> 此前爆料出小霸王出手机&#xff0c;今天又有新消息啦。 现在又有消息人士给出了小霸王手机最新的参数情况&#xff0c;其厚度为9.8mm&#xff0c;配备的是4.5寸夏普IPS材质触摸屏&#xff0c;分辨率为960x540像素&#…

聊一聊跨域,Vue向Django请求数据的一些问题

1.做前后端分离 前端使用Vue程序&#xff0c;后端使用Django配合rest-framework。 那么前端Vue通过API接口拿到数据会出现跨域的问题&#xff0c;JSONP只是在get中会用到的&#xff0c;所以这里使用cors来解决一下。 一个Vue程序通过ajax或者axios发送一个请求过来&#xff0c;…

php配置控制器和视图位置,视图控制器

视图控制器视图控制器是连接控制器和模板的桥梁, 更是对模板的强大扩展基本用法视图控制器文件夹位于 app\web\views 目录下&#xff0c; 视图控制器的名称是和控制器的名称相对应的, 并在结尾加上View, Main控制器的默认的视图控制器类名为MainView&#xff0c;内容如下:names…

L1-016 查验身份证 (15 分)

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下&#xff1a; 首先对前17位数字加权求和&#xff0c;权重分配为&#xff1a;{7&#xff0c;9&#xff0c;10&#xff0c;5&#xff0c;8&#xff0c;4&#xff0c;2&#xff0c;1&am…

中国互联网的十一种盈利模式

盈利模式一&#xff1a;在线广告 最主要最常见的网络在线盈利模式&#xff0c;国内比较好的是各大门户网站(新浪&#xff0c;搜狐等)&#xff0c;也包括行业门户&#xff0c;而且大多数个人网站的盈利模式也是这样&#xff0c;靠挂别人的广告生存。 新新兴的在线短视频网站…

grid布局初试

/*这是HTML*/<!DOCTYPE html> <html><head><meta charset"utf-8" /><title>main</title><link rel"stylesheet" href"css/header.css" /><link rel"stylesheet" href"css/aside.cs…

matlab文件启动位置,matlab中uigetfile()设置默认路径

每次使用uigetfile()函数选择文件路径&#xff0c;默认都是从current folder中选择数据文件&#xff0c;而current folder路径又不是数据文件&#xff0c;那么每次都需要选择径路好几步&#xff0c;繁琐的很。想通过设置current folder路径&#xff0c;使每次运行时uigetfile直…

thinkphp5框架一小时搭建一个php后端(1)

开发环境使用phpstudy 编辑器用sublime 数据库navicat 需要下载composer 先配置好本地域名&#xff0c;然后需要我们将资源引入到项目里面 下载地址www.layui.com. layui框架有很多我们后台开发需要的控件&#xff0c;帮助我们高效完成后台搭建。 先创建我们的入口文件ad…

usb调试模式已打开,adb devices显示List of devices attached 解决办法!纽维K333一键ROOT,获取ROOT权限!...

usb调试模式已打开&#xff0c;adb devices显示老显示List of devices attached 。刚开始以为USB线问题&#xff0c;跟朋友借了一根&#xff0c;未果。 更换其他的机子测试就可以显示设备&#xff0c;但是这部纽维K333 &#xff08;国产机/android 4.1.1&#xff09;却显示不出…

记录每个登陆用户的操作记录

在linux系统的环境下&#xff0c;不管是root用户还是其它的用户只有登陆系统后用进入操作我们都可以通过命令history来查看历史记录&#xff0c;可是假如一台服务器多人登陆&#xff0c;一天因为某人误操作了删除了重要的数据。这时候通过查看历史记录&#xff08;命令&#xf…

SRM 563 Div1 500 SpellCards

Description 有n张符卡排成一个队列&#xff0c;每张符卡有两个属性&#xff0c;等级lili和伤害didi。 你可以做任意次操作&#xff0c;每次操作为以下二者之一&#xff1a; 把队首的符卡移动到队尾。使用队首的符卡&#xff0c;对敌人造成di点伤害&#xff0c;并丢弃队首的li张…

一小时Thinkphp后台(2)

之前我们已经写好管理员页面&#xff0c;现在对功能继续实现 基础功能1&#xff1a;对管理进行增删改查 增加 需要在view中新建一个add.html add.html <!DOCTYPE html> <html> <head><title></title><link rel"stylesheet" type…

php的延迟绑定,PHP延迟静态绑定使用方法实例解析

这篇文章主要介绍了PHP延迟静态绑定使用方法实例解析,文中通过示例代码介绍的非常详细&#xff0c;对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下PHP的继承模型中有一个存在已久的问题&#xff0c;那就是在父类中引用扩展类的最终状态比较困难。我们来看一…

java中名词概念的理解

方法的重载&#xff1a;方法名称相同&#xff0c;但参数的类型和个数不同&#xff0c;通过传递参数的个数及类型不同以完成不同功能的方法调用。 例如&#xff1a;System.out.println();属于方法的重载。 方法的重载一定是根据参数类型和个数来判断的。 构造函数&#xff1a;构…

Jquery_评分

Description:星星评分--鼠标移动高亮星星来评分&#xff0c;文字描述也对应改变。 KeyTech:无&#xff0c;熟悉Jquery 主要代码: var oContent ["极差", "很差", "一般" , "推荐" , "力荐"];/*定义评价数组*/ var oDiv …

L1-023 输出GPLT (C++解决,含题解)

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序&#xff0c;按GPLTGPLT....这样的顺序输出&#xff0c;并忽略其它字符。当然&#xff0c;四种字符&#xff08;不区分大小写&#xff09;的个数不一定是一样多的&#xff0c;若某种字符已经输出完&…

php要求掌握链表结构吗,PHP 链表结构之单链表(一)

php链表结构&#xff0c;单链表(一)单链表结构&#xff0c;我们这边定义一个节点类&#xff0c;属性有当前值(data)和指向下一个节点的(next)class ListNode {public $data NULL;public $next NULL;public function __construct(string $data NULL) {$this->data $data;…

使用 Sticky-Kit 实现基于 jQuery 的元素固定效果

元素固定效果在网页中应用得很多&#xff0c;比较常见的使用场景有改进导航&#xff0c;显示广告。Sticky-Kit 是一个非常方便的 jQuery 插件&#xff0c;简化了创建/管理粘元素&#xff0c;有复杂的使用功能。这些功能包括&#xff1a;处理多个固定元素&#xff0c;启用/禁用的…

java中的Random()注意!

Random 类专门用于生成一个伪随机数&#xff0c;他有两个构造函数&#xff1a;一个构造函数使用默认的种子&#xff0c;另一个构造函数需要程序员显示传入一个long 类型的种子。同时他提供了很多方法来生成伪随机数。即如果类的两个实例时用同一个种子创建的&#xff0c;对他们…

狄利克雷卷积莫比乌斯反演证明

狄利克雷卷积简介 卷积这名字听起来挺学究的&#xff0c;今天学了之后发现其实挺朴实hhh。 卷积&#xff1a; “&#xff08;n&#xff09;”表示到n的一个范围。 设\(f,g\)是两个数论函数&#xff08;也就是说&#xff0c;以自然数集为定义域的复数值函数&#xff09;&#xf…

L1-027 出租 (C++暴力解法)

L1-027 出租 (20 分) 下面是新浪微博上曾经很火的一张图&#xff1a; 一时间网上一片求救声&#xff0c;急问这个怎么破。其实这段代码很简单&#xff0c;index数组就是arr数组的下标&#xff0c;index[0]2 对应 arr[2]1&#xff0c;index[1]0 对应 arr[0]8&#xff0c;index[…

oracle9i安装不上,终于成功安装oracle9i了(Cent OS 4.0+oracle9204)

本来没想过要做这个总结的&#xff0c;但就安装个数据库来说&#xff0c;在linux下安装oracle简直就是折磨人&#xff0c;它不难&#xff0c;但就是要很细心(&#xff1d;繁琐)&#xff1a;操作系统&#xff1a;Cent OS&#xff0d;4ISOs(相当于RedHat Enterprise linux 4.0)or…

UESTC 1811 Hero Saving Princess

九野的博客&#xff0c;转载请注明出处 http://blog.csdn.net/acmmmm/article/details/11104265 题目链接 &#xff1a;http://222.197.181.5/problem.php?pid1811 题意&#xff1a;T个测试数据 n m //n个点 m条边 m条无向边 que//下面有que个数据 a b // 表示a点的钥匙在b中…

VC:其他控件(CProgressCtrl、CScrollBar、CDateTimeCtrl、CMonthCalCtrl)

1、进度条 m_progressCtrl.SetRange(0,100); for(int i0;i<100;i) { m_progressCtrl.SetPos(i); Sleep(100); } AfxMessageBox("进度条到达终点"); 2、滑块控件&#xff1a;添加WM_VSCROLL消息。 void COtherCtrlDlg::OnHScroll(UINT nSBCode, UINT nPos, CScroll…

获取checkbox所选中的值

<input name"demand" type"checkbox" value"222" />//获取所有name为demand的对象var obj document.getElementsByName(demand);var demand ;for (var i 0; i < obj.length; i) {if (obj[i].checked) {demand obj[i].value ,;//如…

oracle plsql开启并行,Oracle开启并行的几种方法

并行执行是同时开启多个进程/线程来完成同一个任务&#xff0c;并行执行的每一个进程/线程都会消耗额外的硬件资源&#xff0c;所以并行执行的本质就是以额外的硬件资源消耗来换取执行时间的缩短。这里的额外硬件资源消耗是指对数据库服务器上多个CPU、内存、从个I/O通道&#…

L1-044 稳赢 (暴力法)

L1-044 稳赢 (15 分) 大家应该都会玩“锤子剪刀布”的游戏&#xff1a;两人同时给出手势&#xff0c;胜负规则如图所示&#xff1a; 现要求你编写一个稳赢不输的程序&#xff0c;根据对方的出招&#xff0c;给出对应的赢招。但是&#xff01;为了不让对方输得太惨&#xff0c;…