ISAP求最大流模板

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=;
const int inf=1e9;
struct node {
int from,to,next;
int cap;
}edge[maxn*];
int tol;
int head[maxn];
int dep[maxn];
int gap[maxn];//gep[x]=y说明残量网络中dep[i]==x的个数为y
int n;//点的个数
void init()
{
tol=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int w)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].cap=w;
edge[tol].next=head[u];
head[u]=tol++;
}
void bfs (int st,int ed) {
memset (dep,-,sizeof(dep));
memset (gap,,sizeof(gap));
gap[]=;
queue<int> q;
dep[ed]=;
q.push(ed);
while (!q.empty()) {
int u=q.front();
q.pop();
for (int i=head[u];i!=-;i=edge[i].next) {
int v=edge[i].to;
if (dep[v]!=-) continue;
q.push(v);
dep[v]=dep[u]+;
gap[dep[v]]++;
}
}
}
int sap (int st,int ed) {
int res=;
bfs (st,ed);
int cur[maxn];
int s[maxn];
int top=;
memcpy (cur,head,sizeof(head));
int u=st;
int i;
while (dep[st]<n) {
if (u==ed) {
int tmp=inf;
int inser;
for (i=;i<top;i++)
if (tmp>edge[s[i]].cap) {
tmp=edge[s[i]].cap;
inser=i;
}
for (i=;i<top;i++) {
edge[s[i]].cap-=tmp;
edge[s[i]^].cap+=tmp;
}
res+=tmp;
top=inser;
u=edge[s[top]].from;
}
if (u!=ed&&gap[dep[u]-]==) break;
for (i=cur[u];i!=-;i=edge[i].next)
if (edge[i].cap!=&&dep[u]==dep[edge[i].to]+) break;
if (i!=-) {
cur[u]=i;
s[top++]=i;
u=edge[i].to;
}
else {
int min=n;
for (i=head[u];i!=-;i=edge[i].next) {
if (edge[i].cap==) continue;
if (min>dep[edge[i].to]) {
min=dep[edge[i].to];
cur[u]=i;
}
}
gap[dep[u]]--;
dep[u]=min+;
gap[dep[u]]++;
if (u!=st) u=edge[s[--top]].from;
}
}
return res;
}
int main() {
int start,end;
int m;
int u,v,z;
int T;
scanf("%d",&T);
while(T--) {
init();
scanf("%d%d",&n,&m);
int Min=inf;
int Max=-inf;
int x,y;
for(int i=;i<=n;i++) {
scanf("%d%d",&x,&y);
if(Min>x) {
Min=x;
start=i;
}
if(Max<x) {
Max=x;
end=i;
}
}
while(m--) {
scanf("%d%d%d",&u,&v,&z);
addedge(u,v,z);
addedge(v,u,z);
}
//n一定是点的总数,这是使用SAP模板需要注意的
int ans=sap(start,end);
printf("%d\n",ans);
}
return ;
}

HDU4280 Island Transport的更多相关文章

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

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

  2. Hdu4280 Island Transport 2017-02-15 17:10 44人阅读 评论(0) 收藏

    Island Transport Problem Description In the vast waters far far away, there are many islands. People ...

  3. hdu4280 Island Transport 最大流

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

  4. HDU4280:Island Transport(最大流)

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

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

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

  6. Island Transport

    Island Transport http://acm.hdu.edu.cn/showproblem.php?pid=4280 Time Limit: 20000/10000 MS (Java/Oth ...

  7. HDU 4280 Island Transport

    Island Transport Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. Origi ...

  8. Hdu 4280 Island Transport(最大流)

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

  9. HDU 4280 Island Transport(dinic+当前弧优化)

    Island Transport Description In the vast waters far far away, there are many islands. People are liv ...

随机推荐

  1. Java的多态-进阶

    Java的多态——进阶 总括 Parent p = new Child(); 反之,Child() c = new Parent(); 会报错. 当使用多态方式调用方法时,首先检查父类中是否有该方法. ...

  2. 用python脚本测试接口

    自己写一个脚本,统计调用200次接口的请求时长. # -*- coding=utf-8 -*-import osimport requestsimport time url = "http: ...

  3. layui-form下隐藏元素的验证问题

    1.情景: 设置为display:none的必填字段,在点击提交(form 包含 class="layui-form":button包含属性 lay-submit)的时候,依旧验证 ...

  4. Map.Entry 类使用简介(转)

    Map.Entry 类使用简介(转)   你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦?使用Map.Entry类,你可以得到在同一时间得到所有的信息.标准的Map访问方法如下: Se ...

  5. MP4转mp3

    python实现: 依赖: glob,pydub "Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not wo ...

  6. GitHub网页版基本操作

    创建存储库 登录GitHub进入主页,点击头像左边的加号,创建存储库 填写存储库名称.描述,根据需求设置其他选项.点击“Create repository”按钮 创建分支 打开之前创建好的存储库,点击 ...

  7. DataGridView 更改Header样式

    '必须先设置 EnableHeadersVisualStyles 属性 才能设置Header颜色dgv.EnableHeadersVisualStyles = Falsedgv.ColumnHeade ...

  8. c语言getipaddrtable获得ip地址与sendArp获得mac地址以及一些字节序问题记录

    https://docs.microsoft.com/zh-cn/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable msdn,有很多c的源码还 ...

  9. DP_1d1d诗人小G

    显然:f[i]=min{f[j]+(s[i]-s[j]+i-j-1-l)^p} 此题可以基于决策单调优化 证明,反正我现在不打算学 实际上就是双向队列 不停弹出队头的元素,直到当前位置在队头元素最优的 ...

  10. Codeforces Round #598 (Div. 3) D. Binary String Minimizing

    You are given a binary string of length nn (i. e. a string consisting of nn characters '0' and '1'). ...