HDU4280 Island Transport
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的更多相关文章
- HDU4280 Island Transport —— 最大流 ISAP算法
题目链接:https://vjudge.net/problem/HDU-4280 Island Transport Time Limit: 20000/10000 MS (Java/Others) ...
- 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 ...
- hdu4280 Island Transport 最大流
In the vast waters far far away, there are many islands. People are living on the islands, and all t ...
- HDU4280:Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- Island Transport
Island Transport http://acm.hdu.edu.cn/showproblem.php?pid=4280 Time Limit: 20000/10000 MS (Java/Oth ...
- HDU 4280 Island Transport
Island Transport Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. Origi ...
- Hdu 4280 Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 4280 Island Transport(dinic+当前弧优化)
Island Transport Description In the vast waters far far away, there are many islands. People are liv ...
随机推荐
- 深浅拷贝_python
一.浅拷贝 拷贝第一层的东西,如其他列表修改他们共同的第二层(或更深),他管不了,只能跟着变. 用处:很少用,用不同账号关联共享: import copyhusband= ['chen','123', ...
- PHP SDK+Oss 上传文件流
// Endpoint以杭州为例,其它Region请按实际情况填写. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // 云 ...
- Pycharm调试django项目时发现断点失效
解决方法: 第一步: 第二步: 点击 Edit Configuration 第三步 : 点击 + 选择python 填写相关参数信息 或者 点击ok 完成配置 重启 IDE 注意 重启IDE ...
- php一些实用的自制方法
时间戳转多久之前 function mdate($time = NULL) { $text = ''; $time = $time === NULL || $time > time() ? ti ...
- bootstrap环境
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 【网易官方】极客战记(codecombat)攻略-地牢-囚犯
关卡连接: https://codecombat.163.com/play/level/the-prisoner 解放囚犯,你会得到盟友. 简介 敬请期待! 默认代码 # 释放囚犯,击败守卫并夺取宝石 ...
- bugku 隐写2
首先打开链接发现是一张图片 然后分析一下图片 发现这个图片实际是一个压缩包 然后更改一下后缀名 改成zip 然后解压之后会发现另一个压缩包和一张图片压缩包需要密码然后这个图片是一个提示 然后分析 (我 ...
- Linux实现树莓派3B的国密SM9算法交叉编译——(二)miracl库的测试与静态库的生成
先参考这篇文章 Linux实现树莓派3B的国密SM9算法交叉编译——(一)环境部署.简单测试与eclipse工程项目测试 部署好环境,并简单测试交叉编译环境是否安装成功,最后实现在Eclipse上进行 ...
- nginx配置指令auth_basic、auth_basic_user_file及相关知识
参考链接 https://www.jianshu.com/p/1c0691c9ad3c auth_basic_user_file 所需要的密码,可以通过如下方法生成 1)安装htpasswd (yum ...
- knn 算法 k个相近邻居
# 一个最基本的例子 #样本数据的封装 feature = [[170,70,42],[166,56,39],[188,90,44],[165,88,40],[170,66,40],[176,80,4 ...