poj2749 Building roads(没事翻译一下吧)

译文

题目描述

农夫John的农场中有N个谷仓,每个谷仓都有一头奶牛。奶牛喜欢四处走动,因此John想建立一些道路来连接这些谷仓。如果ta为每一对不同的谷仓建造道路,那么ta必须建造N *(N-1)/ 2条道路,这是非常昂贵的,约翰肯定不会傻到要这么做,尽管这是奶牛的最佳选择。

聪明的John刚刚想出了一个好主意。ta首先建立两个中转站S1和S2,然后建立一条连接S1和S2的路,以及连接每个谷仓与S1或S2的N条道路的道路,即每个谷仓与S1或S2连接,但不会和两者都连接。这样每一对谷仓都将通过道路相连。为了让奶牛不会花费太多时间,约翰想要尽量减少每对谷仓之间的距离。

这还不是全部,因为还有另一个麻烦的问题。一些谷仓的牛互相讨厌,约翰无法将ta们的谷仓连接到同一个中转站。一些谷仓的奶牛是很好的朋友,约翰必须将ta们的谷仓连接到同一个中转站。真让人头疼!现在约翰向你寻求帮助。你的任务是找到一个可行的最佳道路建设方案,尽可能缩短每一对谷仓之间的距离,这意味着你必须决定每个谷仓应该连接到哪个中转站。

我们已经知道了S1,S2和N个谷仓的坐标,奶牛彼此讨厌的谷仓对,以及奶牛彼此交好的谷仓对。

请注意,约翰总是垂直和水平地建立道路,所以两个地方之间的道路长度是ta们的曼哈顿距离。例如,说坐标为(x1,y1)和(x2,y2)的两点,ta们之间的曼哈顿距离为| x1 - x2 | + | y1 - y2 |。

输入

第一行输入由3个整数N,A和B组成(2 <= N <= 500,0 <= A <= 1000,0 <= B <= 1000),分别表示谷仓的数量,两头奶牛相互讨厌的两个谷仓以及两头奶牛相互交好的谷仓对数量。

下一行包含4个整数sx1,sy1,sx2,sy2,分别表示两个不同中转站S1和S2的坐标。

以下N行中的每一行都包含两个整数x和y。表示从第一个谷仓到最后一个谷仓的坐标。

以下A行中的每一行都包含两个不同的整数i和j(1 <= i < j <= N),它们表示奶牛彼此讨厌的第i和第j个谷仓。同一对谷仓不会重复出现。

以下B行中的每一行都包含两个不同的整数i和j(1 <= i < j <= N),它们代表奶牛彼此为朋友的第i和第j个谷仓。同一对谷仓不会重复出现。

注意,所有的坐标都在[-1000000,1000000]的范围内。

输出

如果存在最佳道路建设方案,你只需输出包含单个整数的一行,该整数代表每对谷仓之间距离的最大值。注意如果没有可行的解决方案,只需输出-1。

样例输入

4 1 1
12750 28546 15361 32055
6706 3887
10754 8166
12668 19380
15788 16059
3 4
2 3

样例输出

53246

原文

Description

Farmer John’s farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some roads to connect these barns. If he builds roads for every pair of different barns, then he must build N * (N - 1) / 2 roads, which is so costly that cheapskate John will never do that, though that’s the best choice for the cows.

Clever John just had another good idea. He first builds two transferring point S1 and S2, and then builds a road connecting S1 and S2 and N roads connecting each barn with S1 or S2, namely every barn will connect with S1 or S2, but not both. So that every pair of barns will be connected by the roads. To make the cows don’t spend too much time while dropping around, John wants to minimize the maximum of distances between every pair of barns.

That’s not the whole story because there is another troublesome problem. The cows of some barns hate each other, and John can’t connect their barns to the same transferring point. The cows of some barns are friends with each other, and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible, which means that you must decide which transferring point each barn should connect to.

We have known the coordinates of S1, S2 and the N barns, the pairs of barns in which the cows hate each other, and the pairs of barns in which the cows are friends with each other.

Note that John always builds roads vertically and horizontally, so the length of road between two places is their Manhattan distance. For example, saying two points with coordinates (x1, y1) and (x2, y2), the Manhattan distance between them is |x1 - x2| + |y1 - y2|.

Input

The first line of input consists of 3 integers N, A and B (2 <= N <= 500, 0 <= A <= 1000, 0 <= B <= 1000), which are the number of barns, the number of pairs of barns in which the cows hate each other and the number of pairs of barns in which the cows are friends with each other.

Next line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively.

Each of the following N line contains two integer x and y. They are coordinates of the barns from the first barn to the last one.

Each of the following A lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows hate each other.

The same pair of barns never appears more than once.

Each of the following B lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows are friends with each other. The same pair of barns never appears more than once.

You should note that all the coordinates are in the range [-1000000, 1000000].

Output

You just need output a line containing a single integer, which represents the maximum of the distances between every pair of barns, if John selects the optimal road-building scheme. Note if there is no feasible solution, just output -1.

Sample Input

4 1 1
12750 28546 15361 32055
6706 3887
10754 8166
12668 19380
15788 16059
3 4
2 3

Sample Output

53246

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值