【HDUOJ】4280 Island Transport
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280
题意:有n个岛屿,m条无向路,每个路给出最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿
题解:最大流的裸题。输入记得找到最西和最东的岛屿,以及注意是双向边。。这题用的sap.
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<queue>
#include<algorithm>
using namespace std; const int N=;
const int M=;
const int inf=0xfffffff; int n,m,cnt; struct Edge{
int v , cap , next;
} edge[M]; int head[N],pre[N],d[N],numd[N];
int cur_edge[N]; void addedge(int u,int v,int c){
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = head[u];
head[u] = cnt++; edge[cnt].v = u;
edge[cnt].cap = ;
edge[cnt].next = head[v];
head[v] = cnt++;
} void bfs(int s){
memset(numd,,sizeof(numd));
for(int i=; i<=n; i++) numd[ d[i] = n ]++;
d[s] = ;
numd[n]--;
numd[]++;
queue<int> Q;
Q.push(s); while(!Q.empty()){
int v=Q.front();
Q.pop(); int i=head[v];
while(i != -){
int u=edge[i].v; if(d[u]<n){
i=edge[i].next;
continue ;
} d[u] = d[v]+;
numd[n]--;
numd[d[u]]++;
Q.push(u);
i=edge[i].next;
}
}
} int SAP(int s,int t){
for(int i = ; i <= n; i++) cur_edge[i] = head[i];
int max_flow = ;
bfs(t);
int u = s ;
while(d[s] < n){
if(u == t){
int cur_flow = inf,neck;
for(int from = s; from != t; from = edge[cur_edge[from]].v){
if(cur_flow > edge[cur_edge[from]].cap){
neck = from;
cur_flow = edge[cur_edge[from]].cap;
}
} for(int from = s; from != t; from = edge[cur_edge[from]].v){ //修改增广路上的边的容量
int tmp = cur_edge[from];
edge[tmp].cap -= cur_flow;
edge[tmp^].cap += cur_flow;
}
max_flow += cur_flow;
u = neck;
} int i;
for(i = cur_edge[u]; i != -; i = edge[i].next)
if(edge[i].cap && d[u] == d[edge[i].v]+)
break; if(i != -){
cur_edge[u] = i;
pre[edge[i].v] = u;
u = edge[i].v;
}
else{
numd[d[u]]--;
if(!numd[d[u]]) break;
cur_edge[u] = head[u];
int tmp = n;
for(int j = head[u]; j != -; j = edge[j].next)
if(edge[j].cap && tmp > d[edge[j].v])
tmp = d[edge[j].v]; d[u] = tmp+;
numd[d[u]]++;
if(u != s)
u = pre[u];
}
}
return max_flow;
} inline void pre_init(){
cnt = ;
memset(head, -, sizeof head);
} void mapping(){
int u, v, w;
for(int i = ; i <= m; ++i){
scanf("%d %d %d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
} int main(){ int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d",&n,&m);
int x,y,s,t;
int minn = inf, maxx = -inf;
for(int i = ; i <= n; i++){
scanf("%d%d",&x,&y);
if(x <= minn){
s = i;
minn = x;
}
if(x >= maxx){
t = i;
maxx = x;
}
}
pre_init();
mapping();
int ans = SAP(s,t);
printf("%d\n",ans);
}
return ;
}
【HDUOJ】4280 Island Transport的更多相关文章
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- HDU 4280 Island Transport
Island Transport Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. Origi ...
- Hdu 4280 Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 4280 Island Transport(dinic+当前弧优化)
Island Transport Description In the vast waters far far away, there are many islands. People are liv ...
- HDU 4280 Island Transport(网络流)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=4280">http://acm.hdu.edu.cn/showproblem.php ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- HDU 4280 Island Transport(无向图最大流)
HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 比较裸的最大流题目,就是这是个无向图,并且比较卡时间. 思路: 是这样的,由于是 ...
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
- 【HDOJ】1385 Minimum Transport Cost
Floyd.注意字典序!!! #include <stdio.h> #include <string.h> #define MAXNUM 55 #define INF 0x1f ...
随机推荐
- leetcode.矩阵.766托普里茨矩阵-Java
1. 具体题目 如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵.给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True. 示例 1: 输入: ma ...
- [POI2010]OWC-Sheep
题目 不难猜到或者发现的性质,如果连了一条对角线划分出了奇数个点,那么这条对角线肯定不合法:因为划分成三角形就不可能有对角线相交,于是划分成奇数的那一边怎么样也不可能划分成全是偶数 于是我们需要对每一 ...
- 《linux 内核全然剖析》sched.c sched.h 代码分析笔记
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011368821/article/details/25129835 sched.c sched.h ...
- 自己写IRP,做文件操作,遇到的坑
在写文件的时候没问题,但是写完文件之后,就出问题了, 什么问题呢,是因为写完文件之后,文件关闭之后, 调用了一个叫做 CcFlushCache 的函数,这个函数是从CcWriteBehind 调过来的 ...
- BUUCTF MISC ZIP
这道题有点烦,拿出来单独写先贴两张图,一会用 首先这题给了68个压缩包,每个压缩包里只有4bytes大小,于是可以想到是crc爆破,自己写的脚本总是被killed,犯懒找了个脚本 import zip ...
- 博客中引入了gitment评论系统
官方github地址:https://github.com/imsun/gitment 官方中文说明地址:https://imsun.net/posts/gitment-introduction/ 官 ...
- printf sscanf进阶
printf ; printf (3d", a);//将打印 035 printf(“%-*s”, width, string): “*”: 在这里用width代替,其实和printf(“% ...
- react 数据发生变化,页面改变的原理
数据发生变化,页面改变的原理: 比较虚拟的dom 不怎么损耗性能,真实的dom比较会损耗性能 1.state 数据 2.jsx 模板 3.生成虚拟的dom 3.数据和模板结合,生成虚拟的dom 4.用 ...
- 阿里linux-Centos各版本下载
https://mirrors.aliyun.com/centos/7/isos/x86_64/ Index of /centos/7/isos/x86_64/ ../ 0_README.txt 16 ...
- 每天一个Linux命令:ls(1)
ls ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录). 格式 ls [-alrtAFR] [name...] 参数选项 参数 备注 -a 列出目录下的所有文件,包括以 . ...