POJ3625 Building Roads

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10803   Accepted: 3062

Description

Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.

Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Two space-separated integers: Xand Y
* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.

Output

* Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.

Sample Input

4 1
1 1
3 1
2 3
4 3
1 4

Sample Output

4.00

Source

USACO 2007 December Silver

kruskal最小生成树

先处理出每两个点之间的距离,加入到边集。读入的已连接的点对也加入边集,距离为0。跑一遍kruskal出解

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=1020;
 9 int n,m;
10 struct point{
11     double x,y;
12 }p[mxn];
13 struct edge{
14     int x,y;
15     double dis;
16 }e[mxn*mxn];
17 int cmp(edge a,edge b){
18     return a.dis<b.dis;
19 }
20 double dist(point a,point b){
21     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
22 }
23 //
24 int fa[mxn];
25 int find(int x){
26     if(fa[x]==x)return x;
27     return fa[x]=find(fa[x]);
28 }
29 int cnt=0;
30 double ans=0;
31 void kruskal(){
32     int now=0;
33     for(int i=1;i<=cnt && now<n-1;i++){
34         int x=find(e[i].x);
35         int y=find(e[i].y);
36         if(x!=y){
37             ans+=e[i].dis;
38             fa[x]=y;now++;
39         }
40     }
41     printf("%.2f\n",ans);
42     return;
43 }
44 int main(){
45     scanf("%d%d",&n,&m);
46     int i,j;
47     for(i=1;i<=n;i++) fa[i]=i;
48     for(i=1;i<=n;i++)
49         scanf("%lf%lf",&p[i].x,&p[i].y);
50     for(i=1;i<n;i++)
51       for(j=i+1;j<=n;j++){
52           e[++cnt].dis=dist(p[i],p[j]);
53           e[cnt].x=i;e[cnt].y=j;
54       }
55     for(i=1;i<=m;i++){
56         cnt++;
57         scanf("%d%d",&e[cnt].x,&e[cnt].y);
58         e[cnt].dis=0;
59     }
60     sort(e+1,e+cnt+1,cmp);
61     kruskal();
62     return 0;
63 }
时间: 2024-10-14 13:28:42

POJ3625 Building Roads的相关文章

[BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1730  Solved: 727 [Submit][Status][Discuss] Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场之间原本就有道路相连.

hdu 1815 Building roads

Building roads Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 822    Accepted Submission(s): 239 Problem Description Farmer John's farm has N barns, and there are some cows that live in each b

poj 3625 Building Roads

题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already conn

洛谷——P2872 [USACO07DEC]道路建设Building Roads

P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms

Building roads

Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34 Accepted Submission(s): 13   Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn.

HDU 1815, POJ 2749 Building roads(2-sat)

HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得任意牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2. 有a对牛棚互相有仇恨,所以不能让他们的路连接到同一个中转站.还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站. 道路的长度是两点的曼哈顿距离. 问最小的任意两牛棚间的距离中的最大值是多少? 思路:二分距离,考虑每两个牛棚之间4种连

poj 2749 Building roads (二分+拆点+2-sat)

Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 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 ro

bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

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,

BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

计算距离时平方爆了int结果就WA了一次...... ----------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<cmath