解题报告 之 POJ2391 Ombrophobic Bovines

解题报告 之 POJ2391 Ombrophobic Bovines


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


题目大意:有n个棚,有m条路,每个棚紧挨着一些牛,每个棚有一定容量。每条路有一个长度,每条路可以同时跑无数条牛。问要让所有牛有棚住,牛中移动距离的最长路最小能有多小?


分析:与前面的题类似,先跑Floyd,然后都是二分,然后跑最大流,直到满足为止。但是这个题建图有一个很困难的地方。就是拆点及拆点的作用。这里把每个棚拆成两个点,负载为INF,关键在于如果两个点u->v可达且路径长度<=mid,那么此时在u的入点连一条边到v的出点。为什么那么奇葩呢,因为转移的量只能转移一次,如果不拆点流量可能转移多次,那么总路径长度将会大于mid。好好体会一下!


另外不知道为什么二分用 while(low<high){ if(...) low=mid+1;else high=mid} 输出low不行,而用ans记录法(见代码)就可以,求大神解答,叩谢!


上代码


<span style="font-size:18px;">#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

const int MAXN = 510;
const int MAXM = 330110;
const int inf = 0x3f3f3f3f;
const long long INF = 1e16;

struct Edge
{
	int to, cap, next;
};

Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int cow[MAXN];
int hold[MAXN];
long long dist[MAXN][MAXN];
int src, des, cnt;

void addedge( int from, int to, int cap )
{
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	edge[cnt].to = from;
	edge[cnt].cap = 0;
	edge[cnt].next = head[to];
	head[to] = cnt++;
}

int bfs( )
{
	queue<int> q;
	while (!q.empty( ))
		q.pop( );
	memset( level, -1, sizeof level );
	level[src] = 0;
	q.push( src );

	while (!q.empty( ))
	{
		int u = q.front( );
		q.pop( );
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;

			if (edge[i].cap > 0 && level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push( v );
			}
		}
	}
	return level[des] != -1;
}


int dfs( int u, int f )
{
	if (u == des)	return f;
	int tem;
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cap > 0 && level[v] == level[u] + 1)
		{
			tem = dfs( v, min( f, edge[i].cap ) );
			if (tem > 0)
			{
				edge[i].cap -= tem;
				edge[i ^ 1].cap += tem;
				return tem;
			}
		}
	}
	level[u] = -1;
	return 0;
}


int Dinic( )
{
	int ans = 0, tem;

	while (bfs( ))
	{
		while (tem = dfs( src, INF ))
		{
			ans += tem;
		}
	}
	return ans;
}


int main( )
{
	int n, m;
	while (cin >> n >> m)
	{

		int cows = 0;
		memset( dist, -1, sizeof dist );
		src = 0, des = 501;
		for (int i = 1; i <= n; i++)
		{
			scanf( "%d%d", &cow[i], &hold[i] );
			cows += cow[i];
		}

		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
				dist[i][j] = INF;
		}

		for (int i = 1; i <= m; i++)
		{
			int a, b, dis;
			scanf( "%d%d%d", &a, &b, &dis );
			if (dis < dist[a][b])
				dist[a][b] = dist[b][a] = dis;
		}



		//Floyd
		for (int k = 1; k <= n; k++)
		for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		if (dist[i][j] > dist[i][k] + dist[k][j])
		{
			dist[i][j] = dist[i][k] + dist[k][j];
		}


		long long low = -1, high = INF - 1;
		long long ans = -1;
		while (low <= high)
		{
			long long  mid = (low + high) / 2;
			memset( head, -1, sizeof head );
			cnt = 0;
			for (int i = 1; i <= n; i++)
			{
				addedge( src, i, cow[i] );
				addedge( i + 250, des, hold[i] );
				addedge( i, i + 250, hold[i] );
			}

			for (int i = 1; i <= n; i++)
			{
				for (int j = i + 1; j <= n; j++)
				{
					if (dist[i][j] <= mid)
					{
						addedge( i, j + 250, inf );
						addedge( j, i + 250, inf );
					}
				}
			}

			if (Dinic( ) ==cows)
			{
				ans = mid;
				high = mid-1;
			}
			else low = mid+1;
		}
		cout << ans << endl;
	}

	return 0;
}</span>

今天累了,,洗洗睡! 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1753题目为"Flip Game",题目给出了一个4x4的棋盘,每个格子有黑色或白色,每次翻转一个格子会同时翻转它上下左右四个格子的颜色,目标是把整个棋盘都变为同一种颜色,求把棋盘变成同种颜色的最小步数。 解题思路: 一般关于棋盘变色的题目,可以考虑使用搜索来解决。对于POJ1753题目,可以使用广度优先搜索(BFS)来解决。 首先,对于每个格子,定义一个状态,0表示当前格子是白色,1表示当前格子是黑色。 然后,我们可以把棋盘抽象成一个长度为16的二进制数,将所有格子的状态按照从左往右,从上往下的顺序排列,就可以用一个16位的二进制数表示整个棋盘的状态。例如,一个棋盘状态为: 0101 1010 0101 1010 则按照从左往右,从上往下的顺序把所有格子的状态连接起来,即可得到该棋盘的状态为"0101101001011010"。 接着,我们可以使用队列来实现广度优先搜索。首先将初始状态加入队列中,然后对于队列中的每一个状态,我们都尝试将棋盘上的每个格子翻转一次,生成一个新状态,将新状态加入队列中。对于每一个新状态,我们也需要记录它是从哪个状态翻转得到的,以便在得到最终状态时能够输出路径。 在搜索过程中,我们需要维护每个状态离初始状态的步数,即将该状态转换为最终状态需要的最小步数。如果我们找到了最终状态,就可以输出答案,即最小步数。 代码实现:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值