传送门:Island Transport

题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿。

分析:无向图正反都加弧,权值一样,这题点多,使用SAP优势大,点少时dinic好些。

dinic:8314ms

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 100010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();
int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,vs,vt,tot;
int pre[N],q[N],cur[N],h[N];
struct edge
{
int v,w,next;
edge(){}
edge(int v,int w,int next):v(v),w(w),next(next){}
}e[N<<];
void addedge(int u,int v,int w)
{
e[tot]=edge(v,w,pre[u]);
pre[u]=tot++;
e[tot]=edge(u,w,pre[v]);
pre[v]=tot++;
}
void init()
{
memset(pre,-,sizeof(pre));
tot=;
}
/*******************dinic************************/
int bfs()
{
int head=,tail=;
memset(h,-,sizeof(h));
q[]=vs;h[vs]=;
while(head!=tail)
{
int u=q[head++];
for(int i=pre[u];~i;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(w&&h[v]==-)
{
h[v]=h[u]+;
q[tail++]=v;
}
}
}
return h[vt]!=-;
}
int dfs(int u,int flow)
{
if(u==vt)return flow;
int used=;
for(int i=cur[u];~i;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(h[v]==h[u]+)
{
w=dfs(v,min(flow-used,w));
e[i].w-=w;e[i^].w+=w;
if(e[i].w)cur[u]=i;
used+=w;
if(used==flow)return flow;
}
}
if(!used)h[u]=-;
return used;
}
int dinic()
{
int res=;
while(bfs())
{
for(int i=;i<=n;i++)cur[i]=pre[i];
res+=dfs(vs,inf);
}
return res;
}
/********************dinic***********************/
void build()
{
int u,v,w;
int mx=-inf,mn=inf;
n=read();m=read();
for(int i=;i<=n;i++)
{
u=read();v=read();
if(u<mn)mn=u,vs=i;
if(u>mx)mx=u,vt=i;
}
for(int i=;i<=m;i++)
{
u=read();v=read();w=read();
addedge(u,v,w);
}
}
int main()
{
int T;
T=read();
while(T--)
{
init();
build();
printf("%d\n",dinic());
}
}

SAP:2917ms

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 100010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();
int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,vs,vt,tot,NV;
int head[N],gap[N],level[N],q[N];
struct edge
{
int v,w,next;
edge(){}
edge(int v,int w,int next):v(v),w(w),next(next){}
}e[N<<];
void addedge(int u,int v,int w)
{
e[tot]=edge(v,w,head[u]);
head[u]=tot++;
e[tot]=edge(u,w,head[v]);
head[v]=tot++;
}
void init()
{
memset(head,-,sizeof(head));
tot=;
}
/***************************SAP***********************/
void bfs(int vt)
{
memset(level,-,sizeof(level));
memset(gap,,sizeof(gap));
level[vt]=;
gap[level[vt]]++;
queue<int>que;
que.push(vt);
while(!que.empty()) {
int u=que.front();
que.pop();
for(int i=head[u]; i!=-; i=e[i].next) {
int v=e[i].v;
if(level[v]!=-)continue;
level[v]=level[u]+;
gap[level[v]]++;
que.push(v); }
}
}
int pre[N];
int cur[N];
int SAP()
{
bfs(vt);
memset(pre,-,sizeof(pre));
memcpy(cur,head,sizeof(head));
int u=pre[vs]=vs,flow=,aug=inf;
gap[]=NV;
while(level[vs]<NV) {
bool flag=false;
for(int &i=cur[u]; i!=-; i=e[i].next) {
int v=e[i].v;
if(e[i].w&&level[u]==level[v]+) {
flag=true;
pre[v]=u;
u=v;
aug=min(aug,e[i].w);
if(v==vt) {
flow+=aug;
for(u=pre[v]; v!=vs; v=u,u=pre[u]) {
e[cur[u]].w-=aug;
e[cur[u]^].w+=aug;
}
aug=inf;
}
break;
}
}
if(flag)continue;
int minlevel=NV;
for(int i=head[u]; i!=-; i=e[i].next) {
int v=e[i].v;
if(e[i].w&&level[v]<minlevel) {
minlevel=level[v];
cur[u]=i;
}
}
if(--gap[level[u]]==)break;
level[u]=minlevel+;
gap[level[u]]++;
u=pre[u];
}
return flow;
}
/**************************SAP**********************/
void build()
{
int u,v,w;
int mx=-inf,mn=inf;
n=read();m=read();
NV=n+;
for(int i=;i<=n;i++)
{
u=read();v=read();
if(u<mn)mn=u,vs=i;
if(u>mx)mx=u,vt=i;
}
for(int i=;i<=m;i++)
{
u=read();v=read();w=read();
addedge(u,v,w);
}
}
int main()
{
int T;
T=read();
while(T--)
{
init();
build();
printf("%d\n",SAP());
}
}

