HDU4280(KB11-G 最大流)

Island Transport

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 9151    Accepted Submission(s): 2958

Problem Description

  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

Source

2012 ACM/ICPC Asia Regional Tianjin Online

  1 //2017-08-24
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <vector>
  8 #pragma comment(linker, "/STACK:1024000000,1024000000")
  9
 10 using namespace std;
 11
 12 const int N = 100010;
 13 const int INF = 0x3f3f3f3f;
 14 int head[N], tot;
 15 struct Edge{
 16     int next, to, w;
 17 }edge[N<<4];
 18
 19 void add_edge(int u, int v, int w){
 20     edge[tot].w = w;
 21     edge[tot].to = v;
 22     edge[tot].next = head[u];
 23     head[u] = tot++;
 24 }
 25
 26 struct Dinic{
 27     int level[N], S, T;
 28     void init(int _S, int _T){
 29         S = _S;
 30         T = _T;
 31         tot = 0;
 32         memset(head, -1, sizeof(head));
 33     }
 34     bool bfs(){
 35         queue<int> que;
 36         memset(level, -1, sizeof(level));
 37         level[S] = 0;
 38         que.push(S);
 39         while(!que.empty()){
 40             int u = que.front();
 41             que.pop();
 42             for(int i = head[u]; i != -1; i = edge[i].next){
 43                 int v = edge[i].to;
 44                 int w = edge[i].w;
 45                 if(level[v] == -1 && w > 0){
 46                     level[v] = level[u]+1;
 47                     que.push(v);
 48                 }
 49             }
 50         }
 51         return level[T] != -1;
 52     }
 53     int dfs(int u, int flow){
 54         if(u == T)return flow;
 55         int ans = 0, fw;
 56         for(int i = head[u]; i != -1; i = edge[i].next){
 57             int v = edge[i].to, w = edge[i].w;
 58             if(!w || level[v] != level[u]+1)
 59                   continue;
 60             fw = dfs(v, min(flow-ans, w));
 61             ans += fw;
 62             edge[i].w -= fw;
 63             edge[i^1].w += fw;
 64             if(ans == flow)return ans;
 65         }
 66         if(ans == 0)level[u] = -2;
 67         return ans;
 68     }
 69     int maxflow(){
 70         int flow = 0, tmp;
 71         while(bfs())
 72               while((tmp = dfs(S, INF)) > 0)
 73                   flow += tmp;
 74         return flow;
 75     }
 76 }dinic;
 77
 78 int main()
 79 {
 80     //std::ios::sync_with_stdio(false);
 81     //freopen("inputG.txt", "r", stdin);
 82     int T, n, m;
 83     scanf("%d", &T);
 84     while(T--){
 85         scanf("%d%d", &n, &m);
 86         int x, y, s = 1, t = 1, mininum = INF, maxinum = -INF;
 87         for(int i = 1; i <= n; i++){
 88             scanf("%d%d", &x, &y);
 89             if(x < mininum){
 90                 mininum = x;
 91                 s = i;
 92             }
 93             if(x > maxinum){
 94                 maxinum = x;
 95                 t = i;
 96             }
 97         }
 98         dinic.init(s, t);
 99         int u, v, w;
100         for(int i = 0; i < m; i++){
101             scanf("%d%d%d", &u, &v, &w);
102             add_edge(u, v, w);
103             add_edge(v, u, w);
104         }
105         printf("%d\n", dinic.maxflow());
106     }
107     return 0;
108 }
时间: 2024-10-28 10:44:01

HDU4280(KB11-G 最大流)的相关文章

hdu4280 Island Transport(最大流Dinic数组模拟邻接连边)

Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 10716    Accepted Submission(s): 3430 Problem Description In the vast waters far far away, there are many islands. People are l

最大流学习笔记(1)

1 流网络.流网络G=(V,E)是一个有向图,每条边$(u,v)\in E$有一个非负容量值$c(u,v)\geq 0$.如果$(u,v)\notin E,c(u,v)=0$.另外有一个源节点s和汇点t. 2 流.G中的流是一个实值函数$f:V\times V\rightarrow R$,满足: (1)容量限制:对所有的$u,v\in V$,$0 \leq f(u,v)\leq c(u,v)$ (2)流量守恒:对所有的$u\in V-\{s,t\}$,满足$\sum_{v\in V}f(v,u)

