题目链接

题意:农场有F(1 <= F <= 200)片草地用于放牛,这些草地有P(1 <= P <= 1500)连接,农场的草地上有一些避雨点,奶牛们可以在避雨点避雨,但是避雨点的大小是有限的,所以有的时候需要经过道路去其他避雨点避雨,而经过道路是需要时间的,求出所有奶牛进入避雨点所需要的最小时间,如果不能是所有奶牛进入避雨点,输出-1;每块草地上的奶牛数量范围为(1—1000),草地避雨点的范围为(1--1000)经过道路的时间范围为(1--1000000000)

floyd:首先需要用floyd计算奶牛距离他最近的避雨点,也就是任意两块草地的距离;因为奶牛需要用最少的时间到达避雨点

二分:其次这是100000000,然后1500条边,最后floyd计算出的时间可能会达到1000000000000,所以需要用到long long,然后二分去计算中间时间能否让所有奶牛进入避雨点,一次次去靠近正确答案;

最大流:用二分的时间上限去建图,用最大流去计算该时间上限能否让所有奶牛进入避雨点,不行则在去二分;

再说几个坑点:

1)一定要用long long ,不然会wa;

2)数组也别太大,可能会tle

3)在用结构体封装的dinic模板的时候,一定要在清除旧的数据,别新开,会tle

其实下面的代码还可以在优化的,比如把vector换成链式前向星

AC代码+部分测试数据

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<cstring>
#define ll long long
using namespace std;
const int maxn = 405;
const ll INF = 1e16;
int N,M;
ll Map[maxn][maxn];
int cow[maxn],sh[maxn]; struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
}; void init(){
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
if(i==j)Map[i][j]=0;
else Map[i][j]=INF;
}
}
} struct Dinic
{
int n,m,s,t;//结点数,边数(包括反向弧),源点编号,汇点编号
vector<Edge>edges;//边表,dges[e]和dges[e^1]互为反向弧
vector<int>G[maxn];//邻接表,G[i][j]表示结点i的第j条边在e数组中的编号
bool vis[maxn]; //BFS的使用
int d[maxn]; //从起点到i的距离
int cur[maxn]; //当前弧下标 void Init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
} void addedge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
int m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=0;
vis[s]=1;
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]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
} }
return vis[t];
} int dfs(int x,int a)//x表示当前结点,a表示目前为止的最小残量
{
if(x==t||a==0)return a;//a等于0时及时退出,此时相当于断路了
int flow=0,f;
for(int&i=cur[x];i<G[x].size();i++)//从上次考虑的弧开始,注意要使用引用,同时修改cur[x]
{
Edge&e=edges[G[x][i]];//e是一条边
if(d[x]+1==d[e.to]&&(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)break;//a等于0及时退出,当a!=0,说明当前节点还存在另一个曾广路分支。 }
}
return flow;
} int Maxflow(int s,int t)//主过程
{
this->s=s,this->t=t;
int flow=0;
while(bfs())//不停地用bfs构造分层网络,然后用dfs沿着阻塞流增广
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}din; int main(){
while(~scanf("%d%d",&N,&M)){
init();
int S=0,T=2*N+1;
int nowc=0;
for(int i=1;i<=N;i++){
scanf("%d%d",&cow[i],&sh[i]);
nowc+=cow[i];
}
int u,v;
ll MIN=-1,w;
while(M--){
scanf("%d%d%lld",&u,&v,&w);
if(w<Map[u][v]){
Map[u][v]=Map[v][u]=w;
MIN=max(MIN,w);
}
}
for(int k=1;k<=N;k++){//floyd建立最短路
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
if(Map[i][k]!=INF&&Map[k][j]!=INF){
Map[i][j]=min(Map[i][j],Map[i][k]+Map[k][j]);
MIN=max(MIN,Map[i][j]);
}
}
}
}
ll high=MIN+1,low=0,mid,ans=-1;
while(low<high){
mid=(low+high)>>1;
din.Init(2*N+1);
for(int i=1;i<=N;i++){
din.addedge(S,i,cow[i]);
din.addedge(i+N,T,sh[i]);
din.addedge(i,i+N,0x3f3f3f3f);
for(int j=i+1;j<=N;j++){
if(Map[i][j]<=mid){
din.addedge(i,j+N,0x3f3f3f3f);
din.addedge(j,i+N,0x3f3f3f3f);
}
}
}
int sum=din.Maxflow(S,T);
if(sum==nowc)
ans=high=mid;
else
low=mid+1;
}
printf("%lld\n",ans);
}
return 0;
}

