您的位置:首页 > 运维架构

POJ 2391 Ombrophobic Bovines (二分+最短路+最大流)

2013-05-06 14:38 441 查看
Ombrophobic Bovines

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 11468Accepted: 2546
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

此题调了一天-,- 一开始考虑到直接对路径长度进行二分查找,最坏情况的上界是1,000,000,000,中间可能很多不必要的查找。所以选择离散化,把所有满足的路径去掉重复的,然后从小到大排序,然后对路径编号进行二分。但不知为何WA了。老老实实换回直接查找路径长度,就AC了。此题注意路径会超int就没什么了。

 

//POJ 2391
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 512
#define inf 0xfffffff

using namespace std;

typedef __int64 Int;

struct node
{
int to,next,val;
}edge[SIZE*1000];

int F,P,sc,sk,pt;  //输入的F,P;sc,sk,pt分别是源点,汇点和总点数
Int map[SIZE][SIZE],cow[SIZE],shelter[SIZE]; //道路的链接情况,每个点的牛数,容纳量
Int maxi;
int num_sum,vol_sum;
int head[SIZE],idx;
int gap[SIZE],dis[SIZE];

void addnode(int from,int to,int val)
{
edge[idx].to = to;
edge[idx].val = val;
edge[idx].next = head[from];
head[from] = idx ++;
edge[idx].to = from;
edge[idx].val = 0;
edge[idx].next = head[to];
head[to] = idx ++;
}

void floyd()
{
for(int i=1; i<=F; i++)
for(int j=1; j<=F; j++)
for(int k=1; k<=F; k++)
{
if(map[j][i] == -1 || map[i][k] == -1)
continue;
if(map[j][k] > map[j][i] + map[i][k] || map[j][k] == -1)
{
map[j][k] = map[j][i] + map[i][k];
maxi = max(maxi,map[j][k]);
}
}
}

int dfs(int cur,int cval)
{
if(cur == sk) return cval;
int mindis = pt - 1, tval = cval;
for(int i=head[cur]; i!=-1; i=edge[i].next)
{
int to = edge[i].to;
if(edge[i].val > 0)
{
if(dis[to] + 1 == dis[cur])
{
int val = dfs(to,min(edge[i].val,tval));
tval -= val;
edge[i].val -= val;
edge[i^1].val += val;
if(dis[sc] >= pt)
return cval-tval;
if(tval == 0)
break;
}
if(dis[to] < mindis)
mindis = dis[to];
}
}
if(cval == tval)
{
--gap[dis[cur]];
if(!gap[dis[cur]])
dis[sc] = pt;
dis[cur] = mindis + 1;
++gap[dis[cur]];
}
return cval-tval;
}

bool sap()
{
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
int ret = 0;
gap[sc] = pt;
while(dis[sc] < pt)
ret += dfs(sc,inf);
return ret >= num_sum? true:false;
}

void construct(Int m) //构图
{
idx = 0;
memset(head,-1,sizeof(head));
for(int i=1; i<=F; i++)
addnode(sc,i,cow[i]); //源点与每个点连边,权值为每个点初始的牛的数量
for(int i=1; i<=F; i++)
addnode(F+i,sk,shelter[i]); //拆点,每个拆除来的点与汇点连边,权值为点的容纳量
for(int i=1; i<=F; i++)
{
addnode(i,i+F,inf); //点与拆出来的点连边
for(int j=1; j<=F; j++)
{
if(map[i][j] <= m && i!=j && map[i][j] !=-1) //满足二分结果的边连起来
{
addnode(i,j+F,inf);
}
}
}
}

void binarySearch()
{
Int low = 0, high = maxi;
Int ans = -1;
while(low < high)
{
Int mid = (low + high) >> 1;
construct(mid);
if(sap())
ans = high = mid;
else
low = mid + 1;
}
printf("%I64d\n",ans);
}

int main()
{
scanf("%d%d",&F,&P);
int num,vol;
num_sum = 0;
vol_sum = 0;
maxi = -1;
for(int i=1; i<=F; i++)
{
scanf("%d%d",&num,&vol);
cow[i] = num;
shelter[i] = vol;
num_sum += num;
vol_sum += vol;
}
int s,e;
Int v;
for(int i=1; i<=F; i++)
for(int j=1; j<=F; j++)
map[i][j] = (i == j)?0:(-1);
for(int i=1; i<=P; i++)
{
scanf("%d%d%I64d",&s,&e,&v);
if(v < map[s][e] || map[s][e] == -1)
{
map[s][e] = map[e][s] = v;
maxi = max(maxi,v);
}
}
if(num_sum > vol_sum) //初始牛数比容纳量大,直接输出-1
puts("-1");
else
{
floyd(); //求最短路
sc = 0, sk = F+F+1, pt = sk+1;
maxi += 1;
binarySearch();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: