题目:

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. Hadoop不同模式下关键配置属性

    Hadoop分为三种模式: 独立(或本地)模式. 伪分布模式. 全分布模式 不同模式下关键配置项及属性内容如下面表格所示 组件名称 配置的文件名 属性名称 独立模式 伪分布模式 全分布模式 Commo ...

  2. layui计算剩余时间

    <div id="test"></div> <script> layui.use('util', function(){ var util = ...

  3. Artisan 命令

    php artisan key:generate 生成 App Key php artisan make:controller 生成控制器 php artisan make:model 生成模型 ph ...

  4. DES 加密

    package com.cloudunicomm.utils; import java.io.UnsupportedEncodingException; import java.security.Se ...

  5. springboot --> web 应用开发-CORS 支持

    一.Web 开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS 等等 CORS 与 JSONP 相比 1. JSONP 只能实现 GET 请求,而 CORS 支持所有类型的 HTT ...

  6. Docker_入门?只要这篇就够了!(纯干货适合0基础小白)

    与sgy一起开启你的Docker之路 关键词: Docker; mac; Docker中使用gdb无法进入断点,无法调试; 更新1: 看起来之前那一版博文中参考资料部分引用的外站链接太多,被系统自动屏 ...

  7. CI框架源代码阅读笔记2 一切的入口 index.php

    上一节(CI框架源代码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程.这里再次贴出流程图.以备參考: 作为CI框架的入口文件.源代码阅读,自然由此開始. 在源代码阅读的 ...

  8. [android]DES/3DES/AES加密方式

    DES 支持8位加密解密,3Des支持24位,Aes支持32位.3Des是Des算法做三次.位数的单位是字节byte.不是bits. 3Des是把24位分成3组.第一组八位用来加密,第二组8位用于解密 ...

  9. vs2012碰到生成时报该错误:项目中不存在目标 “XXXXXX”

    vs2012碰到生成时报该错误:项目中不存在目标 "XXXXXX" 首先打开project文件,找到 以下信息: <Import Project="$(MSBuil ...

  10. bzoj1497: [NOI2006]最大获利(最大权闭合子图)

    1497: [NOI2006]最大获利 题目:传送门 题解: %%%关于最大权闭合子图很好的入门题 简单说一下什么叫最大权闭合子图吧...最简单的解释就是正权边连源点,负权边连汇点(注意把边权改为正数 ...