Link: poj 2391 ombrophobic bovines
Ombrophobic bovines
Time limit:1000 ms |
|
Memory limit:65536 K |
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 47 20 42 61 2 403 2 702 3 901 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 |
Question:
There are f lawns on the farm, and the cows graze on the grass. There are P links between the lawns, and the roads are wide enough to allow unlimited cows at the same time. Some lawns have shelter from rain, and cows can shelter it from rain. The shelter capacity is limited. A shelter cannot accommodate all cows. The grass is relatively small than the road, and it does not take time for cows to pass through the grass. The minimum time it takes for all the cows to return to the rain shelter. If it cannot be ensured that all traffic can reach the shelter,-1 is output.
Analysis:
This question is very similar to poj 2112. But it is difficult.
First, use Floyd to calculate the shortest distance between all points. Then, the distance of the binary enumeration is used to find the maximum stream. The minimum distance that satisfies the maximum flow is equal to the number of cattle heads, that is, the answer.
But there is a problem, and the node now has the capacity, so we need to split the original node u into two nodes, u1 and U2, and connect a directed arc in the middle, the capacity is equal to the node capacity. The arc originally arriving at u is changed to U1, And the arc originally starting from u is changed to U2.
Diagram:
Find a Source Vertex S, a sink vertex t, s and each lawn is connected with one edge, the capacity is the number of cattle on the lawn, each shelter and T connected to one side, the capacity is to shelter from the rain to accommodate the number of cattle.
The split point problem is described above.
If the shortest path from one lawn to the other is short, and the shortest path is smaller than the path length listed in the current enumeration, the two lawns are connected to one edge with an infinite capacity, because it can be infinite.
The solution enumeration satisfies the minimum value.
Code:
After writing t many times, we finally changed the vector and queue to a normal array. After that, we only gave one second, which is too pitfall.
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; # define maxn 410 # define maxm 80080 # define INF 0x3f3f3f3f # define ll long longstruct 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, 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. CA P> 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 and each lawn edge if (Bi [I]! = 0) addedge (I + F, T, Bi [I]); // edge of each lawn split point and rain proof point} For (INT I = 1; I <= f; I ++) for (Int J = 1; j <= f; j ++) if (MP [I] [J] <= mid) addedge (I, j + F, INF); // lawn and lawn edge} void solve () {ll l = 0, r = 1ll * INF, mid, ans =-1; // binary enumeration. Note that the length is as large as possible while (L <= r) {mid = (L + r)> 1ll; build (MID ); // composition int K = dinic (); // obtain the maximum stream if (sum = k) ans = mid, r = mid-1; else l = Mid + 1 ;} printf ("% LLD \ n", ANS);} int main () {// freopen ("poj_2391.txt", "r", stdin); int U, V; ll W; scanf ("% d", & F, & P); sum = 0; S = 0; t = 2 * F + 1; N = 2 * F + 2; for (INT I = 1; I <= f; I ++) {scanf ("% d", & cow [I], & BI [I]); sum + = cow [I];} memset (MP, INF, sizeof (MP); // set a value as big as possible, but it is not actually INF. memset is quite useful, and bytes are filled with for (INT I = 1; I <= P; I ++) {scanf ("% 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 ++) // note that you can use your own MP [I] [I] = 0; Floyd (); // calculate the solve (); Return 0 ;}
T has been deleted many times, and the STL is getting better and better. It is also drunk:
Run ID |
User |
Problem |
Result |
Memory |
Time |
Language |
Code Length |
Submit time |
13481032 |
Acmer |
2391 |
Accepted |
5112 K |
610 Ms |
G ++ |
3159b |
22:37:24 |
13480087 |
Acmer |
2391 |
Accepted |
5112 K |
625 Ms |
G ++ |
3203b |
17:43:11 |
13480059 |
Acmer |
2391 |
Accepted |
5136 K |
641 Ms |
G ++ |
3215b |
17:33:17 |
13480007 |
Acmer |
2391 |
Accepted |
5240 K |
704 Ms |
G ++ |
3199b |
17:20:58 |
13479803 |
Acmer |
2391 |
Accepted |
3344 K |
922 Ms |
G ++ |
2349b |
16:39:45 |
Poj 2391 ombrophobic bovines cows that do not like rain Floyd + binary enumeration + maximum flow