BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路【MST】

1626: [Usaco2007 Dec]Building Roads 修建道路

Time Limit: 5 Sec Memory Limit: 64 MB

Description

Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场)。有些农场之间原本就有道路相连。 所有N(1 <= N <= 1,000)个农场(用1..N顺次编号)在地图上都表示为坐标为(X_i, Y_i)的点(0 <= X_i <= 1,000,000;0 <= Y_i <= 1,000,000),两个农场间道路的长度自然就是代表它们的点之间的距离。现在Farmer John也告诉了你农场间原有的M(1 <= M <= 1,000)条路分别连接了哪两个农场,他希望你计算一下,为了使得所有农场连通,他所需建造道路的最小总长是多少。

Input

  • 第1行: 2个用空格隔开的整数:N 和 M
    • 第2..N+1行: 第i+1行为2个用空格隔开的整数:X_i、Y_i * 第N+2..N+M+2行: 每行用2个以空格隔开的整数i、j描述了一条已有的道路, 这条道路连接了农场i和农场j

Output

  • 第1行: 输出使所有农场连通所需建设道路的最小总长,保留2位小数,不必做 任何额外的取整操作。为了避免精度误差,计算农场间距离及答案时 请使用64位实型变量

Sample Input

4 1
1 1
3 1
2 3
4 3
1 4
输入说明:
FJ一共有4个坐标分别为(1,1),(3,1),(2,3),(4,3)的农场。农场1和农场
4之间原本就有道路相连。

Sample Output

4.00
输出说明:
FJ选择在农场1和农场2间建一条长度为2.00的道路,在农场3和农场4间建一
条长度为2.00的道路。这样,所建道路的总长为4.00,并且这是所有方案中道路
总长最小的一种。

题解

水题,最小生成树模板题,一开始有几条边是连起来的,所以先处理一趟,然后MST就可以了。

代码如下

#include<cmath>
#include<cstdio>
#include<algorithm>
#define LD double
using namespace std;
int n,m,cnt,Now,fa[1005],X[1005],Y[1005];
double Ans;
struct xcw{int x,y;double Len;bool operator <(const xcw b)const{return Len<b.Len;};}a[1005*1005];
double Get(int i,int j){return sqrt(((LD)X[i]-X[j])*(X[i]-X[j])+((LD)Y[i]-Y[j])*(Y[i]-Y[j]));}
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) fa[i]=i,scanf("%d%d",&X[i],&Y[i]);
    for(int i=1;i<=n;i++)
    for(int j=1;j<i;j++) a[++Now]=(xcw){i,j,Get(i,j)};
    for(int i=1;i<=m;i++){
        int x,y;scanf("%d%d",&x,&y);
        x=get(x),y=get(y);
        if(x^y) fa[y]=x,cnt++;
    }
    sort(a+1,a+1+Now);
    for(int i=1;i<=Now;i++){
        if(cnt==n-1) break;
        int x=a[i].x,y=a[i].y;
        x=get(x),y=get(y);
        if(x^y) fa[y]=x,Ans+=a[i].Len,cnt++;
    }
    printf("%.2lf",Ans);
    return 0;
}
阅读更多

Building roads

12-23

DescriptionnnFarmer 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. nnClever 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. nnThat'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. nnWe 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. nnNote 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|. nInputnnThe 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. nnNext line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively. nnEach 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. nnEach 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. nnThe same pair of barns never appears more than once. nnEach 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. nnYou should note that all the coordinates are in the range [-1000000, 1000000]. nOutputnnYou 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.nSample Inputnn4 1 1n12750 28546 15361 32055n6706 3887n10754 8166n12668 19380n15788 16059n3 4n2 3nSample Outputnn53246

没有更多推荐了,返回首页