G - Island Transport 网络流
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.
Input The first line contains one integer T (1<=T<=20), the number of test cases.
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.
Output
For each test case, output an integer in one line, the transport capacity.
Sample Input
2
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
Sample Output
9
6 题解:
题目大意: 就是有些岛,岛与岛之间有路,给你岛的坐标,保证最东边和最西边的岛只有一个,问你从最西边走到最东边的每一个小时可以走过的最多的人。 这个题目,很明显是网络流,原因呢,就是因为题目说每一个小时内可以走的最多的人,但是又没有告诉你速度,再画一下图,发现其实就是一次性可以走多少人。
就是一个最大流的裸题,但是这里有一点不同就是这个建图,这个是一个双向的,是一个有环无向图,所以呢,这个建图就是正着和反着的容量应该是一样的。
这个具体为什么我还要去研究一下,现在就线这么认为吧。 然后就跑一个最大流的模板就可以了。
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m, s, t;
void init(int n)
{
for (int i = ; i <= n; i++)G[i].clear();
e.clear();
}
void add(int u, int v, int c)
{
e.push_back(edge(u, v, c, ));
e.push_back(edge(v, u, c, ));
m = e.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -, sizeof(level));
queue<int>q;
level[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
if (u == t) return;
q.pop();
for (int v = ; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < )
{
level[now.v] = level[u] + ;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > )
{
now.f += d;//正向边流量加d
e[G[u][v] ^ ].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return ;
}
int Maxflow(int s, int t)
{
int flow = ;
for (;;)
{
BFS(s);
if (level[t] < )return flow;//残余网络中到达不了t,增广路不存在
memset(iter, , sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > )
{
flow += f;
}
}
return flow;
} int main()
{
int qw;
scanf("%d", &qw);
while(qw--)
{ int n, m;
scanf("%d%d", &n, &m);
init(n);
int mans = inf, mark = ;
int mana = -inf, mark1 = ;
for(int i=;i<=n;i++)
{
int x, y;
scanf("%d%d", &x, &y);
if(x<mans)
{
mans = x;
s = i;
}
if(x>mana)
{
mana = x;
t = i;
}
}
for(int i=;i<=m;i++)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
add(x, y, c);
}
int ans = Maxflow(s, t);
printf("%d\n", ans);
}
return ;
}
G - Island Transport 网络流的更多相关文章
- HDU 4280 Island Transport(网络流)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=4280">http://acm.hdu.edu.cn/showproblem.php ...
- G - Island Transport - hdu 4280(最大流)
题意:有N个岛屿,M条路线,每条路都连接两个岛屿,并且每条路都有一个最大承载人数,现在想知道从最西边的岛到最东面的岛最多能有多少人过去(最西面和最东面的岛屿只有一个). 分析:可以比较明显的看出来是一 ...
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- HDU 4280 Island Transport
Island Transport Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. Origi ...
- Island Transport
Island Transport http://acm.hdu.edu.cn/showproblem.php?pid=4280 Time Limit: 20000/10000 MS (Java/Oth ...
- 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(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU4280 Island Transport —— 最大流 ISAP算法
题目链接:https://vjudge.net/problem/HDU-4280 Island Transport Time Limit: 20000/10000 MS (Java/Others) ...
- Hdu 4280 Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
随机推荐
- jpa是什么,和hibernate 有什么关系
JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.JPA 的目标之一是制定一个可以由很多供应商实现的API,并且开发人员可以编码来实现该API,而不 ...
- redis集群搭建-3.0/4.0版本
1. Redis的安装 1.1. Redis的安装 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: 第 ...
- AJ学IOS(09)UI之UIScrollView代理触摸实现_图片缩放
AJ分享,必须精品 先看效果 代码 // // NYViewController.m // 05-放大缩小图片UIScrollView // // Created by apple on 15-3-2 ...
- JAVA—SQL注入
之前看到的一道java面试题,Statement与PreparedStatement的区别,什么是SQL注入,如何防止SQL注入 前面部分比较好回答 1.PreparedStatement支持动态设置 ...
- Zipper 杭电 1501
Given three strings, you are to determine whether the third string can be formed by combining the ch ...
- A - Chat Group Gym-101775A
题目连接:https://codeforces.com/gym/101775/problem/A 题解:就是累加组合数 但是直接由K累加到N肯定会TLE ,所以我们不妨判断不能组成group的情况,即 ...
- 关于go的init函数
亲测,如果加载一个包,如果一个包里的每个文件,均含有init函数,那么均会执行. 目前来看,init的执行顺序,是文件名称的自然排序进行执行的. 并且只是所加载包里的go文件的init函数执行,对于包 ...
- 小白必看,Python 各种下划线都是啥意思_、_xx、xx_、__xx、__xx__、_classname_
我们在定义一些变量或者方法的时候,常常会用到下划线,在 Python 中,下划线可是很有用处的哟,比如变量,有些是一个下划线开头的(_xx),有些是两个下划线开头的(__xx),有些是在名称的结尾添加 ...
- XSS Challenge(1)
XSS Challenges http://xss-quiz.int21h.jp/ Stage #1 注入alert(document.domain),先试一试输入后会返回什么: 返回在标签中,直接尝 ...
- Component Object Model (COM) 是什么?
本文主要介绍 COM 的基础知识,倾向于理论性的理解,面向初学者,浅尝辄止. 1. COM 是什么: COM 的英文全称是,Component Object Model,中文译为,组件对象模型.它官方 ...