Island Transport

  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

题意:

n个点,要从最西边(X最小)的城市向最东边的(X最大)的城市运送人。求最大运送量。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=1e5+10;struct Edge
{int to,cap,next;
}edge[maxn<<1];
int head[maxn];
int cnt=0;
void init()
{cnt=0;memset(head,-1,sizeof head);
}
void AddEdge(int start,int to,int cap)
{edge[cnt].to=to;edge[cnt].cap=cap;edge[cnt].next=head[start];head[start]=cnt++;
}int src,tar;
int d[maxn];//深度
int q[maxn<<1];//汇点是否成功标号,即是否找到增广路
bool bfs()
{memset(d,-1,sizeof d);int front=0,tail=0;q[tail++]=src;d[src]=0;while(front<tail){int x=q[front++];if(x==tar)  return true;for(int i=head[x];i!=-1;i=edge[i].next){int temp=edge[i].to;//没有标记,且可行流大于0if(d[temp]==-1&&edge[i].cap>0){d[temp]=d[x]+1;q[tail++]=temp;}}}return false;
}int dfs(int x,int cap)
{if(x==tar)  return cap;int flow=0;for(int i=head[x];i!=-1;i=edge[i].next){int temp=edge[i].to;if(d[temp]==d[x]+1&&edge[i].cap){int f=dfs(temp,min(cap-flow,edge[i].cap));edge[i].cap-=f;edge[i^1].cap+=f;flow+=f;if(flow==cap) break;}}if(!flow)   d[x]=-2;//防止重搜return flow;
}int max_flow()
{int flow=0,f=0;while(bfs()){while((f=dfs(src,INF))>0)flow+=f;//printf("%d\n",flow);}return flow;
}int main()
{int T;scanf("%d",&T);while(T--){int n,m;scanf("%d%d",&n,&m);int west=INF,east=-INF;//int src,tar;for(int i=1;i<=n;i++){int x,y;scanf("%d%d",&x,&y);if(x<west)  src=i,west=x;if(x>east)  tar=i,east=x;}init();for(int i=1;i<=m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);AddEdge(u,v,w);AddEdge(v,u,w);}printf("%d\n",max_flow());}return 0;
}

STL超时代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=1e6+10;struct Edge
{int from,to,cap,flow;Edge(){}Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};struct Dinic
{int n,m,s,t;vector<Edge> edges;//存边vector<int> G[maxn];//邻接表存图bool vis[maxn];//标记节点的深度int d[maxn];//节点深度void init(int n,int s,int t){this->n=n, this->s=s, this->t=t;edges.clear();for(int i=0;i<n;i++) G[i].clear();}void AddEdge(int from,int to,int cap){edges.push_back( Edge(from,to,cap,0) );edges.push_back( Edge(to,from,cap,0) );m=edges.size();G[from].push_back( m-2 );G[to].push_back(m-1);}bool BFS(){queue<int> Q;memset(vis,0,sizeof(vis));vis[s]=true;d[s]=0;Q.push(s);while(!Q.empty()){int x= Q.front(); Q.pop();for(int i=0;i<G[x].size();++i){Edge& e=edges[G[x][i]];if(!vis[e.to] && e.cap>e.flow){vis[e.to]=true;d[e.to]=d[x]+1;Q.push(e.to);}}}return vis[t];}int DFS(int x,int a){if(x==t || a==0) return a;int flow=0,f;for(int i=0;i<G[x].size();i++){Edge& e=edges[G[x][i]];if(d[e.to]==d[x]+1 && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>0){e.flow +=f;edges[G[x][i]^1].flow -=f;flow +=f;a-=f;if(a==0) break;}}return flow;}int max_flow(){int ans=0;while(BFS()){ans+= DFS(s,INF);}return ans;}
}DC;int main()
{int T;scanf("%d",&T);while(T--){int n,m;scanf("%d%d",&n,&m);int west=INF,east=-INF;int src,tar;for(int i=1;i<=n;i++){int x,y;scanf("%d%d",&x,&y);if(x<west)  src=i,west=x;if(x>east)  tar=i,east=x;}DC.init(n,src,tar);for(int i=1;i<=m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);DC.AddEdge(u,v,w);}printf("%d\n",DC.max_flow());}return 0;
}

hdu4280 Island Transport 网络流最大流 Dinic算法高效模板相关推荐

  1. 图论 —— 网络流 —— 最大流 —— Dinic 算法

    [概述] Dinic 算法在 EK 算法的基础上进行了优化,其时间复杂度为 O(n*n*m). Dinic 在找增广路的时也是找最短增广路, 但与 EK 算法不同的是 Dinic 算法并不是每次 bf ...

  2. 网络流最大流 Dinic算法模板

    Dinic算法跟EK算法的优化地方就在于,EK是每次bfs记录前驱结点,然后从汇点找到一条回到源点的路. 而dinic是bfs记录深度,不断从源点dfs到汇点并且更新路径流量,直到dfs不到汇点. 邻 ...

  3. 网络流最大流Edmonds-Karp算法(模板)

    最大流算法的Edmonds-Karp算法,实际用的不多因为复杂度比Dinic高,把流初始化为零流,Maxflow返回最大流的值,同时在算法结束时所有已标号结点(a[u]>0的结点)构成集合S,剩 ...

  4. 网络流——最大流EK算法讲解

    网络流--最大流EK算法讲解 好了,这是第二篇博客了,如第一篇所述,来讲一讲刚刚理解的网络流.因为本人只会EK算法,所以先讲这个算法.(我会去补知识点的!!!) 什么是网络流??? 读者们刚接触这个知 ...

  5. 网络流-最大流(Ford-Fulkerson算法Dinic算法)

    文章目录 最大流 FF算法: 增广路: 反边: 代码: Dinic算法: Dinic + 当前弧优化 最大流 就如同水流,存在一张图,既有起点又有终点,从起点流向终点的最大流量就是最大流. 在上面的图 ...

  6. 【网络流】最大流问题(EK算法带模板,Dinic算法带模板及弧优化,ISAP算法带模板及弧优化)上下界网络流

    本blog重点是代码 网络流的相关概念 流网络(flow network) 流(flow) 网络的流 残留网络(residual network) 增广路径(augmenting path) Edmo ...

  7. UESTC 1143 数据传输 网络流 最大流 Dinic

    数据传输 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Sta ...

  8. (通俗易懂小白入门)网络流最大流——EK算法

    网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...

  9. HDU Problem - 4280 Island Transport(最大流)

    题目链接 Problem Description In the vast waters far far away, there are many islands. People are living ...

最新文章

  1. 关于“托管代码”和“非托管代码”
  2. Python 的函数
  3. 无法显示 xml 页 解决方案
  4. [html] 说说你对网格布局的理解
  5. 智能可穿戴迎来长续航焕新活力 出门问问TicWatch Pro 3即将国内上市
  6. [leetcode]LRU Cache
  7. C++ - extern C用法浅析
  8. mysql java.util.date_jdbc-java.sql.date和java.util.date之间转换
  9. 【每日更新】万维钢精英日课3课程分享笔记:模糊逻辑:灰度认知,灰度决策,黑白执行
  10. 微信公众平台接口测试帐号登录
  11. 【Keras】Keras中fit_generator的使用,及fit、fit_generator、和train_on_batch的区别
  12. safari浏览器找不到服务器怎么办,safari打不开网页因为服务器已停止响应解决方法...
  13. 项目进度控制的主要任务是什么?
  14. AjaxFileUpload组件结合Struts2异步图片上传
  15. 知识精华—修改文件夹的只读属性
  16. go ip过滤_智慧识别“GOIP”呼转 罪犯无所遁形
  17. java计算机毕业设计智能推荐电影网站源码+mysql数据库+系统+lw文档+部署
  18. w8dns服务器未响应,必应输入法在Win8.1下设置属性显示不正常的解决方法
  19. 7个秘诀,带你由数据分析师成长为数据科学家
  20. QQ空间迁移_【廉价共享存储解决方案1-drbd+ha+nfs】

热门文章

  1. 常用模块(数据序列化 json、pickle、shelve)
  2. ArcGIS API for JavaScript 入门教程[5] 再讲数据——Map类之底图与高程
  3. 学习练习 java 二分查找法
  4. IOS 项目名称修改(XCODE4.6)
  5. Microsoft SQL Server 2008技术内幕:T-SQL查询---------查询优化
  6. SQL Server XML性能优化(Best Practices)
  7. android 不可点击状态,Android系统.如何使用setClickable同时设置所有按钮可点击或不可点击?...
  8. 在html语言中frame,html Frame、Iframe、Frameset 的区别
  9. 云点播网页版_微软宣布:免费开放微软云办公、云桌面、云远程方案
  10. 通过javascript改变form提交的action,实现不同的按钮向不同的action提交同一个form的数据