bzoj1738: [Usaco2005 mar]ombrophobic bovines Shivering Cow

Source: Internet
Author: User

It's not hard to write. The tune is 1hQAQ.

1. A baffling error. Do not put too much things in the main function of the writing, write a function of a more clear;

2.r=inf-1. If the =inf is clearly wrong.

And then because of these two reasons WA 1h. Sading ...

Oh, that's right. This is floyed+ two +maxflow. There is a Rauzi csdn introduce a lot of things ...

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define REP (i,n) for (int i=1;i<=n;i++) #define LL long long#define CLR (x,c) memset (x,c,sizeof (x)) int read () {int x= 0;char C=getchar (); bool F=true;while (!isdigit (c)) {if (c== '-') F=false;c=getchar ();} while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return f?x:-x;} const int Nmax=505;const int MAXN=100005;CONST ll inf=1e15;const int inf=0x7f7f7f7f;struct edge{int To,cap;edge *next,*re V;}; Edge Edges[maxn],*pt,*head[nmax],*cur[nmax],*p[nmax];int Cnt[nmax],h[nmax],a[nmax],b[nmax],f,sum=0;ll g[nmax][ nmax];void Add (int u,int v,int d) {pt->to=v;pt->cap=d;pt->next=head[u];head[u]=pt++;} void Adde (int u,int v,int d) {Add (u,v,d); add (v,u,0); head[u]->rev=head[v];head[v]->rev=head[u];} int maxflow (int s,int t,int n) {clr (cnt,0); cnt[0]=n;clr (h,0); CLR (cur,0); int Flow=0,a=inf,x=s;edge *e;while (h[s]<n) {for (E=cur[x];e;e=e->next) if (e->cap>0&&h[x]==h[e->to]+1) break;iF (e) {a=min (A,e->cap), p[e->to]=cur[x]=e,x=e->to;if (x==t) {while (x!=s) p[x]->cap-=a,p[x]->rev-> Cap+=a,x=p[x]->rev->to;flow+=a,a=inf;}} Else{if (!--cnt[h[x]]) break;h[x]=n;for (e=head[x];e;e=e->next) if (e->cap>0&&h[x]>h[e->to]+ 1) h[x]=h[e->to]+1,cur[x]=e;cnt[h[x]]++;if (x!=s) x=p[x]->rev->to;}} return flow;} ll find () {ll l=0,r=inf-1,m;int s=0,t=f+f+1,n=t+1;ll ans=-1;while (l<=r) {m= (l+r) >>1;pt=edges;clr (head,0); Rep (i,f) Adde (S,i,a[i]), Adde (I+f,t,b[i]), Rep (I,f) Rep (j,f) if (g[i][j]<=m) Adde (I,j+f,inf);/*for (int i=0;i<=n; i++) {for (Edge *o=head[i];o;o=o->next) printf ("%d%d\n", O->to,o->cap);p rintf ("\ n");} */int tmp;if ((Tmp=maxflow (s,t,n)) ==sum) R=m-1,ans=m;else l=m+1;//printf ("%lld%lld%lld:%d\n", l,r,m,tmp);} return ans;} int main () {f=read (); int p=read (), U,v;ll d;rep (i,f) A[i]=read (), B[i]=read (), Sum+=a[i];rep (i,f) Rep (j,f) G[i][j]=inf; Rep (i,f) g[i][i]=0;rep (i,p) U=read (), V=read (), D=read (), G[u][v]=g[v][u]=min (D,g[u][v]); Rep (K,F) Rep (i,f) Rep (j,f) if (g[i][k]!=inf&&g[k][j]!=inf) g[i][j]=min (G[i][j],g[i][k]+g[k][j]);p rintf ("%lld\n" , find ()); return 0;}

  

1738: [Usaco2005 mar]ombrophobic bovines shivering cow time limit:5 Sec Memory limit:64 MB
submit:367 solved:134
[Submit] [Status] [Discuss] 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.

John's cows were so frightened by the rain that they shivered. They are going to install a rain alarm and arrange a retreat plan.    They need to calculate the minimum time to get all the cows into the awning. The cows graze on the farm's F (1≤f≤200) field. There are P (1≤p≤1500) Two-way links to these fields. The road is wide and the unlimited number of cows can pass. There is a rain shed on the field, a certain capacity in the rain shed, and the cows can enter the rain shed from this field in a moment. Please calculate the minimum time for each cow to enter the canopy. 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.

Line 1th: Two integers f and p; 2nd to F+1: line I+l has two integers describing the field of I, the first one representing the number of cows on the field, and the second representing the canopy capacity on the field.    Two integers are between 0 and 1000. Line f+2 to F+p+i: three integers per line describes a route, which is the end point of the beginning, and the time required to pass the route (between 1 and 10^9). 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".

An integer that represents the minimum time. If you can't get all the cows into the canopy, output-1. Sample Input3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

Sample Output110

Of the 7 cattle in field 1th, 2 cattle went directly into the rain shed of field 1th and 4 cattle entered the canopy of the 1th field, and 1 went into the canopy of field 3rd and joined the other cattle from the field of No. 3rd. All cows are able to reach the shed within 110 units of time. HINT Source

Gold

[Submit] [Status] [Discuss]

bzoj1738: [Usaco2005 mar]ombrophobic bovines Shivering Cow

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.