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

 
 //2017-08-24
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int N = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[N<<]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} struct Dinic{
int level[N], S, T;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = -;
return ans;
}
int maxflow(){
int flow = , tmp;
while(bfs())
while((tmp = dfs(S, INF)) > )
flow += tmp;
return flow;
}
}dinic; int main()
{
//std::ios::sync_with_stdio(false);
//freopen("inputG.txt", "r", stdin);
int T, n, m;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
int x, y, s = , t = , mininum = INF, maxinum = -INF;
for(int i = ; i <= n; i++){
scanf("%d%d", &x, &y);
if(x < mininum){
mininum = x;
s = i;
}
if(x > maxinum){
maxinum = x;
t = i;
}
}
dinic.init(s, t);
int u, v, w;
for(int i = ; i < m; i++){
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w);
}
printf("%d\n", dinic.maxflow());
}
return ;
}

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

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

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

  2. hdu4280 Island Transport 最大流

    In the vast waters far far away, there are many islands. People are living on the islands, and all t ...

  3. Ford-Fulkerson 最大流算法

    流网络(Flow Networks)指的是一个有向图 G = (V, E),其中每条边 (u, v) ∈ E 均有一非负容量 c(u, v) ≥ 0.如果 (u, v) ∉ E 则可以规定 c(u, ...

  4. 关于最大流的EdmondsKarp算法详解

    最近大三学生让我去讲课,我就恶补了最大流算法,笔者认为最重要的是让学弟学妹们入门,知道算法怎么来的?为什么是这样?理解的话提出自己的改进,然后再看看Dinic.SAP和ISAP算法….. 一.概念引入 ...

  5. [数据结构]最大流之Ford-Fulkerson算法

    本文主要讲解最大流问题的Ford-Fulkerson解法.可以说这是一种方法,而不是算法,因为它包含具有不同运行时间的几种实现.该方法依赖于三种重要思想:残留网络,增广路径和割. 在介绍着三种概念之前 ...

  6. Java学习 · 初识 IO流

    IO流   1. 原理与概念 a)     流 i.           流动,流向 ii.           从一端移动到另一端 源头到目的地 iii.           抽象.动态概念,是一连 ...

  7. Java8 新特性 Stream() 创建流

    通过Controllere类的Stream()和parallelStream()创建流 //通过集合创建流 @Test public void test1() { String arr[] = new ...

  8. sh2.sed脚本练习

    1,删除/etc/grub.conf文件中行首的空白字符: sed -r 's@^[[ :spapce: ]] +@@g' /etc/grub.conf 2.替换/etc/inittab 文件中&qu ...

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

    题目1 : 网络流一·Ford-Fulkerson算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho住在P市,P市是一个很大很大的城市,所以也面临着一个 ...

  10. 最大流问题Ford-Fulkerson方法(转)

    本篇主要讲解最大流问题的Ford-Fulkerson解法.可是说这是一种方法,而不是算法,因为它包含具有不同运行时间的几种实现.该方法依赖于三种重要思想:残留网络,增广路径和割.本文将会详细介绍这些内 ...

随机推荐

  1. 预处理命令使用详解----#if、#endif、#undef、#ifdef、#else、#elif

    预处理命令 在接触#if.#undef这类预处理指令前,大部分都都接触过#define.#include等预处理命令,通俗来讲预处理命令的作用就是在编译和链接之前,对源文件进行一些文本方面的操作,比如 ...

  2. 如何执行超过一百兆(100MB)的sql脚本?

    最近遇到一个问题,在sqlserver的查询分析器里面执行一个超过100MB的数据库脚本,发现老是报“引发类型为“System.OutOfMemoryException”的异常”,上网查了一下,主要是 ...

  3. java打包jar后,使之一直在linux上运行,不随终端退出而关闭

      nohup java -jar xxx.jar&

  4. 修改ssh远程默认端口

    修改ssh远程默认端口 Linuxssh端口修改 1. 修改ssh配置文件 [root@distzabbix ~]# vim /etc/ssh/sshd_config 找到第17行附近#Port 22 ...

  5. ajax请求报语法错误

    今天改代码修正完一个ajax请求后,调试发现出错进error方法,查看错误信息报语法错误,具体是调用parseJSON方法时出错,因为我是用json方式传递的参数,所以第一时间查看data参数是否正确 ...

  6. AndroidStudio -- AndroidStuido中找不到cache.properties文件

    AndroidStuido中找不到cache.properties文件 报错信息: 16:32:10 Gradle sync failed: C:\Users\***\.gradle\caches\2 ...

  7. redhat_6.5下载地址

    redhat官方下载(需要注册帐号+订阅/申请试用方可下载) https://access.redhat.com/downloads/ 网络资源:附RHEL 6.5安装文件MD5及SHA-256:一. ...

  8. ORACLE数据库表解锁record is locked by another user

    出现此问题多由于操作Oracle执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user! ...

  9. (转)Python异常类的继承关系

    原文:https://blog.csdn.net/Dragonfli_Lee/article/details/52350793 https://www.cnblogs.com/Lival/p/6203 ...

  10. Activity四大启动模式

    ctivity的四种启动模式: standard.singleTop.singleTask.singleInstance 为了打印方便,定义一个基础Activity,在其onCreate方法和onNe ...