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

解析:这是一道基础的最大流,题目要求的是求出从最左边到最右边通过的最大流,而且每条路是双向的,代码如下,这道题不知道我之前写的代码怎么错了,怎么改都改不对,难受,等以后有时间再看吧,或是哪位大佬知道,欢迎指点

这是正确的代码


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=100005;
const int maxx=200005;
struct node
{int v,w,next;
}e[maxx];
int head[maxn],vis[maxn];
int tot,S,T;
void addEdge(int u,int v,int cap)
{e[tot].v=v,e[tot].w=cap,e[tot].next=head[u],head[u]=tot++;e[tot].v=u,e[tot].w=cap,e[tot].next=head[v],head[v]=tot++;
}
bool bfs()
{queue<int> q;memset(vis,-1,sizeof(vis));q.push(S);vis[S]=0;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];~i;i=e[i].next){int v=e[i].v;if(vis[v]==-1&&e[i].w){vis[v]=vis[u]+1;if(v==T)return true;q.push(v);}}}return false;
}
int dfs(int u,int f)
{if(u==T||!f)return f;int r=0;for(int i=head[u];~i;i=e[i].next){int v=e[i].v;if(vis[v]==vis[u]+1&&e[i].w){int d=dfs(v,min(f,e[i].w));if(d>0){e[i].w-=d;e[i^1].w+=d;r+=d;f-=d;if(!f)break;}}}if(!r)vis[u]=-1;return r;
}int Dinic()//然后直接调用这个即可
{int ans=0;while(bfs())ans+=dfs(S,INF);return ans;
}void init()//记得每次使用前初始化
{memset(head,-1,sizeof(head));tot=0;
}int main()
{int t;scanf("%d",&t);int x,y,w;int n,m;int maxI,minI;while(t--){init();scanf("%d%d",&n,&m);scanf("%d%d",&x,&y);S=T=1;maxI=minI=x;for(int i=2;i<=n;i++){scanf("%d%d",&x,&y);if(maxI<x)T=i,maxI=x;if(minI>x)S=i,minI=x;}while(m--){scanf("%d%d%d",&x,&y,&w);addEdge(x,y,w);}int ans=Dinic();printf("%d\n",ans);}return 0;
}

这是一直超时的代码wa的我难受

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=200010;
const int mm=100010;
struct node
{int v,w,next;
} e[M];
int first[mm],vis[mm];
int tot=0,head,tail;
int m,n;
void init()
{tot=0;memset(first,-1,sizeof(first));
}
void add_edge(int u,int v,int w)
{e[tot].v=v;e[tot].w=w;e[tot].next=first[u];first[u]=tot++;e[tot].v=u;e[tot].w=w;e[tot].next=first[v];first[v]=tot++;
}
int bfs()
{memset(vis,-1,sizeof(vis));vis[head]=0;queue<int>q;while(!q.empty())q.pop();q.push(head);while(!q.empty()){int u=q.front();q.pop();for(int i=first[u]; i!=-1; i=e[i].next){int v=e[i].v;if(e[i].w>0&&vis[v]==-1){vis[v]=vis[u]+1;q.push(v);}}}return vis[tail]!=-1;
}
int dfs(int u,int w1)
{if(u==tail)return w1;int f=0,ans=0;for(int i=first[u]; i!=-1; i=e[i].next){int v=e[i].v,w=e[i].w;if(w>0&&vis[v]==vis[u]+1&&(f=dfs(v,min(w1,w)))){e[i].w-=f;e[i^1].w+=f;ans+=f;w1-=f;if(!f)break;}}vis[u]=-1;return ans;
}
void Dinic()
{int ans=0;while(bfs()){ans+=dfs(head,inf);}printf("%d\n",ans);
}
int main()
{int t,x,y,z;scanf("%d",&t);while(t--){init();scanf("%d%d",&n,&m);int maxx=-100000,minn=100000;for(int i=1; i<=n; i++){scanf("%d%d",&x,&y);if(x>maxx)maxx=x,tail=i;if(x<minn)minn=x,head=i;}for(int i=1; i<=m; i++){scanf("%d%d%d",&x,&y,&z);add_edge(x,y,z);}Dinic();}return 0;
}

