|
不难的一道网络流,因为为递增性,所以可以用二分,用二分还是相当的快,然后就是基本的建图,和sap了。
Ombrophobic Bovines
Time Limit: 1000MS | | Memory Limit: 65536K | Total Submissions: 10819 | | Accepted: 2419 |
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
|
1 #include <stdio.h>
2 #include <string.h>
3 #include <string>
4 #include <algorithm>
5 #include <iostream>
6 using namespace std;
7 #define INF 0x3ffffff
8 #define MAX 1000000000000000LL
9 #define N 440
10 #define M 10000000
11 struct node
12 {
13 int to,w,next;
14 }edge[M];
15
16 struct node1
17 {
18 int u,v;
19 __int64 w;
20 }g[1505];
21
22 int n,m;
23 int cnt,pre[N];
24 int s,t;
25 int savex[N],savey[N];
26 __int64 map[N][N];
27 int ans;
28 int lv[N],gap[N];
29 int nn;
30
31 void add_edge(int u,int v,__int64 w)
32 {
33 edge[cnt].to=v;
34 edge[cnt].w=w;
35 edge[cnt].next=pre;
36 pre=cnt++;
37 }
38
39 int sdfs(int k,int w)
40 {
41 if(k==t)
42 return w;
43 int f=0;
44 int mi=nn-1;
45 for(int p=pre[k];p!=-1;p=edge[p].next)
46 {
47 int v=edge[p].to;
48 if(edge[p].w!=0)
49 {
50 if(lv[k]==lv[v]+1)
51 {
52 int tmp=sdfs(v,min(w-f,edge[p].w));
53 f+=tmp;
54 edge[p].w-=tmp;
55 edge[p^1].w+=tmp;
56 if(f==w||lv==nn) return f;
57 }
58 if(lv[v]<mi) mi=lv[v];
59 }
60 }
61 if(f==0)
62 {
63 gap[lv[k]]--;
64 if( gap[lv[k]]==0 )
65 {
66 lv=nn;
67 return f;
68 }
69 lv[k]=mi+1;
70 gap[lv[k]]++;
71 }
72 return f;
73 }
74
75 int sap()
76 {
77 nn=2*n+2;
78 int sum=0;
79 memset(lv,0,sizeof(lv));
80 memset(gap,0,sizeof(gap));
81 gap[0]=nn;
82 while(lv<nn)
83 {
84 sum+=sdfs(s,INF);
85 }
86 return sum;
87 }
88
89 int check(__int64 mid)
90 {
91 cnt=0;
92 memset(pre,-1,sizeof(pre));
93 for(int i=1;i<=n;i++)
94 {
95 add_edge(s,i,savex);
96 add_edge(i,s,0);
97
98 add_edge(n+i,t,savey);
99 add_edge(t,n+i,0);
100
101 add_edge(i,n+i,INF);
102 add_edge(n+i,i,0);
103 }
104 for(int i=1;i<=n;i++)
105 for(int j=1;j<=n;j++)
106 {
107 if(map[j]<=mid && map[j]!=0&&map[j]<MAX&&i!=j) //
108 {
109 add_edge(i,n+j,INF);
110 add_edge(n+j,i,0);
111 }
112 }
113 if(sap()==ans)
114 return 1;
115 else return 0;
116 }
117
118 int main()
119 {
120 s=0;
121 scanf("%d%d",&n,&m);
122 t=2*n+1;
123 for(int i=1;i<=n;i++)
124 {
125 int x,y;
126 scanf("%d%d",&x,&y);
127 ans+=x;
128 savex=x;
129 savey=y;
130 }
131 for(int i=1;i<=n;i++)
132 for(int j=1;j<=n;j++)
133 if(i!=j)
134 {
135 map[j]=MAX; //
136 }
137 else
138 map[j]=0;
139
140 for(int i=0;i<m;i++)
141 {
142 scanf("%d%d%I64d",&g.u,&g.v,&g.w);
143 if(map[g.u][g.v]>g.w)
144 {
145 map[g.u][g.v] = map[g.v][g.u]=g.w;
146 }
147 }
148 for(int k=1;k<=n;k++)
149 for(int i=1;i<=n;i++)
150 for(int j=1;j<=n;j++)
151 {
152 if(map[j]>map[k]+map[k][j])
153 {
154 map[j]=map[k]+map[k][j];
155 }
156 }
157 __int64 b,d;
158 b=0;
159 d=50000001000000LL; // 最大的距离,1000000000*200
160 if(check(d)!=1)
161 {
162 printf("-1");
163 return 0;
164 }
165 while(b<d)
166 {
167 __int64 mid=(b+d)/2;
168 if(check(mid)==1)
169 {
170 d=mid;
171 }
172 else
173 {
174 b=mid+1;
175 }
176 }
177 printf("%I64d",b);
178 printf("\n");
179 return 0;
180 }
|
|