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 ;
}

最新文章

  1. 【C#进阶系列】28 基元线程同步构造
  2. 使用Spring进行统一日志管理 + 统一异常管理
  3. SQL Server 中字符串中包含字符串变量的表示方法
  4. Netty5 + Protobuf 使用
  5. jboss7.1.1配置mysql数据源
  6. python-day6 常见算法 python内置模块
  7. Python语言精要---上
  8. IOS框架概览
  9. Docker容器管理平台Humpback进阶-私有仓库
  10. 图解ARP协议(二)ARP攻击原理与实践
  11. centos6.5上进行crontab操作
  12. Linux下使用crontab对MYSQL进行备份以及定时清
  13. 第二篇-ubuntu18.04下怎么制作GIF动画
  14. 『Python』内存分析_list和array
  15. linux常用命令:cat 命令
  16. Go Example--指针
  17. python 术语
  18. 定时任务的N种解决方案
  19. 如何使用 SSH 连接 VMWare 虚拟机中的 Ubuntu
  20. html中属于布尔类型的属性

热门文章

  1. 杀掉所有 skynet 进程
  2. 用代码来细说Csrf漏洞危害以及防御
  3. 【UML】:时序图
  4. spring mvc 使用kaptcha配置生成验证码实例
  5. 两台linux主机使用unison + inotify实现web文件夹同步
  6. 6_文件IO
  7. POJ 2853
  8. Kafka连接SparkStreaming的两种方式
  9. Twitter Bootstrap3小结
  10. Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.