hdu4280(最大流)的更多相关文章

  1. hdu4280 最大流DINIC

    题意:       x最小的到x最大的点同一时间的最大运输量. 思路:       裸的最大流,不解释,注意一点,记得加上防爆栈. #pragma comment(linker, "/STA ...

  2. HDU4280 Island Transport —— 最大流 ISAP算法

    题目链接:https://vjudge.net/problem/HDU-4280 Island Transport Time Limit: 20000/10000 MS (Java/Others)   ...

  3. HDU4280:Island Transport(最大流)

    Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. hdu4280 Island Transport 最大流

    In the vast waters far far away, there are many islands. People are living on the islands, and all t ...

  5. HDU4280(KB11-G 最大流)

    Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU 4280 Island Transport(网络流,最大流)

    HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...

  7. 使用C#处理基于比特流的数据

    使用C#处理基于比特流的数据 0x00 起因 最近需要处理一些基于比特流的数据,计算机处理数据一般都是以byte(8bit)为单位的,使用BinaryReader读取的数据也是如此,即使读取bool型 ...

  8. HTML 事件(三) 事件流与事件委托

    本篇主要介绍HTML DOM中的事件流和事件委托. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4 ...

  9. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

随机推荐

  1. C++ 中使用boost::property_tree读取解析ini文件

    boost 官网 http://www.boost.org/ 下载页面 http://sourceforge.net/projects/boost/files/boost/1.53.0/ 我下载的是  ...

  2. ASP.NET - URL中参数加密解密操作

    效果: 代码: using System; using System.Text; using System.IO; using System.Security.Cryptography; public ...

  3. linux shell种类

    1.shell 种类 目前我们的 Linux (以 CentOS 5.x 为例) 有多少我们可以使用的 shells 呢? 你可以检查一下 /etc/shells 这个文件,至少就有底下这几个可以用的 ...

  4. ArcGIS 10.3 for Desktop新特性介绍

    ArcGIS 10.3是一个完整公布的ArcGIS平台,它包含新的产品(ArcGIS Pro),针对10.2版本号产品进行了功能增强和稳定性的改进. ArcGIS 10.3 for Server新特性 ...

  5. 批处理运行python

    @echo off cd C:\test start python test.py start python test2.py exit

  6. C#用链式方法

    C#用链式方法表达循环嵌套   情节故事得有情节,不喜欢情节的朋友可看第1版代码,然后直接跳至“三.想要链式写法” 一.起缘 故事缘于一位朋友的一道题: 朋友四人玩LOL游戏.第一局,分别选择位置:中 ...

  7. GetCursorPos/WindowFromPoint/SendMessage

    GetCursorPos/WindowFromPoint/SendMessage (用API函数向Edit框发送字符) GetCursorPos(mPoint); DTWND:=WindowFromP ...

  8. asp.net web api帮助文档的说明

    为asp.net的mvc web api填写自己的帮助文档 1. 加入Help的area(能够通过命令行或其它方式加入) 命令行:Install-Package Microsoft.AspNet.We ...

  9. C++ Primer 学习笔记_60_重载操作符与转换 --赋值、下标、成员訪问操作符

    重载操作符与转换 --赋值.下标.成员訪问操作符 一.赋值操作符 类赋值操作符接受类类型形參,通常该形參是对类类型的const引用,但也能够是类类型或对类类型的非const引用.假设未定义这个操作符, ...

  10. Spring中的FactoryBean

    从SessionFactory说起: 在使用SSH集成开发的时候,我们有时候会在applicationContext.xml中配置Hibernate的信息,以下是配置SessionFactory的一段 ...