Island Transport

Time Limit: 10000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 4280
64-bit integer IO format: %I64d      Java class name: Main

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

 
解题:网络流模板题,注意双向边,其实可以这样子搞~
 #include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[];
int head[maxn],d[maxn],gap[maxn],tot,S,T,n,m;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,flow,head[v]);
head[v] = tot++;
}
queue<int>q;
void bfs(){
for(int i = ; i <= n; ++i){
d[i] = -;
gap[i] = ;
}
d[T] = ;
q.push(T);
while(!q.empty()){
int u = q.front();
q.pop();
++gap[d[u]];
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,minH = n - ;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow){
if(d[e[i].to] + == d[u]){
int a = dfs(e[i].to,min(low,e[i].flow));
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
if(d[S] >= n) return tmp;
}
if(e[i].flow) minH = min(minH,d[e[i].to]);
}
}
if(!tmp){
if(--gap[d[u]] == ) d[S] = n;
++gap[d[u] = minH + ];
}
return tmp;
}
int main(){
int kase,x,y,s,t,u,v,w;
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&n,&m);
memset(head,-,sizeof head);
s = INF;
int ret = t = ;
for(int i = ; i <= n; ++i){
scanf("%d%d",&x,&y);
if(s > x){
s = x;
S = i;
}
if(t < x){
t = x;
T = i;
}
}
for(int i = tot = ; i < m; ++i){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
bfs();
while(d[S] < n) ret += dfs(S,INF);
printf("%d\n",ret);
}
return ;
}

最新文章

  1. 【grunt第二弹】30分钟学会使用grunt打包前端代码(02)
  2. Python Day1
  3. adapter用法
  4. iOS中使用 Reachability 检测网络
  5. VBS练习题
  6. 关于Redis的启动过程
  7. Android数据存储(三)——SQLite
  8. (转载)一句简单命令重启nginx - [nginx]
  9. hdu 1563 Find your present!
  10. 关于在Java代码中写Sql语句需要注意的问题
  11. mysql 一张表的数据插入另一张表的sql语句
  12. leetcode(js)算法605之种花问题
  13. 《Android进阶之光》--多线程编程
  14. Delphi获取默认打印机名称及端口
  15. Spark分析之Master、Worker以及Application三者之间如何建立连接
  16. ubuntu修改运行级别方法
  17. svn服务器快速搭建及简单配置
  18. 【WinRT】TransitionDemo
  19. linux下redis安装步骤
  20. block 回调个人理解

热门文章

  1. .Net 遍历目录下第一层的子文件夹和子文件夹里的文件
  2. Navicat for MySQL在ubuntu下运行没有反应
  3. java定时读取文件
  4. 使用vscode软件运行zebrajs框架小结
  5. 超简单!一步创建自己的wifi热点~
  6. C# 语言 类
  7. Python学习日志9月17日 一周总结
  8. 迅为IMX6Q开发板在道路交通信号控制系统解决方案中的应用
  9. python之路——目录
  10. 【Qt】2.1 创建对话框