题意:给你一张图,每个城市有一些人,有不超过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. 【知识强化】第五章 传输层 5.3 TCP协议

    这节课我们来学习一下TCP协议的特点以及TCP报文段的格式. 首先呢我们来看一下TCP有哪些特点呢.之前我们说过TCP它是一个比较可靠的面向连接的协议,所以最主要的特点它是可以面向连接的一种传输层协议 ...

  2. 三、IIS通过目录方式部署以供外部调试

    一.IIS 下面是通过 gif 为 因项目是bin生成后的,非运行方式的调试,所以断点调试无效,仅修改文件后,右击项目重新生成解决方案即可,好处:启动快,坏处:不可以断点调试查看变量和分步执行语句.

  3. 关于ovf导入到Esxi上出显的“文件条目(行1)无效,sha256……”处理办法

    通常删除同名文件*.mf文件即可导入!

  4. 转载 初学者必看——最简单最清晰的Struts2项目搭建流程

    https://blog.csdn.net/key0323/article/details/50773499 在项目中想要使用Struts2框架,我该怎么做?从哪里开始?这是我的疑惑,我想也是很多初学 ...

  5. postgres服务之加密

    数据中往往会出现一些敏感字段,例如电话,邮箱等,这时候有需求进行加密保存 目前可以实现的方式有两种 方式一:这种方法,aes的加密方法不支持aes-192,不支持aes-256 #使用encrypt加 ...

  6. SSH出错

    [root@node01 ~]# ssh node02 ssh_exchange_identification: Connection closed by remote host 修改连接数无效 [r ...

  7. 怀旧浪潮来袭,小霸王游戏、windows95......曾经的经典哪些能戳中你的心怀?

    随着前两天上架的 Rewound 在 iPhone 上复刻了 iPod Classic为大家掀起一场怀旧浪潮,那么除了 Rewound还有什么经典?今天我们就来怀旧一下那些曾经的经典.80经典小霸王游 ...

  8. hdu 6134 Battlestation Operational (莫比乌斯反演+埃式筛)

    Problem Description   > The Death Star, known officially as the DS-1 Orbital Battle Station, also ...

  9. POJ 2387 Til the Cows Come Home (dijkstra模板题)

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  10. 使用vue完成一个分页效果

    基于 element-ui 分页组件实现分页效果 效果如下: 使用说明: 0.首先在头部引入需要的外部文件 1.从element官方网页中复制想要的组件代码直接放入body中 2.编写逻辑代码 3.完 ...