题目:

problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet

分类:有源有汇有上下界网络流

题意:有 n 天和 m 个girls,然后每天给一部分girls拍照,每一个girls 有拍照的下限。即最少要拍这么多张。然后每天有k个女孩拍照,摄影师最多能够拍num张,然后 k 个女该每天拍照数量值有上下限,然后问你有没有满足这样条件的给女孩拍照的最慷慨案。然后依照输入输出每天给女孩拍照的张数。

做这道题目推荐先做:这儿

分析:首先它让你推断能不能满足条件。

依照题目给出的条件非常easy建图:

s ---> 天数,

天数  ---->  girls

girls  ----> t

(流量都为当前的上界减去下界)

这种话就建图成功了,可是这样不能推断。我们在加一条边 t -----> s,流量无穷

这种话原图编程一个循环图。即前面推荐的题目。依照那样的方法建图,然后求一次最大流看看是否满流。

然后增加满流的话。就能够考虑第二个问题了。每天给女孩拍照的最大的张数?

这样我们能够删去 t ---->  s 得边,然后求一次 s 到 t 的最大流。

然后依照题目要求输出流量就ok。

PS:伤在了英语上,没有读懂题目,然后错了无数次,最后最终懂了,坑题目。

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#define Del(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1550;
struct Node
{
int from,to,cap,flow;
};
vector<int> v[N];
vector<Node> e;
int vis[N]; //构建层次图
int cur[N];
void add_Node(int from,int to,int cap)
{
e.push_back((Node){from,to,cap,0});
e.push_back((Node){to,from,0,0});
int tmp=e.size();
v[from].push_back(tmp-2);
v[to].push_back(tmp-1);
}
bool bfs(int s,int t)
{
Del(vis,-1);
queue<int> q;
q.push(s);
vis[s] = 0;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0;i<v[x].size();i++)
{
Node tmp = e[v[x][i]];
if(vis[tmp.to]<0 && tmp.cap>tmp.flow) //第二个条件保证
{
vis[tmp.to]=vis[x]+1;
q.push(tmp.to);
}
}
}
if(vis[t]>0)
return true;
return false;
}
int dfs(int o,int f,int t)
{
if(o==t || f==0) //优化
return f;
int a = 0,ans=0;
for(int &i=cur[o];i<v[o].size();i++) //注意前面 ’&‘,非常重要的优化
{
Node &tmp = e[v[o][i]];
if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))>0)
{
tmp.flow+=a;
e[v[o][i]^1].flow-=a; //存图方式
ans+=a;
f-=a;
if(f==0) //注意优化
break;
}
}
return ans; //优化
}
int dinci(int s,int t)
{
int ans=0;
while(bfs(s,t))
{
Del(cur,0);
int tm=dfs(s,inf,t);
ans+=tm;
}
return ans;
}
void MP_clear(int n)
{
for(int i=0;i<=n;i++)
v[i].clear();
e.clear();
}
int come[N],to[N];
int flow[400][N];
vector<pair<int ,int> > pp;
int main()
{
//freopen("Input.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m))
{
Del(come,0);
Del(to,0);
Del(flow,0);
int s=n+m,t = s+1;
for(int i=0;i<m;i++)
{
int x;
scanf("%d",&x);
come[n+i]+=x;
to[t]+=x;
add_Node(n+i,t,inf);
}
for(int i=0;i<n;i++)
{
int cas,num;
scanf("%d%d",&cas,&num);
add_Node(s,i,num);
for(int j=0;j<cas;j++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
pair<int,int> ts(i,x);
pp.push_back(ts);
flow[i][x]+=y;
come[i]+=y;
to[n+x]+=y;
add_Node(i,n+x,z-y); //SB
}
}
add_Node(t,s,inf); //
int ss=t+1,tt=ss+1;
int count=0;
for(int i=0;i<=t;i++)
{
int cha = come[i]-to[i];
if(cha<0)
{
count+=-cha;
add_Node(ss,i,-cha);
}
if(cha>0)
add_Node(i,tt,cha);
}
int ans = dinci(ss,tt);
if(ans != count)
puts("-1");
else
{
//add_Node(t,s,0);
printf("%d\n",dinci(s,t)); //SB
for(int i=0;i<e.size();i++)
{
Node f = e[i];
if(i%2==0 && f.from<n){
flow[f.from][f.to-n]+=f.flow;
//printf("%d %d %d %d\n",f.from,f.to,f.cap,f.flow);
}
}
for(int i=0;i<pp.size();i++)
{
printf("%d\n",flow[pp[i].first][pp[i].second]);
}
}
printf("\n");
MP_clear(tt);
pp.clear();
}
return 0;
}

ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】的更多相关文章

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

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

  2. ZOJ 3229 Shoot the Bullet

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

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

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

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

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

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

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

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

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

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

    Description Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It ...

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

    题意:一个人要给女孩子们拍照,一共 n 天,m 个女孩子,每天他至多拍 d[i] 张照片,每个女孩子总共要被至少拍 g[i] 次.在第 i 天,可以拍 c[i] 个女孩子,c[i] 个女孩子中每个女孩 ...

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

    题意:要在n天里给m个女生拍照,每个女生有拍照数量的下限Gi,每天有拍照数量的上限Di,每天当中每个人有拍照的上限Lij和Rij.求在满足限制的基础上,所有人最大能拍多少张照片. 分析:抛开限制,显然 ...

