HDU 4280 ISAP+BFS 最大流 模板
Island Transport
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9473 Accepted Submission(s): 3069
You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.
Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.
最大流裸题 *可用模板
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
const int M = ;
const int INF = 0x3f3f3f3f;
struct Edge{
int to,next,cap,flow;
}edge[*M];
int tol;
int head[M];
int gap[M],dep[M],cur[M];
void init()
{
tol=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w){
int rw=;
edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=;
edge[tol].next=head[u];head[u]=tol++;
edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=;
edge[tol].next=head[v];head[v]=tol++;
}
int Q[M];
void bfs(int start,int end){
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=;
int front=,rear=;
dep[end]=;
Q[rear++]=end;
while(front !=rear){
int u=Q[front++];
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(dep[v]!=-) continue;
Q[rear++]=v;
dep[v]=dep[u]+;
gap[dep[v]]++;
}
}
}
int S[M];
int sap(int start,int end,int N){
bfs(start,end);
memcpy(cur,head,sizeof(head));
int top=;
int u=start;
int ans=;
while(dep[start]<N){
if(u==end){
int Min=INF;
int inser;
for(int i=;i<top;i++){
if(Min>edge[S[i]].cap-edge[S[i]].flow){
Min=edge[S[i]].cap-edge[S[i]].flow;
inser=i;
}
}
for(int i=;i<top;i++){
edge[S[i]].flow+=Min;
edge[S[i]^].flow-=Min;
}
ans+=Min;
top=inser;
u=edge[S[top]^].to;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-;i=edge[i].next){
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+==dep[u]){
flag=true;
cur[u]=i;
break;
}
}
if(flag){
S[top++] = cur[u];
u=v;
continue;
}
int Min=N;
for(int i=head[u];i!=-;i=edge[i].next){
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){
Min=dep[edge[i].to];
cur[u]=i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u]=Min+;
gap[dep[u]]++;
if(u!=start) u=edge[S[--top]^].to;
}
return ans;
}
int t;
int n,m;
int main()
{
scanf("%d",&t);
while(t--){
init();
scanf("%d %d",&n,&m);
int minx=;
int maxx=-;
int start,tail;
for(int i=;i<=n;i++){
int x,y;
scanf("%d %d",&x,&y);
if(x<minx){minx=x;start=i;}
else if(x>maxx){maxx=x;tail=i;}
}
for(int i=;i<=m;i++){
int qq,w,e;
scanf("%d %d %d",&qq,&w,&e);
addedge(qq,w,e);
addedge(w,qq,e);
}
printf("%d\n",sap(start,tail,n));
}
return ;
}
HDU 4280 ISAP+BFS 最大流 模板的更多相关文章
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- hdu - 3549 Flow Problem (最大流模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...
- Island Transport 【HDU - 4280】【最大流Dinic】
题目链接 可以说是真的把时间卡爆了,不断的修改了好多次之后才A了,一直T一直T,哭了…… 可以说是很练时间优化了,不断的改,不断的提交,最后竟然是改了Dinic中的BFS()中,我们一旦搜索到了T之后 ...
- hdu 4280 最大流 sap模板
给你岛的坐标求最西边到最东边的最大流 /* 最大流模板 sap */ #include<stdio.h> #include<string.h> #include<algo ...
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- poj-1459-最大流dinic+链式前向星-isap+bfs+stack
title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...
- HDU 4280 Island Transport(无向图最大流)
HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 比较裸的最大流题目,就是这是个无向图,并且比较卡时间. 思路: 是这样的,由于是 ...
- 【模板】 最大流模板(ISAP)
题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...
- HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解
题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...
随机推荐
- vue-cli 动态绑定图片失败
1.template 中引用图片,第一个为固定路径,第二个为动态绑定路径 eg: <img src="XXXXXX.png" alt=""> < ...
- Latex数学公式编写
小叙闲言 一直想用latex来编辑文档,但是没有需求,所以也没有去学习一下,但是最近由于要大量敲数学公式,有了latex数学公式的需求,所以来稍稍总结学习一下 1.在MathType中编写Latex数 ...
- WPF编程,窗口保持上次关闭时的大小与位置。
原文:WPF编程,窗口保持上次关闭时的大小与位置. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details/8 ...
- POJ 3349&&3274&&2151&&1840&&2002&&2503
(今天兴致大发学了Markdown,第一篇博客) 这次的主要都是hash的题目(当然这就意味这可以用map) hash的方式也有很多: 普通hash hash挂链 双hash以及自然溢出等 当然我还是 ...
- 使用Redis做分布式
一 为什么使用 Redis 在项目中使用 Redis,主要考虑两个角度:性能和并发.如果只是为了分布式锁这些其他功能,还有其他中间件 Zookpeer 等代替,并非一定要使用 Redis. 性能: 如 ...
- 真机调试傻瓜图文教程(Xcode6.4)
先准备好99刀,真机调试才带你玩. PS:万能宝十来块钱可以买个资格... Developer Apple上的设置 1.打开https://developer.apple.com/,点击Member ...
- 前端常见算法面试题之 - 二维数组中的查找[JavaScript解法]
--------------------- 作者:吴潇雄 来源:CSDN 原文:https://blog.csdn.net/weixin_43439741/article/details/835118 ...
- kaggle 欺诈信用卡预测——Smote+LR
from:https://zhuanlan.zhihu.com/p/30461746 本项目需解决的问题 本项目通过利用信用卡的历史交易数据,进行机器学习,构建信用卡反欺诈预测模型,提前发现客户信用卡 ...
- .net中操作Visual SourceSafe
最近整理一些资料,发现以前写的一段代码,提供对微软的版本管理软件visual sourcesafe的一些操作.以下简称vss. 想起以前写的时候,因为资料比较匮乏,只能边研究边测试,走了不少弯路. 由 ...
- About The Algorithm Simplification
For mode 1, you have to ergod all the data in the files. So the key point to solve this problem is t ...