题意:给定一个分层图,即只能够在相邻层次之间流动,给定了各个顶点的层次。要求输出一个阻塞流。

分析:该题直接Dinic求最大流TLE了,网上说采用Isap也TLE,而最大流中的最高标号预流推进(HLPP)能够直接秒掉这一题。当然还有一种挽救的方式就是首先进行一次贪心预流,然后进行dinic。也是第一次听说还有贪心预流这回事,所以找了一份代码特地学习了一番。具体步骤如下:

1.首先将所有节点按照层次进行排序,对每个节点有in[i]和out[i]两个属性,前者表示能够流入到该节点的流量,后者表示能够流出该节点的流量;
2.从层次最低的节点(即源点)开始,设in[S] = inf,表示源点能够进入无限的流量,然后按照层次的递增来推流量,维护好每个节点的in和out值;
3.从层次最高的节点(即汇点)开始,将所有节点的in都清空为0,设in[T] = inf,表示汇点能够收集无限的流量,然后从后往前计算出每条边实际能够流动的流量。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; const int N = ;
const int M = ;
const int inf = 0x3f3f3f3f;
int n, m, L, SS, TT;
int lv[N], rank[N], in[N], out[N]; struct Edge {
int v, c, nxt;
}e[M<<];
int idx, head[N];
int dis[N];
char vis[N];
queue<int>q; void insert(int a, int b, int c) {
e[idx].v = b, e[idx].c = c;
e[idx].nxt = head[a];
head[a] = idx++;
} bool bfs() {
memset(dis, 0xff, sizeof (dis));
memset(vis, , sizeof (vis));
dis[SS] = , vis[SS] = ;
q.push(SS);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = ;
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v, c = e[i].c;
if (dis[v] == - && c) {
dis[v] = dis[u] + ;
if (!vis[v]) {
vis[v] = ;
q.push(v);
}
}
}
}
return dis[TT] != -;
} int dfs(int u, int flow) {
if (u == TT) return flow;
int tf = , f;
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v, c = e[i].c;
if (dis[u]+ == dis[v] && c && (f = dfs(v, min(flow-tf, c)))) {
e[i].c -= f, e[i^].c += f;
tf += f;
if (tf == flow) return tf;
}
}
if (!tf) dis[u] = -;
return tf;
} void dinic() {
while (bfs()) {
dfs(SS, inf);
}
} bool cmp(const int &a, const int &b) {
return lv[a] < lv[b];
} void greedy() {
memset(in, , sizeof (in));
memset(out, , sizeof (out));
sort(rank+, rank++n, cmp);
in[SS] = inf;
for (int i = ; i <= n; ++i) {
int u = rank[i];
for (int j = head[u]; ~j; j = e[j].nxt) {
int v = e[j].v, c = e[j].c;
if (!(j & ) && in[u] > out[u]) {
int f = min(c, in[u]-out[u]);
in[v] += f, out[u] += f;
}
}
}
memset(in, , sizeof (in));
in[TT] = inf;
for (int i = n; i >= ; --i) {
int v = rank[i];
for (int j = head[v]; ~j; j = e[j].nxt) {
int u = e[j].v, c = e[j^].c;
if (j & && out[u] > in[u]) {
int f = min(c, min(out[u]-in[u], in[v]));
in[u] += f, in[v] -= f;
e[j].c += f, e[j^].c -= f;
}
}
}
} int main() {
int T;
scanf("%d", &T);
while (T--) {
idx = ;
memset(head, 0xff, sizeof (head));
scanf("%d %d %d", &n, &m, &L);
for (int i = ; i <= n; ++i) {
rank[i] = i;
scanf("%d", &lv[i]);
if (lv[i] == ) SS = i;
else if (lv[i] == L) TT = i;
}
int a, b, c;
for (int i = ; i < m; ++i) {
scanf("%d %d %d", &a, &b, &c);
insert(a, b, c), insert(b, a, );
}
greedy(), dinic();
for (int i = ; i < m; ++i) {
printf("%d\n", e[i<<|].c);
}
}
return ;
}