测试数据:

input:
200 199
1000 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 1000
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
10 11 1000000000
11 12 1000000000
12 13 1000000000
13 14 1000000000
14 15 1000000000
15 16 1000000000
16 17 1000000000
17 18 1000000000
18 19 1000000000
19 20 1000000000
20 21 1000000000
21 22 1000000000
22 23 1000000000
23 24 1000000000
24 25 1000000000
25 26 1000000000
26 27 1000000000
27 28 1000000000
28 29 1000000000
29 30 1000000000
30 31 1000000000
31 32 1000000000
32 33 1000000000
33 34 1000000000
34 35 1000000000
35 36 1000000000
36 37 1000000000
37 38 1000000000
38 39 1000000000
39 40 1000000000
40 41 1000000000
41 42 1000000000
42 43 1000000000
43 44 1000000000
44 45 1000000000
45 46 1000000000
46 47 1000000000
47 48 1000000000
48 49 1000000000
49 50 1000000000
50 51 1000000000
51 52 1000000000
52 53 1000000000
53 54 1000000000
54 55 1000000000
55 56 1000000000
56 57 1000000000
57 58 1000000000
58 59 1000000000
59 60 1000000000
60 61 1000000000
61 62 1000000000
62 63 1000000000
63 64 1000000000
64 65 1000000000
65 66 1000000000
66 67 1000000000
67 68 1000000000
68 69 1000000000
69 70 1000000000
70 71 1000000000
71 72 1000000000
72 73 1000000000
73 74 1000000000
74 75 1000000000
75 76 1000000000
76 77 1000000000
77 78 1000000000
78 79 1000000000
79 80 1000000000
80 81 1000000000
81 82 1000000000
82 83 1000000000
83 84 1000000000
84 85 1000000000
85 86 1000000000
86 87 1000000000
87 88 1000000000
88 89 1000000000
89 90 1000000000
90 91 1000000000
91 92 1000000000
92 93 1000000000
93 94 1000000000
94 95 1000000000
95 96 1000000000
96 97 1000000000
97 98 1000000000
98 99 1000000000
99 100 1000000000
100 101 1000000000
101 102 1000000000
102 103 1000000000
103 104 1000000000
104 105 1000000000
105 106 1000000000
106 107 1000000000
107 108 1000000000
108 109 1000000000
109 110 1000000000
110 111 1000000000
111 112 1000000000
112 113 1000000000
113 114 1000000000
114 115 1000000000
115 116 1000000000
116 117 1000000000
117 118 1000000000
118 119 1000000000
119 120 1000000000
120 121 1000000000
121 122 1000000000
122 123 1000000000
123 124 1000000000
124 125 1000000000
125 126 1000000000
126 127 1000000000
127 128 1000000000
128 129 1000000000
129 130 1000000000
130 131 1000000000
131 132 1000000000
132 133 1000000000
133 134 1000000000
134 135 1000000000
135 136 1000000000
136 137 1000000000
137 138 1000000000
138 139 1000000000
139 140 1000000000
140 141 1000000000
141 142 1000000000
142 143 1000000000
143 144 1000000000
144 145 1000000000
145 146 1000000000
146 147 1000000000
147 148 1000000000
148 149 1000000000
149 150 1000000000
150 151 1000000000
151 152 1000000000
152 153 1000000000
153 154 1000000000
154 155 1000000000
155 156 1000000000
156 157 1000000000
157 158 1000000000
158 159 1000000000
159 160 1000000000
160 161 1000000000
161 162 1000000000
162 163 1000000000
163 164 1000000000
164 165 1000000000
165 166 1000000000
166 167 1000000000
167 168 1000000000
168 169 1000000000
169 170 1000000000
170 171 1000000000
171 172 1000000000
172 173 1000000000
173 174 1000000000
174 175 1000000000
175 176 1000000000
176 177 1000000000
177 178 1000000000
178 179 1000000000
179 180 1000000000
180 181 1000000000
181 182 1000000000
182 183 1000000000
183 184 1000000000
184 185 1000000000
185 186 1000000000
186 187 1000000000
187 188 1000000000
188 189 1000000000
189 190 1000000000
190 191 1000000000
191 192 1000000000
192 193 1000000000
193 194 1000000000
194 195 1000000000
195 196 1000000000
196 197 1000000000
197 198 1000000000
198 199 1000000000
199 200 1000000000 6 6
10 0
0 3
0 7
3 0
0 2
0 1
1 2 120
5 2 80
5 1 20
5 6 30
6 1 110
4 3 30 10 9
1 0
0 0
0 0
0 1
0 1
0 1
0 1
0 1
0 1
0 1
2 1 10
3 1 4
4 3 2
5 3 5
6 2 11
7 5 1
8 6 7
9 4 1
10 2 10 3 2
1 0
0 0
0 1
1 2 1
2 3 1 output:
199000000000
-1
6
2

