Island Transport

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


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


6

该题是最大流裸体,数据范围很大,可以用dinic但要注意数组要开的够大,且必须要当前弧优化,且最好不用STL(别用queue),方能把时间卡进去(反正我交了一次9400ms左右..)

因为是双向边, 相当于无向图,且题目保证无重边,可以不用每条边再加一个cap为0的残边,

不知把BFS写进主函数会不会节省些时间

 #include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#define MIN(x,y) ((x)>(y))?(y):(x)
#define MAX(x,y) ((x)>(y))?(x):(y) using namespace std; const int inf = 0x3f3f3f3f;
const int ninf = 0xc0c0c0c0; //无穷小
const int vspot = ;
const int espot = ; struct Edges{
int to, next, cap;
}edges[vspot]; int V, E, cnt;
int S, T, ans;
int linjie[vspot], dist[vspot], cur[vspot];
int Q[espot]; void addEdges( int from, int to, int cap )
{
edges[cnt].to = to;
edges[cnt].cap = cap;
edges[cnt].next = linjie[from];
linjie[from] = cnt++; edges[cnt].to = from;
edges[cnt].cap = cap;
edges[cnt].next = linjie[to];
linjie[to] = cnt++;        //直接加两条边就行
} int BFS()
{
memset( dist, -, sizeof(dist) );
int run, head = , rear = ; Q[head] = S;
dist[S] = ; while(head<rear)
{
run = Q[head++];
for( int i = linjie[run]; i+; i = edges[i].next )
{
int v = edges[i].to, flow = edges[i].cap;
if( dist[v] < && flow > )
{
dist[v] = dist[run] + ;
Q[rear++] = v;
}
}
} if( dist[T] > )
return ;
else
return ;
} int find( int s, int low )
{
int ff = ;
if( s == T )
return low;
for( int& i = cur[s]; i+; i = edges[i].next ) //注意int& i = cur[s] 当前弧优化
{
int v = edges[i].to, cap = edges[i].cap;
if( cap >
&& dist[v] == dist[s] +
&& (ff = find(v,MIN(cap,low))) )
{
edges[i].cap -= ff;
edges[i^].cap += ff;
return ff;
}
}
return ;
} void dinic()
{
ans = ;
int tans;
while(BFS())
{
for( int i = ; i <= V; i++ ) //当前弧优化
cur[i] = linjie[i];
while( tans = find(S,inf) )
ans += tans;
}
} int main()
{
int N;
cin >> N;
while( N-- )
{
scanf( "%d %d", &V, &E );
cnt = ;
memset( linjie, -, sizeof(linjie) ); int x, y, val, x2 = ninf, x1 = inf;
for( int i = ; i <= V; i++ )
{
scanf( "%d %d", &x, &y );
if( x < x1 )
{
x1 = x;
S = i;
} if( x > x2 )
{
x2 = x;
T = i;
}
} for( int i = ; i <= E; i++ )
{
scanf( "%d %d %d", &x, &y, &val );
addEdges(x,y,val);
}
/////////////////////////////////////////////////////////////////
dinic();
cout << ans << endl;
} return ;
}

HDU 4280 Island Transport(dinic+当前弧优化)的更多相关文章

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

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

  2. HDU 4280 Island Transport

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

  3. HDU 4280 Island Transport(无向图最大流)

    HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 比较裸的最大流题目,就是这是个无向图,并且比较卡时间. 思路: 是这样的,由于是 ...

  4. Hdu 4280 Island Transport(最大流)

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

  5. HDU 4280 Island Transport(网络流)

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

  6. HDU 4280 Island Transport(HLPP板子)题解

    题意: 求最大流 思路: \(1e5\)条边,偷了一个超长的\(HLPP\)板子.复杂度\(n^2 \sqrt{m}\).但通常在随机情况下并没有isap快. 板子: template<clas ...

  7. ARC085E(最小割规划【最大流】,Dinic当前弧优化)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...

  8. 解题报告:hdu 3572 Task Schedule(当前弧优化Dinic算法)

    Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...

  9. Dinic当前弧优化 模板及教程

    在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...

随机推荐

  1. 19-11-08-Night

    再咕咕咕会被爆捶吗??? ZJ: 喜闻乐见: 27 Miemeng 60 01:59:43 100 01:59:44 0 01:59:44 160 01:59:44 最水的$T1$挂了???? $T2 ...

  2. 在菜单栏对应图标点击右键-关闭窗口,javaw.exe进程未关闭。

    问题: 可视化开发时,运行一个工程,总会生成一个javaw.exe进程. 关闭运行程序,javaw.exe还存在. 解决: 运行java工程时,会启动一个新的虚拟机来运行你的程序. 程序退出的时候,这 ...

  3. <数据链接>常用网站收集

    1.互联网数据指数 百度指数:http://index.baidu.com/ 阿里指数:http://index.1688.com/ TBI腾讯浏览指数:http://tbi.tencent.com/ ...

  4. java开发系列-Http协议

    概述 HTTP(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.这种协议用来规定通信数据的格式. HTTP请求 浏览器往服务器发送数据称之为请求.HTTP ...

  5. Redis安装过程:

  6. pg_hba.conf配置文件

    实例级别的权限由pg_hba.conf来控制,例如 : # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix doma ...

  7. js身份证号码验证(小程序版)

    参考知乎专栏文章https://zhuanlan.zhihu.com/p/22949023 <view class='bgw'> <form> ...... <view ...

  8. 2018-2019-2-20175332-实验四《Android程序设计》实验报告

    一.Android Stuidio的安装测试 题目要求: 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECANDROID,安装 Android St ...

  9. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]

    容器的主要职责有两个:存放元素和浏览元素.根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器. 用迭代器封装对动态数组的遍历:  1  2{<HeadFirst设计模式& ...

  10. IndentationError: expected an indented block错误

    Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...