题目链接

题意:农场有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. Unity的弱联网Json数据传输

    注意事项: 关于dictionary转json的工程中遇到一点问题:要手动添加双引号. 关于json转dictionary:同样需要手动去掉双引号,否则添加到dictionary中的字符串会带有双引号 ...

  2. DT-06 For Homekit

    一. 配置DT-06上网 连接此热点,会自动弹出wifi配置页面. 输入选中的路由密码,点 Join加入,如果路由没有出现在列表中,点 Other手工输入(仅支持2.4g路由配置) 二.配置dt-06 ...

  3. 基于jmeter+perfmon的稳定性测试记录

    1. 引子 最近承接了项目中一些性能测试的任务,因此决定记录一下,将测试的过程和一些心得收录下来. 说起来性能测试算是软件测试行业内,有些特殊的部分.这部分的测试活动,与传统的测试任务差别是比较大的, ...

  4. 物流运输trans「ZJOI2006」

    [题目描述] 物流公司要把一批货物从码头\(A\)运到码头\(B\).由于货物量比较大,需要\(n\)天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运 ...

  5. Stream和方法引用

    1.Stream流 1.for循环带来的弊端 在jdk8中,lambda专注于做什么,而不是怎么做 for循环的语法就是怎么做 for循环的循环体才是做什么 遍历是指每一个元素逐一进行处理,而并不是从 ...

  6. 7.15 迭代器 for循环的本质 生成器

    迭代器 迭代:更新换代的过程,每次的迭代都必须基于上一次的结果 迭代器:迭代取值的工具 作用 迭代器提供了一种不依赖于索引取值的方式 根据以上对于迭代的描述,如果只是简单的重复,不算迭代,如下: n ...

  7. Kafka 系列(二)—— 基于 ZooKeeper 搭建 Kafka 高可用集群

    一.Zookeeper集群搭建 为保证集群高可用,Zookeeper 集群的节点数最好是奇数,最少有三个节点,所以这里搭建一个三个节点的集群. 1.1 下载 & 解压 下载对应版本 Zooke ...

  8. Linux任务调度(8)

    crond任务调度: 是指系统在某个时间执行特定的命令或程序. 分类:1.系统工作,有些重要的工作必须周而复始地执行,如病毒扫描等:2.个别用户工作,个别用户可能希望执行某些程序,如mysql数据库备 ...

  9. 《Java 8 in Action》Chapter 5:使用流

    流让你从外部迭代转向内部迭代,for循环显示迭代不用再写了,流内部管理对集合数据的迭代.这种处理数据的方式很有用,因为你让Stream API管理如何处理数据.这样Stream API就可以在背后进行 ...

  10. Oracle 12c Adoption Discussion — Summary

    Morning (@9:30) Oracle 12c Overview & Features for Developers Oracle Database In-Memory Deep Div ...