随机推荐

  1. 企业微信H5支付返回不到自己指定的结果页面

    项目:vue做的H5项目,嵌入到企业微信里 发现的问题:支付完成后的跳转页面 跳转不到自己指定的页面 问题在于:调支付接口时,redirectUrl: document.location.protoc ...

  2. 强化学习(2)----Q-learning

    1.Q-learning主要是Q表: 当前状态s1,接下来可以有两个动作选择,看电视a1和学习a2,对于agent人来说,可以根据reward来作出决策(Policy).目的就是得到奖励最大. Q-l ...

  3. 不实例化一个 class 的时候使用它的property

    class A: @property def name(self): " print(A.name) # <property object at 0x10d54cf98> cla ...

  4. (2016北京集训十)【xsy1528】azelso - 概率期望dp

    北京集训的题都是好题啊~~(于是我爆0了) 注意到一个重要的性质就是期望是线性的,也就是说每一段的期望步数可以直接加起来,那么dp求出每一段的期望就行了... 设$f_i$表示从$i$出发不回到$i$ ...

  5. 移动端mete设置

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  6. weak和alias

    一.强符号和弱符号 在C语言中,如果多个模块定义同名全局符号时,链接器认为函数和已初始化的全局变量(包括显示初始化为0)是强符号,未初始化的全局变量是弱符号. 根据这个定义,Linux链接器使用下面的 ...

  7. 嵌入式(C)笔试题

    1 读程序段,回答问题 (a) int main(int argc,char *argv[]) { int c=9,d=0; c=c++%5; d=c; printf("d=%d\n&quo ...

  8. Android平台Camera实时滤镜实现方法探讨(九)--磨皮算法探讨(一)

    上一篇开头提到了一些可用于磨皮的去噪算法.以下我们实现这些算法而且观察效果,咱不考虑实时性的问题 本文首先探讨的首先是<基于局部均方差相关信息的图像去噪及其在实时磨皮美容算法中的应用> 该 ...

  9. caffe 训练測试自己的数据集

    简单记录一下自己使用caffe的过程和遇到的一些问题. 下载caffe以及安装不具体叙述了. 可參照 http://caffe.berkeleyvision.org/installation.html ...

  10. sass08 if while for each

    scss @function getzIndex($layer: default){ $zindexMap: (default: 1, modal: 1000, dropdown: 500, grid ...