zoj 3229 Shoot the Bullet(无源汇上下界最大流)
收藏:http://www.tuicool.com/articles/QRr2Qb
把每一天看成一个点,每个女孩也看成一个点,增加源和汇s、t,源向每一天连上[0,d]的边,每一天与每个女孩如果有拍照任务的话连上[l,r]的边,
每个女孩与汇连上[g,oo]的边,于是构成一个有上下界的图。
所以这道题目我们可以转换一下,只要连一条T → S的边,流量为无穷,没有下界,那么原图就得到一个无源汇的循环流图。
接下来的事情一样:原图中的边的流量设成自由流量ci – bi。新建源点SS汇点TT,求Mi,连边。然后求SS → TT最大流,判是否满流。

view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1<<30;
const int N = 1500;
const int M = 1e6;
int n, m, pre[N], d[N], cur[N], s, t, ss, tt;
int dis[N], in[N], low[36666], lcnt;
bool vis[N]; struct edge
{
int u, v, cap, next;
edge() {}
edge(int u, int v, int c, int next):u(u),v(v),cap(c),next(next) {}
}e[M];
int ecnt; void addedge(int u, int v, int w)
{
e[ecnt] = edge(u, v, w, pre[u]);
pre[u] = ecnt++;
e[ecnt] = edge(v, u, 0, pre[v]);
pre[v] = ecnt++;
} bool BFS(int s, int t)
{
memset(vis, 0, sizeof(vis));
queue<int >q;
q.push(s);
vis[s] = 1;
d[s] = 0;
while(!q.empty())
{
int x = q.front(); q.pop();
for(int i=pre[x]; ~i; i=e[i].next)
{
int v = e[i].v;
if(!vis[v] && e[i].cap>0)
{
vis[v] = 1;
d[v] = d[x] + 1;
q.push(v);
}
}
}
return vis[t];
} ll DFS(int x, ll c, int s, int t)
{
if(x==t || c==0) return c;
ll flow = 0, f;
for(int &i = cur[x]; ~i; i = e[i].next)
{
int v = e[i].v;
if(d[v] == d[x]+1 && (f=DFS(v, min(c, (ll)e[i].cap), s, t))>0)
{
e[i].cap -= f;
e[i^1].cap += f;
flow += f;
c -= f;
if(c==0) break;
}
}
return flow;
} ll Maxflow(int s, int t)
{
ll flow = 0;
while(BFS(s, t))
{
memcpy(cur, pre, sizeof(pre));
flow += DFS(s, INF, s, t);
}
return flow;
} int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &m)>0)
{
ll sum = 0;
int g, id, R, k;
s = 0, t = m+n+1;
ss = n+m+2, tt = m+n+3;
memset(pre, -1, sizeof(pre));
memset(in, 0, sizeof(in));
ecnt = 0, lcnt = 0;
for(int i=1; i<=m; i++)
{
scanf("%d", &g);
in[i+n] -= g;
in[t] += g;
}
for(int i=1; i<=n; i++)
{
scanf("%d%d", &k, &dis[i]);
for(int j=0; j<k; j++)
{
scanf("%d%d%d", &id, &low[++lcnt], &R);
addedge(i, id+n+1, R-low[lcnt]);
in[i] -= low[lcnt];
in[id+n+1] += low[lcnt];
}
}
for(int i= 1; i<=n; i++) addedge(s, i, dis[i]);
for(int i= 1; i<=m; i++) addedge(i+n, t, INF);
addedge(t, s, INF);
for(int i = s; i<=t; i++)
{
if(in[i]>0) addedge(ss, i, in[i]), sum += in[i];
if(in[i]<0) addedge(i, tt, -in[i]);
}
if(Maxflow(ss, tt)==sum)
{
Maxflow(s, t);
addedge(t, s, -INF);
int ans = 0;
for(int i = pre[0]; ~i; i=e[i].next)
{
ans += e[i^1].cap ;
// printf("u = %d, cap = %d\n", e[i^1].u, e[i^1].cap);
}
printf("%d\n", ans);
for(int i=1; i<=lcnt; i++) printf("%d\n", e[2*i-1].cap+low[i]);
}
else puts("-1");
puts("");
}
return 0;
}
zoj 3229 Shoot the Bullet(无源汇上下界最大流)的更多相关文章
- ZOJ 3229 Shoot the Bullet(有源汇上下界最大流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 题目大意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给 ...
- ZOJ - 3229 Shoot the Bullet (有源汇点上下界最大流)
题意:要在n天里给m个女生拍照,每个女生有拍照数量的下限Gi,每天有拍照数量的上限Di,每天当中每个人有拍照的上限Lij和Rij.求在满足限制的基础上,所有人最大能拍多少张照片. 分析:抛开限制,显然 ...
- zoj 2314 Reactor Cooling (无源汇上下界可行流)
Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...
- ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...
- 【无源汇上下界最大流】SGU 194 Reactor Cooling
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=194 题目大意: n个点(n<20000!!!不是200!!!RE了无数次) ...
- hdu 4940 Destroy Transportation system (无源汇上下界可行流)
Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 ...
- zoj2314 无源汇上下界可行流
题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...
- ZOJ2314 Reactor Cooling(无源汇上下界可行流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
- ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】
题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...
随机推荐
- 自定义ConfigurationSection,创建多个嵌套的ConfigurationElementCollection节点
由于接口地址都是固定的,所以想到使用自定义节点,来将接口都配置到web.config中. 很快,v1.0版本出炉: public class RequestConfigSection : Config ...
- myeclipse的实用快捷键
(1)Ctrl+M切换窗口的大小(2)Ctrl+Q跳到最后一次的编辑处(3)F2当鼠标放在一个标记处出现Tooltip时候按F2则把鼠标移开时Tooltip还会显示即Show Tooltip Desc ...
- where T : class的含义
public class Reflect<T> where T : class { 这是参数类型约束,指定T必须是Class类型. .NET支持的类型参数约束有以下五种:where T : ...
- 常用库nuget包集合
ColorConsole htmlagilitypack.1.4.9.5 经测试效率比 CsQueryLaster 高 csvhelper Extend Devlib系列一套 itextsharp l ...
- java入门基础知识点总结
JavaScript他是一种描述性语言,其实他并不难学,只要用心学,一定会学好,我相信大家在看这篇文章的时候,一定也学过HTML吧,使用JavaScript就是为了能和网页有更好的交互,下面切入主题. ...
- saltstack学习笔记1 --安装
salt官网:http://docs.saltstack.cn/zh_CN/latest/ 安装教程: - http://docs.saltstack.cn/zh_CN/latest/topics/i ...
- javascript宿主对象之window.history
window.historys属性允许我们操作同一个浏览器回话中的已访问页面,例如我们可以看到在这之前我们浏览页面的数量: window.history.length 由于隐私保护,我们无法获取这些页 ...
- Google Developers中国网站
正于北京举办的谷歌开发者大会上,谷歌宣布,Google Developers中国网站 (developers.google.cn) 正式发布! 谷歌表示,Google Developers中国网站是特 ...
- GridControl列自动匹配宽度
//自动调整所有字段宽度this.gridView1.BestFitColumns(); //调整某列字段宽度this.gridView1.Columns[n].BestFit(); 大多是网上零散找 ...
- Oracle SQL Tips
左连接的同时只输出关联表的一条记录 WITH X AS (SELECT 1 ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL ...