题目描述

FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter. The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction. Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse. Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.

约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200)个田地上吃草.有P(1≤P≤1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚.

输入

* Line 1: Two space-separated integers: F and P

* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i. * Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.

    第1行:两个整数F和P;
    第2到F+1行:第i+l行有两个整数描述第i个田地,第一个表示田地上的牛数,第二个表示田地上的雨棚容量.两个整数都在0和1000之间.
    第F+2到F+P+I行:每行三个整数描述一条路,分别是起点终点,及通过这条路所需的时间(在1和10^9之间).

输出

* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".

一个整数,表示最少的时间.如果无法使牛们全部进入雨棚,输出-1.

样例输入

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

样例输出

110


题解

floyd+二分+拆点+网络流

先用floyd求出任意两点之间的距离。

然后二分答案,若i与j之间的距离小于等于mid,则将i与j'(拆出来的点)间连一条容量为正无穷的边。

将源点与每个点间连一条容量为牛数的边,将每个拆出来的点与汇点间连一条容量为牛棚容量的边。

然后跑网络流,判断是否满流即可。

注意图可以是不连通的,所以当ans过大时,说明必须要用到题目中不存在的边,即无论如何都不能满足题意,输出-1。

注意距离要开long long。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #define inf 0x3fffffff
  5. using namespace std;
  6. queue<int> q;
  7. long long dis[201][201];
  8. int a[201] , b[201] , head[403] , to[180000] , val[180000] , next[180000] , cnt , s , t , deep[403];
  9. void add(int x , int y , long long z)
  10. {
  11. to[++cnt] = y;
  12. val[cnt] = z;
  13. next[cnt] = head[x];
  14. head[x] = cnt;
  15. }
  16. bool bfs()
  17. {
  18. int x , i;
  19. while(!q.empty())
  20. q.pop();
  21. memset(deep , 0 , sizeof(deep));
  22. deep[s] = 1;
  23. q.push(s);
  24. while(!q.empty())
  25. {
  26. x = q.front();
  27. q.pop();
  28. for(i = head[x] ; i ; i = next[i])
  29. {
  30. if(val[i] && !deep[to[i]])
  31. {
  32. deep[to[i]] = deep[x] + 1;
  33. if(to[i] == t)
  34. return 1;
  35. q.push(to[i]);
  36. }
  37. }
  38. }
  39. return 0;
  40. }
  41. int dinic(int x , int low)
  42. {
  43. if(x == t)
  44. return low;
  45. int temp = low , i , k;
  46. for(i = head[x] ; i ; i = next[i])
  47. {
  48. if(val[i] && deep[to[i]] == deep[x] + 1)
  49. {
  50. k = dinic(to[i] , min(temp , val[i]));
  51. if(!k) deep[to[i]] = 0;
  52. val[i] -= k;
  53. val[i ^ 1] += k;
  54. if(!(temp -= k)) break;
  55. }
  56. }
  57. return low - temp;
  58. }
  59. bool judge(int n , long long mid , int sum)
  60. {
  61. memset(head , 0 , sizeof(head));
  62. memset(to , 0 , sizeof(to));
  63. memset(val , 0 , sizeof(val));
  64. memset(next , 0 , sizeof(next));
  65. cnt = 1;
  66. int i , j , maxflow = 0;
  67. for(i = 1 ; i <= n ; i ++ )
  68. {
  69. add(s , i , a[i]);
  70. add(i , s , 0);
  71. add(i + n , t , b[i]);
  72. add(t , i + n , 0);
  73. for(j = 1 ; j <= n ; j ++ )
  74. {
  75. if(i == j || dis[i][j] <= mid)
  76. add(i , j + n , inf) , add(j + n , i , 0);
  77. }
  78. }
  79. while(bfs())
  80. maxflow += dinic(s , inf);
  81. return maxflow == sum;
  82. }
  83. int main()
  84. {
  85. int n , m , i , j , k , x , y , suma = 0 , sumb = 0;
  86. long long z , l = 0 , r = 0 , mid , ans = -1;
  87. scanf("%d%d" , &n , &m);
  88. s = 0 , t = 2 * n + 1;
  89. for(i = 1 ; i <= n ; i ++ )
  90. scanf("%d%d" , &a[i] , &b[i]) , suma += a[i] , sumb += b[i];
  91. memset(dis , 0x3f , sizeof(dis));
  92. for(i = 1 ; i <= m ; i ++ )
  93. scanf("%d%d%lld" , &x , &y , &z) , dis[x][y] = dis[y][x] = min(dis[x][y] , z);
  94. if(suma > sumb)
  95. {
  96. printf("-1\n");
  97. return 0;
  98. }
  99. for(k = 1 ; k <= n ; k ++ )
  100. for(i = 1 ; i <= n ; i ++ )
  101. for(j = 1 ; j <= n ; j ++ )
  102. dis[i][j] = min(dis[i][j] , dis[i][k] + dis[k][j]);
  103. for(i = 1 ; i <= n ; i ++ )
  104. for(j = 1 ; j <= n ; j ++ )
  105. if(i != j)
  106. r = max(r , dis[i][j]);
  107. while(l <= r)
  108. {
  109. mid = (l + r) >> 1;
  110. if(judge(n , mid , suma))
  111. ans = mid , r = mid - 1;
  112. else
  113. l = mid + 1;
  114. }
  115. printf("%lld\n" , ans < 10000000000000ll ? ans : -1);
  116. return 0;
  117. }

【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流的更多相关文章

  1. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

  2. BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

    先预处理出来每个点对之间的最短距离 然后二分答案,网络流判断是否可行就好了恩 /************************************************************ ...

  3. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

    Description 约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200 ...

  4. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  5. bzoj 1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 最大流+二分

    题目要求所有牛都去避雨的最长时间最小. 显然需要二分 二分之后考虑如何判定. 显然每头牛都可以去某个地方 但是前提是最短路径<=mid. 依靠二分出来的东西建图.可以发现这是一个匹配问题 din ...

  6. 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

    题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possi ...

  7. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】

    二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

  8. Ombrophobic Bovines

    poj2391:http://poj.org/problem?id=2391 题意:一个人有n个农场,每个农场都一个避雨的地方,每个农场有一些牛,每个避雨的地方能容纳牛的数量是有限的.农场之间有一些道 ...

  9. POJ 2391 Ombrophobic Bovines

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

随机推荐

  1. Developing iOS 8 Apps with Swift (stanford)

    https://www.youtube.com/watch?v=JkiB8Zwk-9Q&index=2&list=PLcX0opNQliFl0RTGbY9HHWCFg0Q8_fIF4

  2. #386. 【UNR #3】鸽子固定器

    #386. [UNR #3]鸽子固定器 题目链接 官方题解 分析: 神奇的做法+链表. 首先按照大小排序. 对于小于选择小于m个物品的时候,这个m个物品一定是一段连续的区间.因为,如果中间空着一个物品 ...

  3. .net core mvc 模型绑定 之 json and urlencoded

    .net core mvc 模型绑定, FromQuery,对应 url 中的 urlencoded string ("?key1=value1&key2=value2") ...

  4. java nio之channel

    一.通道(Channel):由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel本身不能直接访问 ...

  5. springboot与activemq的使用

    1.springboot和activemq的使用相对来说比较方便了,我在网上看了很多其他的资料,但是自己写出来总是有点问题所以,这里重点描述一下遇到的一些问题. 2.至于activemq的搭建和spr ...

  6. Tomcat7后台通过get接收数据处理乱码

    Tomcat7后台通过get接收数据处理乱码 //因为tomcat7 默认将用get传来的数据用ISO-8859-1封装, //将ajax传过来的值解码,再转码,//因为tomcat7 默认将用get ...

  7. SpringBoot入门(二)——起步依赖

    本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...

  8. Qt-QSplashScreen-程序启动动画

    多数大型应用程序启动时可会在程序完全启动前显示一个启动画面,在程序完全启动后消失,程序启动画面可以显示相关产品的一些信息,使用户在等待程序启动时同时了解产品的相关功能,这也是一种宣传方式. 首先运行界 ...

  9. 用最简单的MVC模式输出内容

    MVC是模型(model)-视图(view)-控制器(controller)的缩写,它的作用是使代码分离,可维护性高.重用性高 编写Model层: <?php class model{ publ ...

  10. JDK11安装后,环境变量的坑

    安装了最新的JDK11,安装完后设置环境变量,打开CMD,没生效 检查了3遍,都没发现问题,在PATH中将JAVA设置移到第一也不行 最后偶然发现,在点击如图右下的‘编辑文本’,用文本方式编辑时,发现 ...