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的更多相关文章

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

    题目:Shoot the Bullet 收藏:http://www.tuicool.com/articles/QRr2Qb 把每一天看成一个点,每个女孩也看成一个点,增加源和汇s.t,源向每一天连上[ ...

  2. ZOJ 3229 Shoot the Bullet [上下界最大流]

    ZOJ 3229 Shoot the Bullet 题意:此生无悔入东方 上下界最大流 spj挂掉了我也不知道对不对,把代码放这里吧以后正常了可能会评测一下 #include <iostream ...

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

    /** zoj提交评判不了,所以不知道代码正不正确.思路是应该没问题的.如果有不对的地方,请多指教. 题目:Shoot the Bullet ZOJ - 3229 链接:https://vjudge. ...

  4. ZOJ 3229 Shoot the Bullet

    Shoot the Bullet Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origin ...

  5. ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

    题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...

  6. Shoot the Bullet(有源汇带上下界最大流)

    有源汇带上下界最大流 在原图基础上连一条汇点到源点流量为inf的边,将有源汇网络流转化为无源汇网络流用相同方法判断是否满流,如果满流再跑一边源点到汇点的最大流就是答案 例题:Shoot the Bul ...

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

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

  8. ZOJ3229 Shoot the Bullet(有源汇流量有上下界网络的最大流)

    题目大概说在n天里给m个女孩拍照,每个女孩至少要拍Gi张照片,每一天最多拍Dk张相片且都有Ck个拍照目标,每一个目标拍照的张数要在[Lki, Rki]范围内,问最多能拍几张照片. 源点-天-女孩-汇点 ...

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

    #pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string ...

随机推荐

  1. android 71 ArrayAdapter和SimpleAdapter

    Activity和item: Activity:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...

  2. Linux模式设计系列( 内核与应用关联思考)

    http://blog.chinaunix.net/uid/20608849/cid-25333-list-2.html

  3. sql like '%x%'优化

    好久没写点什么了.唉(此处省略无数,一切尽在苦逼中...) 说说sql中的全匹配优化吧.在sql server进行模糊查询的时候,如果是进行全匹配的话,那么肯定会用到like.我们知道like '%张 ...

  4. HDU-4593(水题)

    Robot Problem Description A robot is a mechanical or virtual artificial agent, usually an electro-me ...

  5. (转)Spring 读书笔记-----使用Spring容器(一)

    Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spr ...

  6. PHP的无限栏目分类

    自己在PHP的无线栏目分类上面就是搞了很久都没有明白,所以现在是趁着记忆力还没有完全的消退的时候速度的记录下来 这里讲解的是最简单的树形栏目,适合的是小中型的栏目分类需求 1.这里讲解的是针对是只要通 ...

  7. ASP.NET 相关小知识

    后台修改前台html控件属性 添加 runat=server ,后台获取// 客户端隐藏 a.Attributes[ "style "] = "display:none ...

  8. c#中string.trimstart() 和string.trimend() 的用法

    trim(),trimstart(),trimend()这样写是去掉空格,trimstart(a)是去掉字符串开始包含char[] a的字符,trimend同trimstart. 例:char[] a ...

  9. ios开发中MVC模式的理解

    MVC是80年代出现的一种软件设计模式,是模型(model),视图(view)和控制(Controller)的缩写. 其中Model的主要功能包括业务逻辑的处理以及数据的访问,这是应用程序的主体部分. ...

  10. c#数组的交集,差集,并集

    , , , , }; , , , , }; // 差集 var z1 = x.Except(y); foreach (var i in z1) { Console.Write(i + " & ...