In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships. 
  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 网络流的更多相关文章

  1. HDU 4280 Island Transport(网络流)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=4280">http://acm.hdu.edu.cn/showproblem.php ...

  2. G - Island Transport - hdu 4280(最大流)

    题意:有N个岛屿,M条路线,每条路都连接两个岛屿,并且每条路都有一个最大承载人数,现在想知道从最西边的岛到最东面的岛最多能有多少人过去(最西面和最东面的岛屿只有一个). 分析:可以比较明显的看出来是一 ...

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

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

  4. HDU 4280 Island Transport

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

  5. Island Transport

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

  6. 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 ...

  7. HDU4280:Island Transport(最大流)

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

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

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

  9. Hdu 4280 Island Transport(最大流)

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

随机推荐

  1. AJ学IOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据

    AJ分享,必须精品 一:效果 没有图文混排,也没有复杂的UI,仅仅是简单的显示出微博数据,主要介绍AFNetworking和SDWebImage的简单用法 二:加载数据AFNetworking AFN ...

  2. threejs创建地球

    上个月底,在朋友圈看到一个号称“这可能是地球上最美的h5”的分享,点进入后发现这个h5还很别致,思考了一会,决定要不高仿一个? 到今天为止,高仿基本完成, 线上地址 github地址 除了手机端的me ...

  3. stand up meeting 12/21/2015

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  完成PDF UI主页面的页面切换功能,待完善    4  完善 ...

  4. F - Robot Motion 栈加BFS

    A robot has been programmed to follow the instructions in its path. Instructions for the next direct ...

  5. lua使用笔记2:Linux 中安装php的lua扩展

    安装lua扩展的前提是lua已经安装好,如果没有安装,参照 1.http://pecl.php.net/package/lua 下载lua扩展 或者Linux下直接输入 wget http://pec ...

  6. OkHttp 优雅封装 HttpUtils 之 上传下载解密

    曾经在代码里放荡不羁,如今在博文中日夜兼行,只为今天与你分享成果.如果觉得本文有用,记得关注我,我将带给你更多. 还没看过第一篇文章的欢迎移步:OkHttp 优雅封装 HttpUtils 之气海雪山初 ...

  7. linux之cat 操作

    1.查看或创建 cat 1.txt #如果目录有这个文件则会打开查看,没有则会创建 2.压缩空白 cat 1.txt 我是第一行 我是第二 行 cat -bs 1.txt # 变成 cat 1.txt ...

  8. python 工具链 多版本管理工具 pyenv

    理解Shims pyenv会在系统的PATH最前面插入一个shims目录: $(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin 通过一个rehashing ...

  9. 算法笔记刷题2(codeup 1928)

    又磕了一晚上,多点测试真的很烦 ,完全不知道错哪里,后来发现是我变量名命名不规范导致自己晕了填错了,其实思路还是对的 我觉得书上的做法也还行,但我不太喜欢用二维数组,所以拿以前写的算天数的程序改装了一 ...

  10. 点击表头取下标&js时间转时间戳

    1.Date.parse(new Date("2017-7-31")); 2.$("th").eq(this.cellIndex);  // 3.end($ar ...