1. 程式人生 > >POJ -2391 Ombrophobic Bovines (二分+Floyd+網路流)

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

題意:

有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
 */