传送门: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. django-cookieless 0.7 : Python Package Index

    django-cookieless 0.7 : Python Package Index django-cookieless 0.7 Download django-cookieless-0.7.ta ...

  2. hdu1003 最大连续子序和

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  3. linux环境: shell初始化文件, for TCSH, CSH

    TCSHELL, CSHELL 配置文件 全局配置文件 /etc/csh.cshrc个人配置文件 ~/.cshrc或~/.tcshrc 参考: 1.配置你的csh/tcsh,  https://wik ...

  4. html,JavaScript调用winfrom方法

    ---恢复内容开始--- 目的: 在动画上面添加点击事件,通过JavaScript调用winfrom方法 1.创建一个页面 using System; using System.Collections ...

  5. jQuery中对 input 控件的操作

    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关 1.获取值 jquery取radio单选按钮的值 $(" ...

  6. 如何在SourceInsight中选中匹配的大括号中的内容

    如何在SourceInsight中选中匹配的大括号中的内容 要分析的代码很长,多个for,if等分析嵌套在一起,代码有点乱,找到了这个分支的头,却不知道尾在哪,找到了尾却不知道哪是开头,在网上找了一下 ...

  7. 为什么java源文件中只允许一个public类存在

    1.提出问题 为什么java源文件中只允许一个public类存在? 2.分析问题 问题涉及到的条件:源文件的名字    public类     main方法 一般我们在编写一个源文件的时候: 一个pu ...

  8. Hive HA使用说明

    hive让大数据飞了起来,不再需要专人写MR.平常我们都可以用基于thrift的任意语言来调用hive. 不过爱恨各半,hive的thrift不稳定也是出了名的.很容易就出问题,让人无计可施.唯一的办 ...

  9. 禁用win7自己主动配置ipv4地址

    现象 一台新电脑,连了网线,没有dhcp,须要手动配置Ip. 配置了一个Ip后,发现ping网关不通. ipconfig 发现有2 个IP:  自己主动配置 IPv4 地址  . . . . . . ...

  10. 【源代码】LinkedHashMap源代码剖析

    注:下面源代码基于jdk1.7.0_11 之前的两篇文章通过源代码分析了两种常见的Map集合,HashMap和Hashtable.本文将继续介绍还有一种Map集合--LinkedHashMap. 顾名 ...