日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > POJ 2391 Ombrophobic Bovines 不喜歡雨的奶牛 Floyd+二分枚舉+最大流

POJ 2391 Ombrophobic Bovines 不喜歡雨的奶牛 Floyd+二分枚舉+最大流

來源:程序員人生   發布時間:2014-10-08 17:21:07 閱讀次數:3250次

題目鏈接:POJ 2391 Ombrophobic Bovines

Ombrophobic Bovines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15006   Accepted: 3278

Description

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.

Input

* 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.

Output

* 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".

Sample Input

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

Sample Output

110

Hint

OUTPUT DETAILS: 

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.

Source

USACO 2005 March Gold


題意:

農場有F塊草地,奶牛們在草地上吃草。這些草地之間有P條路相連,路足夠寬,可以同時通過無限頭奶牛。有些草地有避雨點,奶牛們可以在此避雨。避雨點容量有限,一個避雨點不能容納所有奶牛。草地與路相比很小,奶牛通過草地時不需要時間。求所有奶牛都回到避雨點所花的最小時間。如果不能保證都能到避雨點,則輸出-1。

分析:

這道題和POJ 2112挺像。不過要難一些。

首先需要用Floyd算出所有點之間的最短距離。然后二分枚舉距離求最大流,求到的滿足最大流等于牛頭數的那個最小的距離即為答案。

但是出現了一個問題,結點現在也有了容量,因而我們需要拆點,把一個原始結點u分裂成u1和u2兩個結點,中間連一條有向弧,容量等于結點容量。原先到達u的弧改成到達u1,原先從u出發的弧改成u2出發。

構圖:

找一個源點s,一個匯點t,s和每個草地連一條邊,容量為草地上的牛數,每個避雨點和t連一條邊,容量為避雨點容納牛頭數。

拆點問題如上所述。

如果一塊草地到另一塊草地有最短路,且這個最短路小于當前枚舉到的路徑長度,那么這兩塊草地連一條邊,容量為無窮,因為可以走無限頭。

求解枚舉滿足最小值。

代碼:

寫了好多遍都T了,最后把vector和queue都換成了普通數組,然后過了,只給了1秒,實在太坑了。

#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 410 #define maxm 80080 #define INF 0x3f3f3f3f #define LL long long struct Edge { int from, to, cap; }EG[maxm]; //vector<Edge> EG; int G[maxn][maxm]; int n, f, p, s, t, sum, cow[maxn], bi[maxn], d[maxn], cur[maxn], cntE; LL mp[maxn][maxn]; int cntg[maxn]; int que[2*maxn]; void addEdge(int from, int to, int cap) { EG[cntE].from = from; EG[cntE].to = to; EG[cntE].cap = cap; cntE++; EG[cntE].from = to; EG[cntE].to = from; EG[cntE].cap = 0; cntE++; G[from][cntg[from]] = cntE-2; cntg[from]++; G[to][cntg[to]] = cntE-1; cntg[to]++; } bool bfs() { memset(d, -1, sizeof(d)); que[0] = s; d[s] = 0; int tt = 1, ff = 0; while(ff < tt) { int x = que[ff++]; for(int i = 0; i < cntg[x]; i++) { Edge& e = EG[G[x][i]]; if(d[e.to] == -1 && e.cap > 0) { d[e.to] = d[x]+1; que[tt++] = e.to; } } } return (d[t]!=-1); } int dfs(int x, int a) { if(x == t || a == 0) return a; int flow = 0, f; for(int& i = cur[x]; i < cntg[x]; i++) { Edge& e = EG[G[x][i]]; if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(a, e.cap))) > 0) { e.cap -= f; EG[G[x][i]^1].cap += f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Dinic() { int ans = 0; while(bfs()) { memset(cur, 0, sizeof(cur)); ans += dfs(s, INF); } return ans; } void Floyd() { for(int k = 1; k <= f; k++) for(int i = 1; i <= f; i++) for(int j = 1; j <= f; j++) mp[i][j] = min(mp[i][j], mp[i][k]+mp[k][j]); } void build(LL mid) { cntE = 0; memset(cntg, 0, sizeof(cntg)); for(int i = 1; i <= f; i++) { if(cow[i] != 0) addEdge(s, i, cow[i]); // s和每個草地連邊 if(bi[i] != 0) addEdge(i+f, t, bi[i]); // 每個草地的拆點和避雨點連邊 } for(int i = 1; i <= f; i++) for(int j = 1; j <= f; j++) if(mp[i][j] <= mid) addEdge(i, j+f, INF); // 草地和草地連邊 } void solve() { LL l = 0, r = 1LL*INF*INF, mid, ans = -1; // 二分枚舉,注意長度盡可能大 while( l <= r) { mid = (l+r)>>1ll; build(mid); // 構圖 int k = Dinic(); // 求最大流 if(sum == k) ans = mid, r = mid-1; else l = mid+1; } printf("%lld ", ans); } int main() { //freopen("poj_2391.txt", "r", stdin); int u, v; LL w; scanf("%d%d", &f, &p); sum = 0; s = 0; t = 2*f+1; n = 2*f+2; for(int i = 1; i <= f; i++) { scanf("%d%d", &cow[i], &bi[i]); sum += cow[i]; } memset(mp, INF, sizeof(mp)); // 設一個盡量大的值,但其實不是INF, memset還是挺管用的,字節填充 for(int i = 1; i <= p; i++) { scanf("%d%d%lld", &u, &v, &w); if(mp[u][v] > w) mp[u][v] = mp[v][u] = w; } for(int i = 0; i <= 2*f; i++) //注意自己是可以到自己的 mp[i][i] = 0; Floyd(); //求最短路 solve(); return 0; }


T了好多次,把STL刪掉越來越好,也是醉了:

Run ID User Problem Result Memory Time Language Code Length Submit Time
13481032 acmer 2391 Accepted 5112K 610MS G++ 3159B 2014-09-26 22:37:24
13480087 acmer 2391 Accepted 5112K 625MS G++ 3203B 2014-09-26 17:43:11
13480059 acmer 2391 Accepted 5136K 641MS G++ 3215B 2014-09-26 17:33:17
13480007 acmer 2391 Accepted 5240K 704MS G++ 3199B 2014-09-26 17:20:58
13479803 acmer 2391 Accepted 3344K 922MS G++ 2349B 2014-09-26 16:39:45



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 毛片一区| 一区二区三区精品在线 | 国产精品久久久久久久av大片 | 成人国产精品免费观看视频 | 日韩av毛片| 欧美日韩在线视频观看 | 日韩在线精品视频 | 精品久久久一区二区 | 国产精品第2页 | 国产九九精品 | cao久久 | 91精品国产91久久久久久最新 | 在线精品亚洲 | 亚洲一区二区三区在线看 | 久久精品视频一区二区 | 中文字幕在线播放第一页 | 国产二区在线播放 | 久久久久久久久久久福利 | 黄色一级片在线看 | 国产一区三区在线 | 在线va| 日韩综合精品 | 国产三级一区 | 很黄的网站在线观看 | 欧美日韩一区二区视频在线观看 | 在线中文字幕视频 | 久精品视频 | 高潮白浆女日韩av免费看 | 亚洲成人av在线播放 | 国产伦精品一区二区三区照片 | 成年免费视频 | 国产一区二区三区视频在线 | 国产a区| 精品国产精品国产 | 国产成人精品免高潮在线观看 | 国产视频一区在线观看 | 在线观看成人 | 国产一区二区三区视频在线 | 国产精品久久久亚洲 | 不卡的一区| 久久成年 |