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. 2016 CCPC网络选拔赛 部分题解

    HDU 5832 - A water problem 题意:有两颗星球,一年的长度分别为37天和173天.问第n天时它们是否为新年的第一天. 思路:显然  n 同时被37和173整除时,两种历法都在新 ...

  2. Vue中的better-scroll插件

    Vue中的better-scroll插件 在需要的文件中添加 import BScorll from 'better-scroll'; 引用的示例代码: let scroll = new BScrol ...

  3. ORA-01790: 表达式必须具有与对应表达式相同的数据类型

    出现这种错误,要先看一下是不是sql中有用到连接:union,unio all之类的,如果有,需要注意相同名称字段的数据类型一定要相同.

  4. springboot2配置druid数据库连接池

    注意配置以下的依赖: <!-- 引入druid数据源--> <dependency> <groupId>com.alibaba</groupId> &l ...

  5. leetcode 1078 Occurrences After Bigram

    lc1078 Occurrences After Bigram trim().split()将原字符串转换成words数组 依次匹配first和second,若两者都能匹配上,则下一个单词为third ...

  6. 使用 top instance 命令查看运行中 MaxCompute 作业

    我们都知道,在 MaxCompute Console 里,可以使用下面的命令来列出运行完成的 instance 列表. show p|proc|processlist [from <yyyy-M ...

  7. 最全的CSS hack没有之一

    1.何为HACK? 简单的说,HACK就是只有特定浏览器才能识别这段hack代码.Hack也可以说是让前端最为头疼的问题,因为要写N多种兼容代码.当然,IE是最让人蛋疼的. 一般来说,CSS HACK ...

  8. 在 Angularjs 中$state.go 如何传递参数

    在目标页面规定接受的参数: .state('app.AttendanceEditFixed', { url: '/AttendanceEditFixed', params: {'id': null,' ...

  9. git commit规范工具

    npm install -g commitizen commitizen init cz-conventional-changelog --save --save-exact 以后,凡是用到git c ...

  10. mysql基础教程(一)-----概述、安装、查询

    概述 好处 •实现数据持久化 •使用完整的管理系统统一管理,易于查询 概念 DB 数据库(database):存储数据的“仓库”.它保存了一系列有组织的数据. DBMS 数据库管理系统(Databas ...