多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > 互聯(lián)網(wǎng) > POJ 2391 Ombrophobic Bovines 不喜歡雨的奶牛 Floyd+二分枚舉+最大流

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

來源:程序員人生   發(fā)布時間:2014-10-08 17:21:07 閱讀次數(shù):3242次

題目鏈接: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


題意:

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

分析:

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

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

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

構(gòu)圖:

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

拆點問題如上所述。

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

求解枚舉滿足最小值。

代碼:

寫了好多遍都T了,最后把vector和queue都換成了普通數(shù)組,然后過了,只給了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); // 構(gòu)圖 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)); // 設(shè)一個盡量大的值,但其實不是INF, memset還是挺管用的,字節(jié)填充 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



生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 天天视频官网天天视频在线 | 亚洲 欧美 精品 中文第三 | 就去干成人 | 亚洲日韩中文字幕在线播放 | 中文字幕成人在线观看 | 另类图片成人偷拍 | 伊人成综合 | 日韩国产精品99久久久久久 | 精品一区二区三区在线视频观看 | 粉嫩00福利视频在线精品 | 亚洲欧美日产综合在线看 | 欧美精品99久久久久久人 | 亚洲国产精品久久日 | 91嫩草国产在线观看免费 | 精品一区二区91 | asianjapanese日本护士 | 欧美日本日韩aⅴ在线视频 欧美日本视频一区 | 亚洲福利一区 | 特级aa一级欧美毛片 | 国产高清在线看免费视频观 | www伊人网| 亚洲在线免费观看 | 日本精品久久久久护士 | 日韩精品一区二区三区免费视频 | 精品二区 | 欧美一级片手机在线观看 | 国产亚洲欧美久久精品 | 天堂亚洲网 | 欧美孕妇与黑人巨交 | 国产成年网站v片在线观看 国产成人 免费观看 | 欧美.亚洲.日本一区二区三区 | 亚洲另类图 | 在线观看的免费视频网站 | 可以免费观看欧美一级毛片 | 精品久久伊人 | 亚洲日韩中文字幕一区 | 亚洲国产成人久久一区www妖精 | 国产精品成人久久久 | 黑人插入 | 亚洲啊v在线 | 亚洲国产第一页 |