POJ 2391-ombrophobic bovines (Network flow _ maximum flow +floyd+ two points)

Source: Internet
Author: User

Ombrophobic bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15485 Accepted: 3361

Description

FJ ' s cows really hate getting wet so much then the mere thought of getting caught in the rain makes them shake in their ho Oves. They has 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, 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 The cows to get to some shelter.

The farm has an F (1 <= f <=) fields on which the cows graze. A set of P (1 <= p <=) paths connects them. The paths is wide, so and any number of cows can traverse a path in either direction.

Some of the farm ' s fields has rain shelters under which the cows can shield themselves. These shelters is of limited size, so a single shelter might is able to hold all the cows. Fields is 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 is sounded so, every cow can get to some she Lter.

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 this 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 a long any cow takes to traverse it.

Output

* Line 1:the Minimum amount of the time required for all cows to get under a shelter, presuming they plan their routes Optima Lly. If It isn't possible for the all of 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-time units, cows from field 1 can get under the shelter on 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 is other plans that would get all the cows under a shelter, none would do it in fewer than time units.

Test instructions: There is f block field, p path, point I has Ai cattle, point I at the barn can accommodate Bi cattle, a minimum time t so that in t time All cows can enter a cow.

Idea: Obviously modeling is from the source point s to connect each barn, the capacity for the current number of cows, and then from each point to connect a side to the meeting point T, capacity for each barn capacity, the shortest time to use is a binary search algorithm, the core is to enumerate the possible time and then the maximum flow, if the maximum flow result is exactly equal to the Then this time obviously in line with test instructions he recorded, we want to find the smallest time, so the time to small place adjustment, if not equal to the current time there is no cattle can go to meeting point, so the two points to the big place adjustment, so always two points down, here need to be split, a point I split into I and I+n, Then in every two minutes before the start of the network reconstruction, if the shortest path between I and I+n is equal to the current value, the description of the current time can go to the meeting point, that is, the barn can be filled, then connect an edge from the i->i+n capacity for infinity, after the diagram to find the maximum flow, Then add two points to compare and get the final result is the problem of the request

PS: Because of a DP initialization problem WA had a night, sad

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < algorithm> #include <set> #include <queue> #include <map>using namespace Std;const long Long maxxint =1e16;const int Inf=0x3f3f3f3f;int Head[100010],num[1010],d[1010],cur[1010],pre[10010],q[1010];long long dp[1010][    1010],n,sum,cnt,s,t,nv;int maxint=inf;struct Node {int u,v,cap; int next;}    Edge[10000010];struct filed {int x, Y,} p[1010];void Add (int u, int v, int cap) {edge[cnt].v=v;    Edge[cnt].cap=cap;    Edge[cnt].next=head[u];    head[u]=cnt++;    Edge[cnt].v=u;    Edge[cnt].cap=0;    EDGE[CNT].NEXT=HEAD[V]; head[v]=cnt++;}    void Floyd () {int I, j, K; For (k=1, k<=n; k++) for (I=1, i<=n; i++) for (j=1; j<=n; j + +) Dp[i][j]=min (dp[i][j ],DP[I][K]+DP[K][J]);}    void BFs () {memset (num,0,sizeof (num));    Memset (d,-1,sizeof (d));    int f1=0,f2=0,i;    q[f1++]=t;    Num[0]=1;    d[t]=0; while (F1&GT;=F2) {int u=q[f2++];            for (i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;            if (d[v]!=-1) continue;            d[v]=d[u]+1;            num[d[v]]++;        Q[f1++]=v;    }}}int Isap () {memcpy (cur,head,sizeof (cur));    int flow=0,u=pre[s]=s,i;    BFS ();            while (D[S]&LT;NV) {if (u==t) {int f=maxint, POS;                    for (i=s; i!=t; i=edge[cur[i]].v) {if (f>edge[cur[i]].cap) {f=edge[cur[i]].cap;                Pos=i;                }} for (i=s; i!=t; i=edge[cur[i]].v) {edge[cur[i]].cap-=f;            Edge[cur[i]^1].cap+=f;            } flow+=f;            if (flow>=sum) return flow;        U=pos; } for (i=cur[u]; i!=-1; i=edge[i].next) {if (D[edge[i].v]+1==d[u]&&edge[i].cap) BR        Eak            } if (i!=-1) {cur[u]=i;     Pre[edge[i].v]=u;       U=EDGE[I].V;            } else {if ((--num[d[u]) ==0) break;            int MIND=NV;                    for (i=head[u]; i!=-1; i=edge[i].next) {if (Mind>d[edge[i].v]&&edge[i].cap) {                    MIND=D[EDGE[I].V];                Cur[u]=i;            }} d[u]=mind+1;            num[d[u]]++;        U=pre[u]; }} return flow;}    int main () {Long long m,i,j;    Long Long a,b,c;    scanf ("%lld%lld", &n,&m);    sum=0;            For (i=0, i<=n; i++) for (j=0; j<=n; J + +) {if (i==j) dp[i][j]=0; Else dp[i][j]=maxxint;//because this place was initialized by the INF that caused WA a night, it should be initialized to Maxxint because the upper bound of the next two points is 2^11} for (I=1; i< =n;        i++) {scanf ("%d%d", &p[i].x,&p[i].y);    sum+=p[i].x;        } while (m--) {scanf ("%lld%lld%lld", &a,&b,&c);        if (dp[a][b]>c) {dp[a][b]=dp[b][a]=c;    }} Floyd ();    int x; Long Long Low=1,high=2000000000000,mid,ans=-1;        while (Low<=high) {mid= (High+low)/2;        cnt=0;        s=0;        t=2*n+1;        nv=t+1;        memset (head,-1,sizeof (head));            for (I=1; i<=n; i++) {Add (s,i,p[i].x);        Add (I+N,T,P[I].Y);                    } for (I=1, i<=n; i++) for (j=1; j<=n; J + +) {if (Dp[i][j]<=mid)            Add (I,j+n,inf);        } X=ISAP ();            if (x>=sum) {ans=mid;        High=mid-1;    } else low=mid+1;    } printf ("%lld\n", ans); return 0;}


POJ 2391-ombrophobic bovines (Network flow _ maximum flow +floyd+ two points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.