题意:给你一张图,每个城市有一些人,有不超过10个城市有避难所,避难所有容量上限,问最快多久可以让所有人进入避难所?

思路:二分时间,对于每个时间跑一遍最大流,判断最大流是不是人数即可。我们还需要用二进制优化一下,对于每个二分的时间,我们需要预处理出某个城市可以到达哪些避难所,表示成状态。如果在当前时间下两个城市可以到达的避难所是一样的,我们就可以用一个状态表示,这样把点数限制在了1e3级别。

代码:

#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#define LL long long
#define pLi pair<LL ,int>
#define pii pair<int, int>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
using namespace __gnu_pbds;
const int maxn = 200100;
const int maxm = 501000;
typedef __gnu_pbds::priority_queue<pLi, greater<pLi>, pairing_heap_tag> Heap;
int heade[maxn],
Nexte[maxm], vere[maxm], edgee[maxm], tote;
LL dis[10][maxn];
pair<int, int> b[20];
int n, k;
int a[maxn];
LL re[maxn * 10];
void adde(int x, int y, int z) {
vere[++tote] = y;
edgee[tote] = z;
Nexte[tote] = heade[x];
heade[x] = tote;
}
Heap q;
int s, t;
void dijkstra(int s, int flag) {
memset(dis[flag], 0x3f, sizeof(dis[flag]));
Heap::point_iterator id[maxn];
dis[flag][s] = 0;
id[s] = q.push(make_pair(dis[flag][s], s));
while(!q.empty()) {
int x = q.top().second;
q.pop();
for (int i = heade[x]; i; i = Nexte[i]) {
int y = vere[i], z = edgee[i];
if(dis[flag][y] > dis[flag][x] + z) {
dis[flag][y] = dis[flag][x] + z;
if(id[y] != 0) q.modify(id[y], make_pair(dis[flag][y], y));
else id[y] = q.push(make_pair(dis[flag][y], y));
}
}
}
}
int head[maxn], Next[maxn * 25], ver[maxn * 25], tot;
LL edge[maxn * 25];
int deep[maxn];
void add(int x, int y, LL z) {
ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;
ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot;
}
queue<int> Q;
bool bfs(LL limit) {
memset(deep, 0, sizeof(deep));
while(Q.size())Q.pop();
Q.push(s); deep[s] = 1;
while(Q.size()) {
int x = Q.front();Q.pop();
for (int i = head[x]; i; i = Next[i]) {
if(edge[i] && !deep[ver[i]]) {
Q.push(ver[i]);
deep[ver[i]] = deep[x] + 1;
if(ver[i] == t) return 1;
}
}
}
return 0;
}
LL dinic(int x, LL flow) {
if(x == t) return flow;
LL rest = flow, k;
for (int i = head[x]; i && rest; i = Next[i]) {
if(edge[i] && deep[ver[i]] == deep[x] + 1) {
k = dinic(ver[i], min(rest, edge[i]));
if(!k) deep[ver[i]] = 0;
edge[i] -= k;
edge[i ^ 1] += k;
rest -= k;
}
}
return flow - rest;
}
LL solve(int now) {
tot = 1;
LL maxflow = 0;
// memset(head, 0, sizeof(head));
memset(head, 0, sizeof(int) * (n + 1));
for (int i = 0; i < k; i++)
head[b[i].first + n] = 0;
head[t] = 0;
for (int i = 1; i <= n; i++) {
if(a[i])
add(s, i, a[i]);
}
LL tmp;
for (int i = 1; i <= n; i++) {
tmp = 0;
if(a[i] == 0) continue;
for (int j = 0; j < k; j++) {
if(dis[j][i] >= re[now]) continue;
tmp += b[j].second;
add(i, b[j].first + n, INF);
}
if(tmp < a[i]) {
return 0;
}
}
for (int i = 0; i < k; i++)
add(b[i].first + n, t, b[i].second);
LL flow;
while(bfs(re[now])) {
while(flow = dinic(s, INF)) maxflow += flow;
}
return maxflow;
}
int main() {
LL cnt = 0;
s = 0, t = 2 * n + 20;
int m, x, y, z;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
cnt += a[i];
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
adde(x, y, z);
adde(y, x, z);
}
for (int i = 0; i < k; i++) {
scanf("%d%d", &b[i].first, &b[i].second);
dijkstra(b[i].first, i);
}
for (int i = 0; i < k; i++)
for (int j = 1; j <= n; j++) {
re[++tot] = dis[i][j];
}
sort(re + 1, re + 1 + tot);
int sz = unique(re + 1, re + 1 + tot) - (re + 1);
int l = 1, r = sz + 1;
re[sz + 1] = INF;
while(l < r) {
int mid = (l + r) >> 1;
if(solve(mid) == cnt) r = mid;
else l = mid + 1;
}
printf("%lld\n", re[l - 1]);
}

  

