题目:

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. [USACO10NOV]奶牛的图片Cow Photographs 树状数组 递推

    Code: #include<cstdio> #include<algorithm> #include<string> #include<cstring> ...

  2. React中的AES加解密请求

    引言 在我们使用React开发Web前端的时候,如果是比较大的项目和正常的项目的话,我们必然会用到加解密,之前的文章中提到.NET的一些加解密,那么,这里我就模拟一个例子: 1.后台开发API接口,但 ...

  3. LightOJ-1259 Goldbach`s Conjecture 数论 素数筛

    题目链接:https://cn.vjudge.net/problem/LightOJ-1259 题意 给一个整数n,问有多少对素数a和b,使得a+b=n 思路 素数筛 埃氏筛O(nloglogn),这 ...

  4. BZOJ 4103 [Thusc 2015]异或运算 (可持久化01Trie+二分)

    题目大意:给你一个长方形矩阵,位置$i,j$上的数是$a_{i}\;xor\;b_{j}$,求某个子矩阵内第$K$大的值 最先想的是二分答案然后验证,然而是$O(qnlogmloga_{i})$,不出 ...

  5. Jtester使用

    1.在Jtester中使用DataMap 为什么要使用DataMap? 早先的jTester中提供了dbFit方式来准备和验证数据库数据,应该来说,这个工具解决了很多问题.实际使用过程中,开发同学反映 ...

  6. 【codeforces 67A】Partial Teacher

    [题目链接]:http://codeforces.com/problemset/problem/67/A [题意] 给一个长度为n-1的字符串; 每个字符串是'L','R','='这3种字符中的一个; ...

  7. linux 流量统计小程序

    源代码例如以下: //2015/7/2 10:30:35 //gino #include <stdio.h> #include <sys/stat.h> #include &l ...

  8. hdu_3308 区间合并

    一两个月没写代码的确是手生的厉害,debug的好艰辛,,不过看到accept时的那种满足感真的就是爽 #include<iostream> #include<cstdio> # ...

  9. word 的使用 —— 分页符与分节符

    节的概念:节定义了一些格式, 如页边距.页面的方向.页眉和页脚,以及页码的顺序. 分节符是指为表示节的结尾插入的标记. 分节符的作用: 分节符起着分隔其前后文本格式的作用,如果删除了某个分节符,它前面 ...

  10. 13.C语言隐藏黑窗口

    //预编译,linker链接,Windows模式 #pragma comment(linker,"/subsystem:\"windows\" /entry:\" ...