最大流ISAP,邻接表+GAP+当前弧优化
//时间为3877ms
#include <stdio.h>
#include <string.h>
#define VM 100010
#define EM 400010
const int inf = 0x3f3f3f3f;
struct E
{
int to, frm, nxt, cap;
}edge[EM];
int head[VM],e,n,m,src,des;
int dep[VM], gap[VM];
void addedge(int cu, int cv, int cw)
{
edge[e].frm = cu;
edge[e].to = cv;
edge[e].cap = cw;
edge[e].nxt = head[cu];
head[cu] = e++;
edge[e].frm = cv;
edge[e].to = cu;
edge[e].cap = 0;
edge[e].nxt = head[cv];
head[cv] = e++;
}
int que[VM];
void BFS()
{
memset(dep, -1, sizeof(dep));
memset(gap, 0, sizeof(gap));
gap[0] = 1;
int front = 0, rear = 0;
dep[des] = 0;
que[rear++] = des;
int u, v;
while (front != rear)
{
u = que[front++];
front = front%VM;
for (int i=head[u]; i!=-1; i=edge[i].nxt)
{
v = edge[i].to;
if (edge[i].cap != 0 || dep[v] != -1)
continue;
que[rear++] = v;
rear = rear % VM;
++gap[dep[v] = dep[u] + 1];
}
}
}
int cur[VM],stack[VM];
int Sap() //sap模板
{
int res = 0;
BFS();
int top = 0;
memcpy(cur, head, sizeof(head));
int u = src, i;
while (dep[src] < n)
{
if (u == des)
{
int temp = inf, inser = n;
for (i=0; i!=top; ++i){
if (temp > edge[stack[i]].cap){
temp = edge[stack[i]].cap;
inser = i;
}
}
for (i=0; i!=top; ++i)
{
edge[stack[i]].cap -= temp;
edge[stack[i]^1].cap += temp;
}
res += temp;
top = inser;
u = edge[stack[top]].frm;
}
if (u != des && gap[dep[u] -1] == 0)
break;
for (i = cur[u]; i != -1; i = edge[i].nxt)
if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1)
break;
if (i != -1)
{
cur[u] = i;
stack[top++] = i;
u = edge[i].to;
}
else
{
int min = n;
for (i = head[u]; i != -1; i = edge[i].nxt)
{
if (edge[i].cap == 0)
continue;
if (min > dep[edge[i].to])
{
min = dep[edge[i].to];
cur[u] = i;
}
}
--gap[dep[u]];
++gap[dep[u] = min + 1];
if (u != src)
u = edge[stack[--top]].frm;
}
}
return res;
}
int main()
{
int T, i;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
int x, y;
int Min = inf, Max = -inf;
for (i=1; i<=n; ++i) //找出起点src 终点des
{
scanf("%d%d", &x, &y);
if (x <= Min)
{
src = i;
Min = x;
}
if (x >= Max)
{
des = i;
Max = x;
}
}
e = 0;
memset(head, -1, sizeof(head));
int u, v, c;
for (i=0; i!=m; ++i)
{
scanf("%d%d%d", &u, &v, &c);
addedge(u,v,c);
addedge(v,u,c);
}
int ans = Sap();
printf("%d\n", ans);
}
return 0;
}
时间2770ms:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
//Macro
//STL-Alias
#define UN(c, a) unique(c, a)
#define MS(c, a) memset(c, a, sizeof c)
#define FLC(c, a ,b) fill(c, c + a, b)
#define LOS(c, a, b) lower_bound(c, a, b)
#define UPS(c, a, b) upper_bound(c, a, b)
//Syntax-Alias
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
//DEBUG
#define FK puts("Fu*k here!")
#define PA(s, c, a, b, p, f){\
printf(s);\
Rep(c, a, b) printf(p, (f));\
puts("");}
//Constant
#define INFL (0x7fffffffffffffffLL)
#define INFI (0x7fffffff)
#define MOD ()
#define MAXN (100005)
//Type-Alias
typedef long long LL;
typedef long double LD;
typedef int AI[MAXN];
typedef bool AB[MAXN];
typedef double AD[MAXN];
typedef LL ALL[MAXN];
typedef LD ALD[MAXN];
//FSG
struct Edge
{
int v, w, next;
};
vector<Edge> E;
AI L;
void G_ini()
{
E.clear();
MS(L, -1);
}
void AE(int u, int v, int w)
{
Edge te = {v, w, L[u]};
L[u] = E.size();
E.push_back(te);
}
#define Ere(c, a, L) for (int c = L[a]; c != -1; c = E[c].next)
//ISAP
int ISAP(int src, int snk, int V)
{
static AI dis, gap, _L, se;
int u = src, mxf = 0, te = 0;
memcpy(_L, L, sizeof L);
MS(dis, -1); MS(gap, 0);
gap[dis[snk] = 0] = 1;
queue<int> Q;
Q.push(snk);
while (!Q.empty())
{
int u = Q.front(); Q.pop();
Ere(i, u, L) if (E[i].w && dis[E[i].v] < 0)
{
gap[dis[E[i].v] = dis[u] + 1]++;
Q.push(E[i].v);
}
}
while (dis[src] < V)
{
int _i = -1;
Ere(i, u, _L) if (E[i].w && dis[u] == dis[E[i].v] + 1)
{
_i = i;
break;
}
if (_i != -1)
{
_L[u] = _i;
u = E[se[te++] = _i].v;
}
else
{
int md = INFI;
_i = -1;
Ere(i, u, L) if (E[i].w && dis[E[i].v] < md)
{
md = dis[E[i].v];
_L[u] = i;
}
if (!--gap[dis[u]]) break;
gap[dis[u] = md + 1]++;
if (u != src) u = E[se[te-- - 1] ^ 1].v;
}
if (u == snk)
{
int _i = 0, mf = INFI;
Rep(i, 0, te) if (E[se[i]].w < mf)
{
mf = E[se[i]].w;
_i = i;
}
Rep(i, 0, te)
{
E[se[i]].w -= mf;
E[se[i] ^ 1].w += mf;
}
mxf += mf;
te = _i;
u = E[se[_i] ^ 1].v;
}
}
return mxf;
}
struct Isl
{
int x, y;
void inp()
{
scanf("%d%d", &x, &y);
}
} I[MAXN];
int n, m;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
//Initialize
scanf("%d%d", &n, &m);
I[0].inp();
int src = 0, snk = 0;
Rep(i, 1, n)
{
I[i].inp();
if (I[i].x < I[src].x) src = i;
if (I[i].x > I[snk].x) snk = i;
}
G_ini();
while (m--)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u--;
v--;
AE(u, v, w);
AE(v, u, w);
}
//Solve
printf("%d\n", ISAP(src, snk, n));
}
return 0;
}
相关推荐
Android GNSS 模块分析(三)JNI 层
Android GNSS;定位服务;GPS;NMEA协议;LocationManagerService;导航;
hdu4280 Island Transport(毒瘤卡常最大流)
传送门 题意:n个岛屿有m个航道,每个航道能运送一定的人,问从最左端的岛屿到最右端的岛屿能运送的最大人流量 最大流裸题,但是这个N和M都是1e5的,dinic很容易被卡,如下图所示 这题10000ms,最后9999ms萌混过关(-ω- ) 过程就没什么好讲了,交就完事了,交个10发总有一发能A 逃走(o>▽<) #pragma GCC optimi...
将点云坐标转换成世界坐标的demo
将点云坐标转换成世界坐标的demo
Hdu4280 Island Transport(最大流卡时间)
题意: 给出一些点从最西边的点经过网络到最东边的点,问最大流量(无向图) 思路: 根据坐标找出最西边的点(源点)和最东边的点(汇点)。 建图也很直接, 无向图网络流,建边要正反向都要赋予流量值;如果有向图则正向流量值,反向为0。 卡时间。 Dinic用STL不行,都换成了数组。另外ISAP也行。 // 抛弃STL的Dinic , 7800ms #pragma comment(linker,"/ST...
【Unity】Unity 跨平台及编译过程
早期的Unity跨平台是基于MONO运行时的,又通过CLS实现了跨语言。其基本思路是将各个版本的语言统一编译成CIL语言,在不同平台运行时使用CLR进行解释。有点类似于Java的jdk和jvm。 CLS(Common Language System)通用语言规范:CLS是一种规范,类似于schema,当文件被指定遵守CLS规范后,在某语言中写出不符合通用规范的代码后将被警告或报错。 MONO是一个开源工程,其中包含C#编译器,CLR运行时,以及一组类库。该工程常见了一系列符合ECMA标准的.NET工具,MO
Minecraft Forge模组开发入门:从环境搭建到事件监听实战
事件驱动编程是一种基于事件和事件监听器的软件架构模式,其核心原理是通过事件总线实现组件间的松耦合通信。在游戏开发领域,这种模式能够有效处理玩家交互、状态变更等异步操作,具有高度的灵活性和可扩展性。Java作为面向对象编程语言,通过注解和反射机制为事件驱动提供了天然支持。Minecraft模组开发正是事件驱动编程的典型应用场景,开发者可以利用Forge框架提供的事件总线监听游戏内各种行为。本文以Minecraft Forge模组开发为例,详细讲解如何通过Gradle构建工具搭建开发环境,并实现自定义方块注册和
HDU 4280 Island Transport (最大流)
题意:给出n个点坐标,m条双向路线,每条路线给出一小时能运送的乘客数量,起点是最左边的点,终点是最右边的点,求一小时最大运客量。 题解:最大流 注意是双向路线,ISAP居然也卡,用了bfs初始化+栈优化才过。 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm>
实现基于 Python 和 xterm.js 的 Web 交互终端demo
前端 (HTML):使用 xterm.js 作为前端终端模拟器库。创建了一个 WebSocket 连接到 ws://localhost:8765,这与后端服务器的地址和端口匹配。监听 WebSocket 消息,并将数据写入终端。同时监听用户输入并发送到服务器。后端 (Python):使用 websockets 库来创建 WebSocket 服务器,监听端口 8765。使用 paramiko 库来创建 SSH 客户端并连接到指定的远程服务器。
HDU-4280 Island Transport (最大流 卡时间)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:给你n个岛屿的坐标,让你求从最西边到最东边的最大流 (题目保证只有一个点在最西边,只有一个点在最东边) 这个题比较卡时间 普通 dinic() 9375ms kuangbin模板 4992ms ISAP+bfs初始化+栈优化 3572ms #include <...
【以太网硬件十二】以太网端口自协商过程解密之二
简述以太网端口自协商的过程
R语言中5种正态性检验的方法
统计学中的t检验和方差分析等方法的应用条件是样本都来自正态总体或近似正态总体,只有符合这个条件,才能用这些方法来检验各样本所属的总体参数的差异显著性。文本向大家介绍在R语言中检验正态性的几种方法: 1、Kolmogorov-Smirnov检验 K-S检验检验单一样本是否来自某一特定分布。比如检验一组数据是否为正态分布。它的检验方法是以样本数据的累积频数分布与特定理论分布比较,若两者间的差距很小,则推论该样本取自某特定分布族。K-S检验的原假设和备择假设为: H0:样本所来自的总体分布服从
[HDU 4280]Island Transport[SAP]
题目链接:[HDU 4280]Island Transport[SAP] 题意分析: 最西边的岛屿给最东边的岛屿运输,问最大运输能力。 解题思路: 最西边源点,最东边汇点,建边就行。 个人感受: 这题之所以写题解是因为,我竟然把边开的和点一样多= =,然后交上去不是RE而是T。。。。改了各种板子我都质疑人生了= =,结果是边开小了!!!!!教训教训!!!! 具体代码如下: #inc
ShowDoc item_id 未授权SQL注入漏洞复现
2024年6月,ShowDoc官方发布新版本修复了一个SQL注入漏洞。鉴于该漏洞无前置条件,易于利用,且默认情况下可暴破用户token获取系统后台权限,建议所有使用该系统的企业尽快升级修复,以确保系统安全。
HDU 4280Island Transport(网络流之最大流)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4280 这个题是一个纯最大流模板题。。就是用来卡时间的。。还好我会isap算法。。但是坑爹的是一直WA,最后加了个输入优化就过了。。。(仅仅是把输入改了改而已。。)至今不知道为什么。。请好心的看到此博客的大神赐教。。 代码如下: #include #include #include #inc
复分析 阿尔福斯(Ahlfors) 第三版答案 英文
答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案
IEM模型,地表粗糙度模拟
利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟
hdu 4280 Island Transport(最大流)
hdu 4280 Island TransportDescription In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships. You ha
win10 跳过U8环境检测的IE WebControls.rar
win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。
HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the shi...
锂电池充电器电路设计 里面有电路
锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。
HDU-4280 Island Transport
题目链接:HDU-4280 Island Transport 题意 给出$n$个岛屿$m$条双向航道,岛屿以直角坐标系上的坐标形式给出,每条航道在单位时间有运输量上限,问单位时间内从$x$轴最左边的岛屿到最右边的岛屿最大的运输量。 思路 最大流问题,最左边的岛屿为源点,最右边的岛屿为汇点,按所给航道连边即可,由于是无向边,所以不需要再建容量为0的反向边。$n,m$很大,要用效...
HDU 4280 Island Transport(无向图最大流)
HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 比较裸的最大流题目,就是这是个无向图,并且比较卡时间。 思路: 是这样的,由于是无向图,所以addedge 的反边容量直接设为原始流量。然后还可以优化搜索的方向,bfs可以从t到s跑,dfs可以从s到t跑,这样快。 //#pragma GCC optim...
hdu 4280(最大流ISAP)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 模板题裸的最大流,n和m较大,时间卡的紧,要用堆栈加bfs优化 #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm...
hdu 4280 最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include...
hdu 4280 Island Transport 最大流dinic算法
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 给定平面上n个点的坐标,最左边的点为源点,最右边的点为汇点,给出m条边的容量求最大流。 思路: 才开始理解错了题意,For safety, no two routes are cross or overlap and no routes will pass an island except...
Island Transport HDU 4280 Dinic模板
Island Transport HDU 4280 Dinic模板 题目链接:https://cn.vjudge.net/contest/163017#problem/G 题目大意:给出N个点的坐标和M条有容量的边,找出从最西边的点到最东边的点的最大流量。 题目分析:抄的李东庆的模板,于是以后就打算用这个板子搞 Dinic 了。 题目分析:略。 #include &lt;bits/s...
hdu4280 最大流模板题:邻接表形式dinic
额这题卡了dinic的时间== 不过还是强行用dinic过了,把stl语句全部换了,包括vector也改成了邻接表 还是比较慢,8000+ms,很奇怪的是,我的邻接表中加了当前弧优化竟然慢了1s=== 至于sap,过两天再写吧== 1 #pragma comment(linker,"/STACK:16777216") 2 #include<stdio.h> 3...
2012天津赛区网络赛第三题--Island Transport(hdu4280)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:有N个岛屿之间有M双向条路,每条路每个小时最多能通过C个人,现在问一个小时内,最多能把多少个顾客从最西边的岛屿送至最东边的岛屿上。 思路:网络流,求最大流。建图:每条路连接的两个岛屿之间建立一条容量为C的双向边,取超级源点与汇点,源点与最西边的岛屿,汇点与最东边的岛
5163



被折叠的 条评论
为什么被折叠?



