Shoot the Bullet
zoj3229:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442
题意:一个摄影师,在n天内给m个女神拍照。每个女神至少要拍Gi张照片,每一天只能给Ci个女神照相,每一天只能只能拍Di张照片,并且每个女神每天被拍的数量在[l,r]之间。问是否存在一种方案,满足条件,如果满足,最多可以照多少照片。
题解:这是一条有源汇的有上下界的最大流。首先源点s,t,源点和每一天i建立一边,上界为Di,下界为0,每个女神和t建立一边,上界是无穷,下界是Gi,因为上界是无穷大,所以下界等同为0,然后是每一天与对应的女神之间就是流量范围是[l,r],这里就可以转化成有上下界的流量处理,最后t-->s建立一边,容量是INF,这样就转化成无汇源的可行流。接下就是设超级源点ss,超级会点tt,把上面的图按照可行流的求解方式来求解。如果所有ss的出边都是满流,则有可行解。然后再跑一边最大流,此时源点时s,t,这里不再是超级源点ss,和会点tt。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 1000000000
const int N=;
struct Node {
int c;
int f;
}map[N][N];
int sx,ex,n,m;
int pre[N];
int du[N],down[N][N];
bool BFS(int x) { //BFS搜索层次网络
memset(pre,,sizeof(pre));
queue< int > Q;
Q.push(sx);
pre[sx]=;
while(!Q.empty()) {
int d=Q.front();
Q.pop();
for(int i=; i<=x; i++) {
if(!pre[i]&&map[d][i].c-map[d][i].f) {
pre[i]=pre[d]+;
Q.push(i);
}
}
}
return pre[ex]!=;
}
int dinic(int pos,int flow,int x) { //pos是顶点号,flow是当前顶点所能得到的流量,一次dinic只能求出一次增加的流量,
int f=flow;
if(pos==ex)
return flow;
for(int i=; i<=x; i++) {
if(map[pos][i].c-map[pos][i].f&&pre[pos]+==pre[i]) {
int a=map[pos][i].c-map[pos][i].f;
int t=dinic(i,min(a,flow),x);
map[pos][i].f+=t;
map[i][pos].f-=t;
flow-=t;
if(flow<=)break;
//我最开始就是这里没弄明白,我不明白为什么要此顶点得到的流量减去改变量;
//答案就在下面的 return f-flow;
}
}
if(f-flow<=)pre[pos]=-;
return f-flow;//其实这里返回给他前一层的就是这个t;因为t在层函数里面都有,所以所过避免重复就写成这样;
}
int solve(int x){
int sum=;
while(BFS(x)) {
sum+=dinic(sx,INF,x);
}
return sum;
}
int main() {
int u,v,w,t1,t2,t3;
while(~scanf("%d%d",&n,&m)) {
int s=m+n+,t=s+;
sx=t+,ex=sx+;
memset(map,,sizeof(map));
memset(du,,sizeof(du));
memset(down,-,sizeof(down));
for(int i=;i<=m; i++) {
scanf("%d",&w);
du[i]-=w;
du[t]+=w;
map[i][t].c+=INF;
}
for(int i=;i<=n;i++){
scanf("%d%d",&u,&v);
map[s][i+m].c+=v;
while(u--){
scanf("%d%d%d",&t1,&t2,&t3);
du[t1+]+=t2;
du[i+m]-=t2;
down[i+m][t1+]=t2;
map[i+m][t1+].c+=(t3-t2);
}
}
map[t][s].c=INF;
int sum=;
for(int i=;i<=t;i++){
if(du[i]>){
map[sx][i].c+=du[i];
sum+=du[i];
}
else{
map[i][ex].c+=(-du[i]);
}
}
if(solve(n+m+)!=sum)puts("-1");
else{
sx=s,ex=t;
printf("%d\n",solve(n+m+));
for(int i=m+;i<=n+m;i++){
for(int j=;j<=m;j++){
if(down[i][j]>=){
printf("%d\n",map[i][j].f+down[i][j]);
}
}
}
}
puts("");
}
return ;
}
Shoot the Bullet的更多相关文章
- zoj 3229 Shoot the Bullet(无源汇上下界最大流)
题目:Shoot the Bullet 收藏:http://www.tuicool.com/articles/QRr2Qb 把每一天看成一个点,每个女孩也看成一个点,增加源和汇s.t,源向每一天连上[ ...
- ZOJ 3229 Shoot the Bullet [上下界最大流]
ZOJ 3229 Shoot the Bullet 题意:此生无悔入东方 上下界最大流 spj挂掉了我也不知道对不对,把代码放这里吧以后正常了可能会评测一下 #include <iostream ...
- Shoot the Bullet ZOJ - 3229 有源汇有上下界的最大流
/** zoj提交评判不了,所以不知道代码正不正确.思路是应该没问题的.如果有不对的地方,请多指教. 题目:Shoot the Bullet ZOJ - 3229 链接:https://vjudge. ...
- ZOJ 3229 Shoot the Bullet
Shoot the Bullet Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origin ...
- ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】
题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...
- Shoot the Bullet(有源汇带上下界最大流)
有源汇带上下界最大流 在原图基础上连一条汇点到源点流量为inf的边,将有源汇网络流转化为无源汇网络流用相同方法判断是否满流,如果满流再跑一边源点到汇点的最大流就是答案 例题:Shoot the Bul ...
- zoj 3229 Shoot the Bullet(有源汇上下界最大流)
Shoot the Bullethttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 Time Limit: 2 Second ...
- ZOJ3229 Shoot the Bullet(有源汇流量有上下界网络的最大流)
题目大概说在n天里给m个女孩拍照,每个女孩至少要拍Gi张照片,每一天最多拍Dk张相片且都有Ck个拍照目标,每一个目标拍照的张数要在[Lki, Rki]范围内,问最多能拍几张照片. 源点-天-女孩-汇点 ...
- ZOJ3229 Shoot the Bullet(有源汇的上下界最大流)
#pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string ...
随机推荐
- android 67 生成和解析xml
生成xml: package com.itheima.createxml; import java.io.File; import java.io.FileNotFoundException; imp ...
- LINUX系统全部参数 sysctl -a + 网络参数设置
http://blog.lifeibo.com/?p=380 1.sysctl sysctl命令被用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中 [root@ser ...
- Android开发之使用活动显示对话框
利用活动显示对话框,需要重写Activity中的onCreateDialog()方法,以此来显示一个对话框窗口. 效果如下: 实现代码如下: package com.example.dialog; i ...
- ASP.NET+ashx+jQuery动态添加删除表格
aspx: <script src="../script/jquery-1.4.4.min.js" type="text/javascript" lang ...
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...
- 移动设备日期选择插件(基于JQUERY)
上周花了2个小时写的一个日期选择插件,比较适合移动端的设备.先看个效果图吧.如果刚好是你需要的就往下吧,不需要的也可以继续..... 其实网络上已经有的了类似的成熟插件,比如基于mobiscroll, ...
- 评论一下现有几个开源IM框架(Msn/QQ/Fetion/Gtalk...)
转载:http://www.cnblogs.com/zc22/archive/2010/05/30/1747300.html 前言 ---------------- 这阵子,在集成通讯框架, 由于不想 ...
- SQL Execute语法.
一,执行字符串: EXECUTE语句可以执行存放SQL语句的字符串变量,或直接执行SQL语句字符串. 语法:EXECUTE({@字符串变量|[N]’SQL语句字符串’}[+...n]) 例子:Decl ...
- Winform获取应用程序的当前路径
//获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembly.Location; result: X:\xxx\xxx\xxx.exe ...
- Windows服务安装方法
操作系统:Win8.1 安装方法:在命令行窗口中输入:InstallUtil service.exe 出错原因:需要以管理员身份启动命令行.