关于最大流的EdmondsKarp算法详解

最近大三学生让我去讲课,我就恶补了最大流算法,笔者认为最重要的是让学弟学妹们入门,知道算法怎么来的?为什么是这样?理解的话提出自己的改进,然后再看看Dinic.SAP和ISAP算法….. 一.概念引入 首先要先清楚最大流的含义,就是说从源点到经过的所有路径的最终到达汇点的所有流量和. 流网络G=(V,E)是一个有向图,其中每条边(u,v)∈E均有一个非负容量c(u,v)>=0.如果(u,v)不属于E,则假定c(u,v)=0.流网络中有两个特别的顶点:源点s和汇点t.下图展示了一个流网络的实例(其

Ford-Fulkerson 最大流算法

流网络(Flow Networks)指的是一个有向图 G = (V, E),其中每条边 (u, v) ∈ E 均有一非负容量 c(u, v) ≥ 0.如果 (u, v) ∉ E 则可以规定 c(u, v) = 0.流网络中有两个特殊的顶点:源点 s (source)和汇点 t(sink).为方便起见,假定每个顶点均处于从源点到汇点的某条路径上,就是说,对每个顶点 v ∈ E,存在一条路径 s --> v --> t.因此,图 G 为连通图,且 |E| ≥ |V| - 1. 下图展示了一个流网络

最大流学习笔记(2)

1 基本的Ford-Fulkerson方法.该方法的思想就是每次找到一个增广路$p$,然后将增广路 $p$对应的流加到之前的流上得到新的流,一直这样直到找不到增广路,这时候找到的流就是最大流. 算法的伪代码如下 假设容量是整数,最大流为$f^{*}$,那么while循环最多执行$|f^{*}|$次,因为每次至少使得流量增加1,每次找增光路的代价是$O(E)$,所以总的复杂度是$O(E|f^{*}|)$ 2 Edmonds-Karp算法.Edmonds-Karp算法是对Ford-Fulkerson

Java8 新特性 Stream() 创建流

通过Controllere类的Stream()和parallelStream()创建流 //通过集合创建流 @Test public void test1() { String arr[] = new String[]{"a", "b", "c"}; //把数组转换成集合 List<String> list = Arrays.asList(arr); //生成stream流(串行流) Stream<String> stre

[转] 网络流算法--Ford-Fulkerson方法及其多种实现

网络流 转载自:http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 在上一章中我们讨论的主题是图中顶点之间的最短路径,例如公路地图上两地点之间的最短路径,所以我们将公路地图抽象为有向带权图.本章我们将对基于有向带权图的模型做进一步扩展. 很多系统中涉及流量问题,例如公路系统中车流量,网络中的数据信息流,供油管道的油流量等.我们可以将有向图进一步理解为“流网络”(flow network),并利用这样的抽象模型求解有关流量

hiho一下 第115周:网络流一?Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)

来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快.   hiho一下 第115周:网络流一•Ford-Fulkerson算法 原题网址:http://hihocoder.com/contest/hiho115/problem/1 网络流一·Ford-Fulkerson算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和

hiho一下,第115周,FF,EK,DINIC

题目1 : 网络流一·Ford-Fulkerson算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho住在P市,P市是一个很大很大的城市,所以也面临着一个大城市都会遇到的问题:交通拥挤. 小Ho:每到周末回家感觉堵车都是一种煎熬啊. 小Hi:平时交通也还好,只是一到上下班的高峰期就会比较拥挤. 小Ho:要是能够限制一下车的数量就好了,不知道有没有办法可以知道交通系统的最大承受车流量,这样就可以限制到一个可以一直很顺畅的数量了. 小Hi:理论上是有算法

网络流基础

1.流网络G=(V,E)是一个有向图,其中每条边(u,v)∈E均有一个非负容量 c(u,v)>=0.如果(u,v)不属于E,则假定c(u,v)=0.流网络中有两个特别的顶点:源点s和汇点t.下图展示了一个流网络的实例 (其中斜线左边的数字表示实际边上的流,右边的数字表示边的最大容量): 对一个流网络G=(V,E),其容量函数为c,源点和汇点分别为s和t.G的流f满足下列三个性质:      容量限制:对所有的u,v∈V,要求f(u,v)<=c(u,v).      反对称性:对所有的u,v∈V