ZOJ-2364 Data Transmission 分层图阻塞流 Dinic+贪心预流的更多相关文章

  1. 网络流--最大流--hlpp(预流推进)模板

    //500ms 秒掉洛谷推流问题 #include <algorithm> #include <iostream> #include <cstring> #incl ...

  2. 洛谷4400 BlueMary的旅行(分层图+最大流)

    qwq 首先,我们观察到题目中提到的每天只能乘坐一次航班的限制,很容易想到建分层图,也就是通过枚举天数,然后每天加入一层新的点. (然而我一开始想的却是erf) 考虑从小到大枚举天数,然后每次新建一层 ...

  3. bzoj 1189: [HNOI2007]紧急疏散evacuate 分层图最大流_拆点_二分

    Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一 块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是 ...

  4. ACdream 1017 [分层图][网络流]

    /* 大连热身C题 不要低头,不要放弃,不要气馁,不要慌张 题意: 给一个城市路线图,给定起点给定终点.有n个货物从起点运送到终点.城市的边是无向边. 每个货物每天如果通过某条路,那么这天这条路只能运 ...

  5. 【bzoj2834】回家的路 分层图最短路

    题目描述 输入 输出 样例输入 2 1 1 2 1 1 2 2 样例输出 5 题解 分层图最短路 dis[i][0]表示到i为横向时起点到i的最短路,dis[i][1]表示到i为纵向时起点到i的最短路 ...

  6. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  7. [USACO09FEB] Revamping Trails 【分层图+Dijkstra】

    任意门:https://www.luogu.org/problemnew/show/P2939 Revamping Trails 题目描述 Farmer John dutifully checks o ...

  8. ACdream 1229 Data Transmission

    Data Transmission Special JudgeTime Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (J ...

  9. free(分层图最短路)(2019牛客暑期多校训练营(第四场))

    示例: 输入: 3 2 1 3 11 2 12 3 2 输出:1 题意:求s,t最短路,可将k条边权值置零. 题解:分层图最短路原题 #include<bits/stdc++.h> usi ...

随机推荐

  1. Environment中针对的读写权限判断

    Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态,并且需要在AndroidManifes ...

  2. [vuforia][unity3d]资源链接

    http://bbs.csdn.net/topics/390787189 CSDN论坛中 “Qualcomm Vuforia(AR虚拟现实)开发” 主题资源下载 http://bbs.csdn.net ...

  3. Oracle绑定变量

    select * from table where id = ? 类似于上面这样的sql,如果不用绑定变量,每次执行时Oracle会认为是不同的sql,会在每次执行时生成一遍执行计划,而执行计划的生成 ...

  4. java初学。加载图片

    public class GameFrame extends Frame{ private static final int WIDTH=900; private static final int H ...

  5. The Cow Lexicon

    The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...

  6. Improving the GPA 分类: 贪心 HDU 比赛 2015-08-08 16:12 11人阅读 评论(0) 收藏

    Improving the GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  7. 精通 ASP.NET MVC 4 学习笔记(一)

    这里记录着从 P132 到 P192 的内容.水分很足,大部分是书上的代码,我只加了一些基于我自己的理解的能帮助初学者看懂的注释,并且把书中的部分内容做了一些的拓展. 建立数据层 设置 DI 容器 / ...

  8. uva 11417 - GCD

    GCDInput: Standard Input Output: Standard Output Given the value of N, you will have to find the val ...

  9. poj3349

    http://poj.org/problem?id=3349 每个雪花都有六个分支,用六个整数代表,这六个整数是从任意一个分支开始,朝顺时针或逆时针方向遍历得到的.输入多个雪花,判断是否有形状一致的雪 ...

  10. shell脚本之lnmp的搭建

    !/bin/bash #this script is source packages installed lnmp .xmal yum -y install wget #"========= ...