题目: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(无源汇上下界最大流)的更多相关文章

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

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

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

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

  3. zoj 2314 Reactor Cooling (无源汇上下界可行流)

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

  4. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

  5. 【无源汇上下界最大流】SGU 194 Reactor Cooling

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=194 题目大意: n个点(n<20000!!!不是200!!!RE了无数次) ...

  6. hdu 4940 Destroy Transportation system (无源汇上下界可行流)

    Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  7. zoj2314 无源汇上下界可行流

    题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...

  8. ZOJ2314 Reactor Cooling(无源汇上下界可行流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  9. ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

    题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...

随机推荐

  1. 【EF 译文系列】模型和数据库连接

    原文链接:Connections and Models 本篇文章主要包括 Entity Framework  是如何选择数据库进行连接,以及我们如何去改变它的连接.无论是通过 Code First 还 ...

  2. 不要迷恋那些没技术含量的Linux发行版

    昨天悲剧了,重装系统,一个手贱点了替换原系统,分区全给删了,将近三天的工作成果没有了.

  3. jquery fadeOut 异步

    1. 概述 jquery实现动画效果的函数使用起来很方便,不过动画执行是异步的, 所以要把自定义的操作放在回调函数里. 2. example <html> <body> < ...

  4. 深入.NET框架

    .NET是微软公司在2000年推出的一个战略(平台). 其目的就是想 任何人使用任何终端设备在任何地方都可以访问微软提供的服务. .NET Framework两大组件: CLR(Common Lang ...

  5. CentOS 编译安装 MySQL5.7

    下载 所有版本下载地址: http://dev.mysql.com/downloads/mysql/ 此处用 5.7.10 wget http://dev.mysql.com/get/Download ...

  6. 总结一下SQL的全局变量

    SQL Server 2008中的全局变量及其用法 T-SQL程序中的变量分为全局变量和局部变量两类,全局变量是由SQL Server系统定义和使用的变量.DBA和用户可以使用全局变量的值,但不能自己 ...

  7. AngularJS 最常用的功能

    第一 迭代输出之ng-repeat标签ng-repeat让table ul ol等标签和js里的数组完美结合 1 2 3 4 5 <ul> <li ng-repeat="p ...

  8. ArcGIS制图之Maplex自动点抽稀

    制图工作中,大量密集点显示是最常遇到的问题.其特点是分布可能不均匀.数据点比较密集,容易造成空间上的重叠,影响制图美观.那么,如果美观而详细的显示制图呢? 主要原理 Maplex中对标注有很好的显示控 ...

  9. anriod TabHost

    package com.example.yanlei.mytk; import android.os.Bundle; import android.support.v7.app.AppCompatAc ...

  10. Bonobo Git Server (Simple git server for Windows.) 测试备忘

    Bonobo Git Server是一款Windows上的Git Server,它使用IIS即可,走的是Http协议,只要简单的安装就能使用,但是因为我的项目大小有1.35GB在 push 的时候一直 ...