Island Transport相关推荐

  1. hdu4280 Island Transport 网络流最大流 Dinic算法高效模板

    Island Transport In the vast waters far far away, there are many islands. People are living on the i ...

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

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

  3. HDU - 4280 Island Transport(最大流)

    题目链接:点击查看 题目大意:给出n个岛屿,由m条无向边连接而成,现在要从最西边的岛屿到达最东边的岛屿,问最大流量为多少 题目分析:最大流模板题,只不过这个毒瘤题卡掉了dinic,只能去网上找了个SA ...

  4. G - Island Transport

    读题 给定一个无向图,求从最左侧的点到最右侧的点的最大流. 解题 无向图的最大流与有向图的最大流的区别在于反向边的流量不是零而是与正向边相等.注意这点之后,再打一个Dinic算法模板.考虑到数据特别地 ...

  5. 解题报告:【kuangbin带你飞】专题十一 网络流

    目录 A.POJ 3436 ACMComputerFactoryACM\ Computer\ FactoryACM Computer Factory[省选/NOI- ] B.POJ 3281 Dini ...

  6. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  7. hdu4280(最大流)

    传送门:Island Transport 题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿. 分析:无向图正反都加弧,权值一样,这题点多, ...

  8. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  9. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  10. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

最新文章

  1. 费用保险单,如何失焦时自动补零
  2. div搜索框与按钮不在一行_前阿里巴巴运营专家:搜索框的5大运营玩法
  3. python输出日期的模版_python按日期区间生成markdown日记模板
  4. c语言循环程序设计教案,10 《C语言程序设计》教案 第三章 程序的控制结构(6)—循环结构 while和do while.doc...
  5. 定时清理日志文件-python实现
  6. C# 函数重载 示例 求圆的面积
  7. 拥抱.NET Core系列:Logging (1)
  8. 前端学习(554):node实现登录和注册第二部分代码
  9. 赋能尖端科技 推进智能布局 |《HPC高性能计算数据存储解决方案蓝皮书》正式发布
  10. Zabbix Agent2监控redis
  11. 企业数字化/数智化转型(一):数智力创新2.0
  12. html5远程桌面 微软,微软正在测试远程桌面HTML5网页版本客户端!
  13. 定时任务每隔10分钟
  14. 小程序文档整理之 -- API(媒体)
  15. linux 查看dns进程,探查Linux系统DNS服务器运行状况
  16. 智慧农业解决方案-智慧农业电子科技威海
  17. 交叉编译libusb和libusb-compat-0.1.5
  18. 新手练习2:人物模型多边形建模流程图解
  19. leaflet一键清空所有已绘制的多边形和点位
  20. 自定义new Date()格式显示,适用JavaScript / Nodejs / Vue / React / UniApp / 其他基于js工程的项目

热门文章

  1. vue中使用第三方阿里巴巴矢量图标库,并修改图标大小
  2. 【附源码】计算机毕业设计java医院人事及科室病区管理设计与实现
  3. ubuntu20.04中安装划词翻译_Chrome翻译插件【沙拉查词】amp;【彩云小译】
  4. HE4484E泛海微5V USB 输入双节锂电池串联应用升压充电IC管理芯片
  5. 51智联等已成“厕所”,猎聘网建“会所”求突破
  6. c++ 向量化_一种新的FIR滤波器系数量化方法
  7. python — numpy计算矩阵特征值,特征向量
  8. 浅谈微信小程序和微信公众平台
  9. 支付系统,支付流程及实现介绍
  10. cocos2d-x横版动作游戏《闯关吧》源码