HDU4280 Island Transport —— 最大流 ISAP算法
题目链接:https://vjudge.net/problem/HDU-4280
Island Transport
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9945 Accepted Submission(s): 3214
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.
5 7
3 3
3 0
3 1
0 0
4 5
1 3 3
2 3 4
2 4 3
1 5 6
4 5 3
1 4 4
3 4 2
6 7
-1 -1
0 1
0 2
1 0
1 1
2 3
1 2 1
2 3 6
4 5 5
5 6 3
1 4 6
2 5 5
3 6 4
6
题解:
最大流的裸题,不过对时间效率要求较高。所以就用了ISAP。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = 1e5+; struct Edge
{
int to, next, cap, flow;
}edge[MAXN<<];
int tot, head[MAXN]; int gap[MAXN], dep[MAXN], cur[MAXN]; void init()
{
tot = ;
memset(head, -, sizeof(head));
} void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ;
edge[tot].next = head[v]; head[v] = tot++;
} int Q[MAXN];
void BFS(int start, int end)
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
dep[end] = ;
gap[] = ;
int front = , rear = ;
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[MAXN];
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;
}
if((--gap[dep[u]])==) break;
gap[dep[u]=Min+]++;
if(u!=start) u = edge[S[--top]^].to;
}
return ans;
} int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n,&m);
int start, end, le = INF, ri = -INF;
for(int i = ; i<=n; i++)
{
int x, y;
scanf("%d%d", &x,&y);
if(x<le) { le = x; start = i; }
if(x>ri) { ri = x; end = i; }
} init();
for(int i = ; i<=m; i++)
{
int u, v, c;
scanf("%d%d%d", &u,&v,&c);
add(u, v, c);
add(v, u, c);
} cout<< sap(start, end, n) <<endl;
}
}
HDU4280 Island Transport —— 最大流 ISAP算法的更多相关文章
- 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 2017-02-15 17:10 44人阅读 评论(0) 收藏
Island Transport Problem Description In the vast waters far far away, there are many islands. People ...
- CCF(引水入城:60分):最大流+ISAP算法
引水入城 201703-5 这从题目分析来看很像最大流的问题,只需要增加一个超级源点和一个超级汇点就可以按照题意连边再跑最大流算法. 因为数据量太大了,肯定会超时.但是没有想到可行的解决方法. #in ...
- HDU4280 Island Transport
ISAP求最大流模板 #include<cstdio> #include<cstring> #include<algorithm> #include<iost ...
- 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 ...
- HDU-4280-Island Transport(网络流,最大流, ISAP)
链接: https://vjudge.net/problem/HDU-4280 题意: In the vast waters far far away, there are many islands. ...
- Hdu 4280 Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- P3376 【模板】网络最大流( Edmonds-krap、Dinic、ISAP 算法)
P3376 [模板]网络最大流( Edmonds-krap.Dinic.ISAP 算法) 题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入格式 第一行包含四个正整数N.M.S ...
随机推荐
- java.util.ResourceBundle 用法小介
java中读取配置文件的信息可以采用properties这个类,但是当遇到国际化问题的时候还是不好解决,因而还是最好使用 ResourceBundle这个类,其实ResourceBundle本质上和P ...
- ngrinder的安装
1.官网下载war包(ngrinder-controller),可以使用tomcat启动或者直接nohup java -XX:Permsize=200m -jar ngrinder-3.4.1.war ...
- poj 2891 模线性方程组求解
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8005 ...
- mysql workbench 导出建表语句
导出建表语句和数据 server—–>data export—–>选中表 注意,export to dump project folder,这种方式是每个表对应一个sql文件. 导出建 ...
- FastMM使用详解
FastMM使用详解 一.引言 FastMM 是适用于delphi的第三方内存管理器,在国外已经是大名鼎鼎,在国内也有许多人在使用或者希望使用,就连 Borland 也在delphi2007 ...
- T3187 队列练习3 codevs
http://codevs.cn/problem/3187/ 题目描述 Description 比起第一题,本题加了另外一个操作,访问队头元素(编号3,保证访问队头元素时或出队时队不为空),现在给出这 ...
- Flink的安装配置
一. Flink的下载 安装包下载地址:http://flink.apache.org/downloads.html ,选择对应Hadoop的Flink版本下载 [admin@node21 soft ...
- Free web scraping | Data extraction | Web Crawler | Octoparse, Free web scraping
Free web scraping | Data extraction | Web Crawler | Octoparse, Free web scraping 人才知了
- vSphere 6.5 新功能 (7) - 支持 512e 硬盘
2016-12-11 Newton 长期以来,机械硬盘在储存数据时,一直都是以 512 byte 大小的扇区(Sector)为单位分割进行读写.随着硬盘容量的不断提升,这种古老的分配标准已经越来越不合 ...
- CentOS 5.4 final下Systemtap的安装
CentOS 5.4 final下Systemtap的安装 时间:2015-02-11来源:linux网站 作者:zklth 一.Systemtap运行环境需求 (1)linux kernel ...