ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】
题目: 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 【有上下界网络流+流量输出】的更多相关文章
- ZOJ 3229 Shoot the Bullet [上下界最大流]
ZOJ 3229 Shoot the Bullet 题意:此生无悔入东方 上下界最大流 spj挂掉了我也不知道对不对,把代码放这里吧以后正常了可能会评测一下 #include <iostream ...
- ZOJ 3229 Shoot the Bullet
Shoot the Bullet Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origin ...
- zoj 3229 Shoot the Bullet(无源汇上下界最大流)
题目:Shoot the Bullet 收藏:http://www.tuicool.com/articles/QRr2Qb 把每一天看成一个点,每个女孩也看成一个点,增加源和汇s.t,源向每一天连上[ ...
- zoj 3229 Shoot the Bullet(有源汇上下界最大流)
Shoot the Bullethttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 Time Limit: 2 Second ...
- 【有源汇上下界最大流】ZOJ 3229 Shoot the Bullet
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229 题目大意: n天给m个女孩拍照(1<=n<= ...
- ZOJ 3229 Shoot the Bullet(有源汇上下界最大流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 题目大意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给 ...
- ZOJ 3229 Shoot the Bullet(有源汇的上下界最大流)
Description Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It ...
- ZOJ 3229 Shoot the Bullet (有源有汇有上下界最大流)
题意:一个人要给女孩子们拍照,一共 n 天,m 个女孩子,每天他至多拍 d[i] 张照片,每个女孩子总共要被至少拍 g[i] 次.在第 i 天,可以拍 c[i] 个女孩子,c[i] 个女孩子中每个女孩 ...
- ZOJ - 3229 Shoot the Bullet (有源汇点上下界最大流)
题意:要在n天里给m个女生拍照,每个女生有拍照数量的下限Gi,每天有拍照数量的上限Di,每天当中每个人有拍照的上限Lij和Rij.求在满足限制的基础上,所有人最大能拍多少张照片. 分析:抛开限制,显然 ...
随机推荐
- 使用TensorFlow编写常用模块的Python代码示例
将数据转化成tfrecords的形式 import os import tensorflow as tf import numpy as np import matplotlib.image as m ...
- 路飞学城Python-Day2
13.变量的定义规范 变量的含义:变量就是定义之后还能发生改变,可以重新赋值的量;变量的定义规范:名字不能随便起,声明一个变量,name = "123"[变量 = 值]变量定义规则 ...
- [agc015c]nuske vs phantom thnook
题意: 有一个n*m的网格图,每个格子是蓝色或白色.四相邻的两个格子连一条边,保证蓝格子构成一个森林. 有q组询问,每次询问给出一个矩形,问矩形内蓝格子组成的联通块个数. $1\leq n,m\leq ...
- UI控件设置
去掉cesium默认的版权信息: 在style样式中添加 .cesium-widget-credits {display:none !important} 去掉动画控件.地址搜索控件.图层选择控件.操 ...
- John Morgan:黎曼几何、曲率、Ricci流以及在三维流形上的应用二讲
本文是笔者在线看Lektorium上John Morgan在圣彼得堡国立大学欧拉研究所的讲座做的笔记.第一讲以如下内容组成 1. 黎曼曲面上的联络 黎曼流形$(M^n,g)$中,$M$为$n$维流形, ...
- 【BZOJ 1196】[HNOI2006]公路修建问题
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二分最后选的边中的最大值是多少. mid 则所有边权小于等于mid的边都可以用了. 那么我们要怎么选择呢? ->优先选择一级的 ...
- XML快速注释
eclipse中编辑java或C/C++,python文件时,注释的快捷键均为 "CTRL + / ",编辑xml文件时,该快捷键无效. eclipse XML 注释:CTRL + ...
- oracle 登录
命令行登录 sql plus登录 plsql登录 自己电脑登录 既然是登录自己电脑的数据库,肯定是已经安装过了oralce,而且已经创建了数据库等等. 局域网登录 1.必须安装oracleclient ...
- Sqlite3的安装Windows
- 微信小程序领取卡券
微信小程序领取卡券 标签(空格分隔): php 开发前需要准备的工作 1 小程序和公众号要有绑定 2 小程序和该公众号要绑定到同一个开发平台下 [https://open.weixin.qq.com/ ...