POJ2391

Ombrophobic Bovines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16007   Accepted: 3485

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.

二分 + 最大流 + 拆點 + 最短路

題目大意爲給你F個點P條邊,經過每條邊需要一定時間。每個點開始有若干奶牛,每個點最後能容納若干奶牛,於是我們可能要將奶牛移動(全部同時移動)。求最少需要多少時間轉移所有奶牛。

首先遇到這種路徑最值問題,我們可以考慮二分答案Limit.

用FLoyd求出兩點間最短路。

考慮到目標是將奶牛從一個點移動到另一個,我們將每個點拆分成兩個,一個點代表初始的奶牛數,另一個代表最後能容納的奶牛數。

將S點與前一個點連邊,容量爲初始奶牛數;將另一個店與T連邊,容量爲最大容量;若兩點之間的距離小於Limit,則將其中一點拆分出的左點與另一點拆分出的右點連邊。

若該網絡的最大流 = 初始奶牛數之和 則說明最遠的路程 <= Limit。

PS: 最短路可能很長,要用longlong

代碼如下:


/* 
* @Author: duyixian
* @Date:   2015-04-20 20:20:47
* @Last Modified by:   duyixian
* @Last Modified time: 2015-04-21 11:51:32
*/

#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "algorithm"
#include "queue"
#include "cstring"

using namespace std;

#define MAX_SIZE 505
#define INF 10000000000000LL

struct Edge
{
	int To, Next, C;
}Edges[MAX_SIZE * MAX_SIZE * 4];

long long Shortest[MAX_SIZE][MAX_SIZE], Limit;
int Front[MAX_SIZE], Distance[MAX_SIZE], Begin[MAX_SIZE], End[MAX_SIZE];
int S, T, Total, F, P, Max;

inline void Add_Edge(int From, int To, int C)
{
	++Total;
	Edges[Total].To = To;
	Edges[Total].C = C;
	Edges[Total].Next = Front[From];
	Front[From] = Total;
}

inline void Add_Edges(int From, int To, int C)
{
	Add_Edge(From, To, C);
	Add_Edge(To, From, 0);
}

inline bool BFS()
{
	memset(Distance, 0, sizeof(Distance));
	Distance[S] = 1;
	queue<int> Queue;
	Queue.push(S);
	while(!Queue.empty())
	{
		int Now = Queue.front();
		Queue.pop();
		for(int temp = Front[Now]; temp; temp = Edges[temp].Next)
		{
			if(Edges[temp].C && !Distance[Edges[temp].To])
			{
				Distance[Edges[temp].To] = Distance[Now] + 1;
				Queue.push(Edges[temp].To);
			}
		}
	}
	return Distance[T];
}

int DFS(int Now, int In)
{
	if(Now == T)
		return In;
	int Rest = In;
	for(int temp = Front[Now]; temp; temp = Edges[temp].Next)
	{
		if(Edges[temp].C && Distance[Edges[temp].To] == Distance[Now] + 1)
		{
			int Increment;
			Increment = DFS(Edges[temp].To, min(Rest, Edges[temp].C));
			Rest -= Increment;
			Edges[temp].C -= Increment;
			Edges[temp ^ 1].C += Increment;
			if(!Rest)
				return In;
		}
	}
	if(Rest == In)
		Distance[Now] = 0;
	return In - Rest;
}

void Floyd()
{
	for(int k = 1; k <= F; ++k)
		for(int i = 1; i <= F; ++i)
			for(int j = 1; j <= F; ++j)
				if(Shortest[i][k] + Shortest[k][j] < Shortest[i][j])
					Shortest[i][j] = Shortest[i][k] + Shortest[k][j];
}

inline void Build()
{
	memset(Front, 0, sizeof(Front));
	Total = 1;
	S = 0;
	T = 2 * F + 1;
	for(int i = 1; i <= F; ++i)
	{
		Add_Edges(S, i, Begin[i]);
		Add_Edges(i + F, T, End[i]);
		for(int j = 1; j <= F; ++j)
			if(Shortest[i][j] <= Limit)
			{
				Add_Edges(i, j + F, INF);
			}
	}
}

inline int Max_Flow()
{
	int Ans = 0;
	Build();
	while(BFS())
	{
		Ans += DFS(S, INF);
	}
	return Ans;
}

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w",stdout);
	cin >> F >> P;
	int Rt = 0;
	for(int i = 1; i <= F; ++i)
	{
		scanf("%d%d", &Begin[i], &End[i]);
		Max += Begin[i];
		Rt += End[i];
	}
	for(int i = 1; i <= F; ++i)
		for(int j = 1; j <= F; ++j)
			if(i != j)
				Shortest[i][j] = INF;
	for(int i = 1; i <= P; ++i)
	{
		long long temp1, temp2, temp3;
		scanf("%lld%lld%lld", &temp1, &temp2, &temp3);
		if(temp3 < Shortest[temp1][temp2])
			Shortest[temp1][temp2] = Shortest[temp2][temp1] = temp3;
	}
	Floyd();
	long long Left = 0, Right = 10000000000000LL, Mid = (Left + Right) / 2;
	while(Mid != Left && Mid != Right)
	{
		int Flow;
		Limit = Mid;
		Flow = Max_Flow();
		if(Flow == Max)
		{
			Right = Mid;
			Mid = (Left + Right) / 2;
		}
		else
		{
			Left = Mid;
			Mid = (Left + Right) / 2;
		}
	}
	if(Right != 10000000000000LL)
		cout << Right << endl;
	else
		cout << -1 << endl;
	fclose(stdout);
	fclose(stdin);
	return 0;
}



發佈了43 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章