POJ -2391 Ombrophobic Bovines (二分+Floyd+网络流)

Ombrophobic Bovines

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21425   Accepted: 4593

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

题意:

有n块草地 , 每块草地上奶牛的数量和避雨点能遮蔽牛的数量

有m条无向边连接任意两块草地, 每条路有固定长度,问如果下雨了, 所有的牛要怎么走,才能在最短的时间内

让所有的牛走到避雨点,  如果不能的话输出-1;   问最短时间.

思路:

以草地为节点, 道路为边,  边长为道路的长度,.

用Floyd的算法计算任意两点之间的最短距离, 然后二分答案,  跑网络流 , 流量为牛的数量

如果最大流 >= 牛的数量, 说明时间可以在缩短些, 否则增大.

网络流构造图: 

超级源点向 没给草地之间  流量为 cow[i] 

每个草地虚拟出另外个点   i'   流量为 inf 

草地 i 和 虚拟草地 j'  之间 流量为 dis(i,j');

虚拟草地 到 超级汇点 之间 流量为 shelter[i]   (可遮蔽的牛的数量)

注意  数据 会爆 int  用 long long 

[代码]

//#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <string.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)

typedef long long ll;
const int maxn = 1e5+10;
const int mod =1e9+7;
const int inf = 0x3f3f3f3f;
const ll INF = 1e16;
using namespace std;
const int NMAX = 420;
const int EMAX = 82000;


int n,m;
int ne ,head[maxn];
int num[maxn],start,END,cnt,sum;
int cow[NMAX],shelter[NMAX];
ll mp[NMAX][NMAX];

struct node{
    int v,w,next; //u  v 从 u-v 权值为w
}edge[maxn];

void init()
{
    cnt = 0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
	/*
	根据题意建立有向图或无向图, 有向图反路0
	无向图反路一样
	*/
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;

    edge[cnt].v = u;
    edge[cnt].w = 0;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
int bfs()
{
    queue<int>Q;
    memset(num,0,sizeof(num));
    num[start] = 1;
    Q.push(start);
    while(!Q.empty())
    {
        int t = Q.front();
        Q.pop();
        if(t==END)
            return 1;
        for(int i=head[t];i!=-1;i=edge[i].next)// 链式前向星访问找增广路
        {
            int t1 = edge[i].v;//下一个节点
            int t2 = edge[i].w;// 当前点 的流
            if(t2&&num[t1]==0)// 当前点存在 并且下一个点没有访问
            {
                num[t1]=num[t]+1;// 点=1
                if(t1==END)//  结束
                    return 1;// 存在
                Q.push(t1);
            }
        }
    }
    return 0;
}
int dfs(int u,int maxflow)
{
    if(u==END)
        return maxflow;
    int res = 0;  //从当前u点流出的流量
    for(int i = head[u];i!=-1;i = edge[i].next)
    {
        int t1 = edge[i].v;// 下一个节点
        int t2 = edge[i].w;// 当前节点的流
        if(t2&&num[t1] == num[u]+1)
        {
            int temp = dfs(t1,min(maxflow-res,t2));// 选择流 小的一部分
            edge[i].w-=temp;// 正向减少
            edge[i^1].w+=temp;//反向增加
            res += temp;
            if(res==maxflow)
                return res;//已达到祖先的最大流,无法再大,剪枝
        }
    }
    if(!res)
        num[u] = -1;//此点已无流,标记掉
    return res;
}
ll Dinic(int s,int ed)
{
    ll ans = 0;
    while(bfs()) //有增广路
    {
        ans+=dfs(s,inf);
    }
    return ans;
}

void floyd()
{
	for(int  k = 1; k <= n ;k++)
	{
		for(int  i = 1 ; i <= n;i++)
		{
			for(int j = 1 ;j <= n;j++)	
			{ 
				if( mp[i][k] + mp[k][j] <  mp[i][j])
				{
					mp[i][j]  = mp[i][k] + mp[k][j];
				}
			}
		}
	}
}
int main(int argc, char const *argv[])
{
	int u,v;
	ll sum,w;
	while(~scanf("%d %d",&n,&m))
	{
		END = 2*n+1;start = 0;
		
		for(int i = 1 ;i <= n ; i++)
			for(int j = 1 ;j <= n ; j++)
					mp[i][j] = INF;
		sum =  0;
		for(int i = 1; i <= n ; i++)
		{
			scanf("%d %d",&cow[i],&shelter[i]);
			sum += cow[i];
		}
		
		while(m--)
		{
			scanf("%d %d %lld",&u,&v,&w);
			if( w < mp[u][v] )
			{
				mp[u][v] = mp[v][u] = w;
			}
		}
		floyd();

		ll mid ,low = 0, high = INF-1 ,ans = -1;
		
		while( low < high)
		{
			mid = (low + high )>>1;
			init();
			for(int i = 1 ;i <= n;i++)
			{
				addedge(0,i,cow[i]);
				addedge(i,i+n,inf);
				addedge(i+n,2*n+1,shelter[i]);
				for( int j = i+ 1 ; j <= n ;j++)
				{
					if(mp[i][j] <= mid)
					{
						addedge(i,j+n,inf);
						addedge(j,i+n,inf);
					}
				}
			}
			ll te = Dinic(0,2*n+1);
			//cout<<te<<" Dinic "<<sum<<" "<<mid<<endl;
			if(  te>= sum)
			{
				ans = mid;
				high = mid;
			}
			else 
				low = mid + 1;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*
	3 4
	7 2
	0 4
	2 6
	1 2 40
	3 2 70
	2 3 90
	1 3 120
 */
发布了372 篇原创文章 · 获赞 89 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/83110155