Gym 102007I 二分 网络流的更多相关文章

  1. hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

    #1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...

  2. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: ...

  3. BZOJ_3993_[SDOI2015]星际战争_二分+网络流

    BZOJ_3993_[SDOI2015]星际战争_二分+网络流 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进 ...

  4. 【bzoj3130】[Sdoi2013]费用流 二分+网络流最大流

    题目描述 Alice和Bob做游戏,给出一张有向图表示运输网络,Alice先给Bob一种最大流方案,然后Bob在所有边上分配总和等于P的非负费用.Alice希望总费用尽量小,而Bob希望总费用尽量大. ...

  5. 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

    题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...

  6. 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

    题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possi ...

  7. 【bzoj1532】[POI2005]Kos-Dicing 二分+网络流最大流

    题目描述 Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一个俱乐部. 俱乐部的人时常在一起玩这个游戏然后评选出玩得最好的人.现在有一个非常不走运的 ...

  8. BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流

    BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流 Description Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一 ...

  9. HDU 3081 Marriage Match II 二分 + 网络流

    Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...

随机推荐

  1. hibernate 参数一览

    实现包含了Hibernate与数据库的基本连接信息的配置方式有两种方式: 第一种是使用hibernate.properties文件作为配置文件. 第二种是使用hibernate.cfg.xml文件作为 ...

  2. 大哥带我走渗透ii--时间盲注,布尔盲注

    5/27 基于布尔的盲注 我连题目都看不懂555,先去补充一点知识.https://blog.csdn.net/weixin_40709439/article/details/81355856 返回的 ...

  3. 九、结构模式之装饰(Decorator)模式

    装饰模式又叫包装模式,装饰模式以客户端透明的方式扩展对象的功能,是继承关系的一个替代方案.装饰模式可以在不使用创造更多的子类的情况下,将对象的功能加以扩展. 装饰模式结构图如下: 其包含的角色就分为: ...

  4. 二、JPA的注解

    @Entity注类就表示实体类了.注意:必须要有@Entity注解,否则会报错. @Table里面的就是表名和类名进行映射. @Id标识主键列,@GeneratedValue主键生成策略配合@Id使用 ...

  5. @HttpEntity参数(怪异)

    1).在Controller中写 与@RequestBody请求体对应 @HttpEntity更强大,不光有请求体,还能获取请求头 @RequestMapping("/test02" ...

  6. sqoop 数据导入hive

    一. sqoop: mysql->hive sqoop import -m 1 --hive-import --connect "jdbc:mysql://127.0.0.1:3306 ...

  7. Es学习第五课, 分词器介绍和中文分词器配置

    上课我们介绍了倒排索引,在里面提到了分词的概念,分词器就是用来分词的. 分词器是ES中专门处理分词的组件,英文为Analyzer,定义为:从一串文本中切分出一个一个的词条,并对每个词条进行标准化.它由 ...

  8. vue部分问题

    [color=#00b050]学 vue 的看过来,vue-cli 挺好用的,但是遇到具体情况还得做一部分调整和配置默认你已经成功启动 vue-cli 1.使用 scsscnpm i node-sas ...

  9. Future模式的简单实现

    /** * 数据接口 */ public interface Data { public String getResult(); } /** * 最终需要使用的数据模型 */ public class ...

  10. 威胁快报|Bulehero挖矿蠕虫升级,PhpStudy后门漏洞加入武器库

    概述 近日,阿里云安全团队监控到Bulehero挖矿蠕虫进行了版本升级,蠕虫升级后开始利用最新出现的PHPStudy后门漏洞作为新的攻击方式对Windows主机进行攻击,攻击成功后会下载门罗币挖矿程序 ...