POJ 2391 Ombrophobic Bovines(Floyd+二分+最大流)的更多相关文章

  1. POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)

    [题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...

  2. POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)

    http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路 ...

  3. POJ 2391 Ombrophobic Bovines【二分 网络流】

    题目大意:F个草场,P条道路(无向),每个草场初始有几头牛,还有庇护所,庇护所有个容量,每条道路走完都有时间,问所有奶牛都到庇护所最大时间最小是多少? 思路:和POJ2112一样的思路,二分以后构建网 ...

  4. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  5. poj 2391 Ombrophobic Bovines(最大流+floyd+二分)

    Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...

  6. POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11651   Accepted: 2 ...

  7. poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点

    题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...

  8. POJ 2391 Ombrophobic Bovines

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 4 ...

  9. poj--2391--Ombrophobic Bovines(floyd+二分+最大流拆点)

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u ...

  10. POJ 2391 Ombrophobic Bovines (二分答案+floyd+最大流)

    <题目链接> 题目大意: 给定一个有$n$个顶点和$m$条边的无向图,点$i$ 处有$A_i$头牛,点$i$ 处的牛棚能容纳$B_i$头牛,每条边有一个时间花费$t_i$(表示从一个端点走 ...

随机推荐

  1. Qt Socket 收发图片——图像拆包、组包、粘包处理

    之前给大家分享了一个使用python发图片数据.Qt server接收图片的Demo.之前的Demo用于传输小字节的图片是可以的,但如果是传输大的图片,使用socket无法一次完成发送该怎么办呢?本次 ...

  2. 转载 | 一种让超大banner图片不拉伸、全屏宽、居中显示的方法

    现在很多网站的Banner图片都是全屏宽度的,这样的网站看起来显得很大气.这种Banner一般都是做一张很大的图片,然后在不同分辨率下都是显示图片的中间部分.实现方法如下: <html> ...

  3. .NETCore C# 中级篇2-4 一文带你完全弄懂正则表达式

    .NETCoreCSharp 中级篇2-4 本节内容为正则表达式的使用 简介 有的时候,你是否有过这种需求:判断一个Ip地址.邮箱.密码规则是否合法.如果让你使用if一类的传统方法进行处理,你肯定会被 ...

  4. Duilib的圆环形 进度条 实现(网易云信版本)

    /** @file CircleProgress.h* @brief 圆环型进度条控件,圆环中间可以有文本(如85%)* @copyright (c) 2019-2022, NetEase Inc. ...

  5. Nunit与Xunit介绍

    Nunit安装 首先说下,nunit2.X与3.X版本需要安装不同的vs扩展. nunit2.x安装 安装如上3个,辅助创建nunit测试项目与在vs中运行单元测试用例 . 1.Nunit2 Test ...

  6. html5新特性-header,nav,footer,aside,article,section等各元素的详解

    Html5新增了27个元素,废弃了16个元素,根据现有的标准规范,把HTML5的元素按优先级定义为结构性属性.级块性元素.行内语义性元素和交互性元素四大类. 下面是对各标签的详解,section.he ...

  7. 对微软的敌视何时休? 从一篇语言评论文章对C#的评价说起

    看到一篇公众号文章<2020年什么编程语言最受欢迎,待遇最高?>,其中对C#的描述如下: 点击阅读原文,看到这是一篇翻译文章:https://codinginfinite.com/top- ...

  8. .net core redis的全套操作

    Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). Redis支持主从同步.数据可以从主服务器向任意数 ...

  9. MySQL隔离性及Spring事务

    一.数据库事务ACID特性 必须要掌握事务的4个特性,其中事务的隔离性之于MySQL,对应4级隔离级别. 原子性(Atomicity): 事务中的所有原子操作,要么都能成功完成,要么都不完成,不能停滞 ...

  10. egret之纹理填充模式(上下填充)

    首先,我们准备两张图片,一张作为背景“瓶子”,一张作位填充物“饮料”. 在皮肤里我们设置右边图片的填充模式为“repeat”,修改Y的缩放为:-1.,调整图片位置使之与地图重合,如下: 现在,我们可以 ...