【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流
題目描述
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。
#include <cstdio> #include <cstring> #include <queue> #define inf 0x3fffffff using namespace std; queue<int> q; long long dis[201][201]; int a[201] , b[201] , head[403] , to[180000] , val[180000] , next[180000] , cnt , s , t , deep[403]; void add(int x , int y , long long z) {to[++cnt] = y;val[cnt] = z;next[cnt] = head[x];head[x] = cnt; } bool bfs() {int x , i;while(!q.empty())q.pop();memset(deep , 0 , sizeof(deep));deep[s] = 1;q.push(s);while(!q.empty()){x = q.front();q.pop();for(i = head[x] ; i ; i = next[i]){if(val[i] && !deep[to[i]]){deep[to[i]] = deep[x] + 1;if(to[i] == t)return 1;q.push(to[i]);}}}return 0; } int dinic(int x , int low) {if(x == t)return low;int temp = low , i , k;for(i = head[x] ; i ; i = next[i]){if(val[i] && deep[to[i]] == deep[x] + 1){k = dinic(to[i] , min(temp , val[i]));if(!k) deep[to[i]] = 0;val[i] -= k;val[i ^ 1] += k;if(!(temp -= k)) break;}}return low - temp; } bool judge(int n , long long mid , int sum) {memset(head , 0 , sizeof(head));memset(to , 0 , sizeof(to));memset(val , 0 , sizeof(val));memset(next , 0 , sizeof(next));cnt = 1;int i , j , maxflow = 0;for(i = 1 ; i <= n ; i ++ ){add(s , i , a[i]);add(i , s , 0);add(i + n , t , b[i]);add(t , i + n , 0);for(j = 1 ; j <= n ; j ++ ){if(i == j || dis[i][j] <= mid)add(i , j + n , inf) , add(j + n , i , 0);}}while(bfs())maxflow += dinic(s , inf);return maxflow == sum; } int main() {int n , m , i , j , k , x , y , suma = 0 , sumb = 0;long long z , l = 0 , r = 0 , mid , ans = -1;scanf("%d%d" , &n , &m);s = 0 , t = 2 * n + 1;for(i = 1 ; i <= n ; i ++ )scanf("%d%d" , &a[i] , &b[i]) , suma += a[i] , sumb += b[i];memset(dis , 0x3f , sizeof(dis));for(i = 1 ; i <= m ; i ++ )scanf("%d%d%lld" , &x , &y , &z) , dis[x][y] = dis[y][x] = min(dis[x][y] , z);if(suma > sumb){printf("-1\n");return 0;}for(k = 1 ; k <= n ; k ++ )for(i = 1 ; i <= n ; i ++ )for(j = 1 ; j <= n ; j ++ )dis[i][j] = min(dis[i][j] , dis[i][k] + dis[k][j]);for(i = 1 ; i <= n ; i ++ )for(j = 1 ; j <= n ; j ++ )if(i != j)r = max(r , dis[i][j]);while(l <= r){mid = (l + r) >> 1;if(judge(n , mid , suma))ans = mid , r = mid - 1;elsel = mid + 1;}printf("%lld\n" , ans < 10000000000000ll ? ans : -1);return 0; }轉載于:https://www.cnblogs.com/GXZlegend/p/6394340.html
總結
以上是生活随笔為你收集整理的【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于利用DEM生成水系图
- 下一篇: 自己为 GridView 写分页 如: