您的位置:首页 > 理论基础 > 计算机网络

POJ2391解题报告

2015-05-18 19:27 681 查看
Ombrophobic Bovines

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 16138 Accepted: 3515

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

Solution:

膜拜杜大神的题解,我就当萌萌哒的链接君吧~

Code:

#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define maxn 500 + 10
#define maxm 250000 + 10
#define INF 0x7f7f7f7f
#define INF2 20000000000000LL
typedef long long LL;
struct edge{
int to, next, cap;
}e[maxm];
int f, p, s = 0, t, cur = 1, que[maxn], front[maxn], h[maxn];
int mxflow = 0, mxcow = 0, mxsh = 0, sh[maxn], cow[maxn];
LL d[maxn][maxn], lim;
inline void addedge(int u, int v,int c){
e[++ cur].cap = c; e[cur].to = v; e[cur].next = front[u]; front[u] = cur;
e[++ cur].cap = 0; e[cur].to = u; e[cur].next = front[v]; front[v] = cur;
}
inline bool bfs(){
int x, d = 1, tp = 0;
memset(h, -1, sizeof(h));
que[tp] = h[0] = 0;
while(tp < d){
x = que[tp]; tp ++;
int now = front[x];
while(now){
if(e[now].cap && h[e[now].to] < 0){
que[d ++] = e[now].to;
h[e[now].to] = h[x] + 1;
}
now = e[now].next;
}
}
return h[t] == -1 ? 0 : 1;
}
inline int dfs(int x, int min_adv){
if(x == t) return min_adv;
int now = front[x], flow = 0, f;
while(now){
if(e[now].cap && h[e[now].to] == h[x] + 1){
f = min_adv - flow;
f = dfs(e[now].to, min(e[now].cap, f));
e[now].cap -= f;
e[now ^ 1].cap += f;
flow += f;
if(flow == min_adv) return min_adv;
}
now = e[now].next;
}
if(!flow) h[x] = -1;
return flow;
}
inline void buildg(){
cur = 1; memset(front, 0, sizeof(front));
for(int i = 1; i <= f; i ++){
addedge(s, i, cow[i]);
addedge(f + i, t, sh[i]);
for(int j = 1; j <= f; j ++)
if(d[i][j] <= lim) addedge(i, f + j, INF);
}
}
inline void dinic(){
buildg(); mxflow = 0;
while(bfs()) mxflow += dfs(0, INF);
}
inline void floyd(){
for(int k = 1; k <= f; k ++)
for(int i = 1; i <= f; i ++)
for(int j = 1; j <= f; j ++)
if(d[i][k] + d[k][j] < d[i][j])
d[i][j] = d[i][k] + d[k][j];
}
inline void solve(){
LL l = 0, r = INF2, mid = l + r >> 1;
while(l < r){
lim = mid; dinic();
if(mxflow == mxcow) r = mid, mid = l + r >> 1;
else l = mid + 1, mid = l + r >> 1;
}
if(r != INF2) printf("%lld\n",r);
else printf("-1\n");
}
int main(){
#ifndef ONLINE_JUDGE
freopen("2391.in","r",stdin);
#endif
scanf("%d%d",&f,&p); t = 2*f + 1;
for(int i = 1; i <= f; i ++){
scanf("%d%d",&cow[i], &sh[i]);
mxcow += cow[i]; mxsh += sh[i];
}
for(int i = 1; i <= f; i ++)
for(int j = 1; j <= f; j ++)
d[i][j] = (i == j) ? 0 : INF2;
for(int i = 1; i <= p; i ++){
int u,v,time;scanf("%d%d%d",&u,&v,&time);
if(time < d[u][v]) d[u][v] = time, d[v][u] = time;
}
if(mxsh < mxcow){printf("-1\n"); return 0;}
floyd();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}


杜大神的网络流解题经验:

1、网络流题大多数的主体都是dinic算法+建图过程,至于求解还要加一些特技,因此每次写完dinic一定要做检查;

2、网络流题常见错误:

WA:如果主算法没写错通常是INF值开小了;

TLE&RE:通常是边数和点数开小了;

感觉自己的网络流水平还是太Naive,因此还要多加练习才行;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二分 网络流