USACO 2007 Dec Silver 2.Building Roads
做了 第一道 最小生成樹 hehe
Time Limit: 1 Sec Memory Limit: 64 MB
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 (X_i, Y_i) on the plane (0 <= X_i <=
1,000,000; 0 <= Y_i <= 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: X_i and Y_i
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
HINT
INPUT DETAILS:
Four farms at locations (1,1), (3,1), (2,3), and (4,3). Farms 1 and 4 are
connected by a road.
OUTPUT DETAILS:
Connect farms 1 and 2 with a road that is 2.00 units long, then connect
farms 3 and 4 with a road that is 2.00 units long. This is the best we can
do, and gives us a total of 4.00 unit lengths.
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<functional>
using namespace std;
typedef long long ll;
int n,m;
struct node
{
int from,to;
double val;
}edge[1000006];
int cnt;
int pos[1003][2];
bool cmp(node x,node y)
{
return x.val<y.val;
}
int fa[1003];
int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
void unnion(int x,int y)
{
x=find(x),y=find(y);
fa[x]=y;
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) fa[i]=i;
for(i=1;i<=n;i++)
{
scanf("%d%d",&pos[i][0],&pos[i][1]);
for(j=1;j<i;j++)
{
edge[++cnt].from=i;
edge[cnt].to=j;
ll p=pos[i][0]-pos[j][0];
ll q=pos[i][1]-pos[j][1];
edge[cnt].val=sqrt(p*p+q*q);
}
}
int kkk=0;
for(i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
unnion(x,y);
kkk++;
}
sort(edge+1,edge+cnt+1,cmp);
double sum=0.0;
for(i=1;i<=cnt;i++)
{
if(kkk==n-1) break;
if(find(edge[i].from)!=find(edge[i].to))
{
sum+=edge[i].val;
kkk++;
unnion(edge[i].from,edge[i].to);
}
}
printf("%.2lf",sum);
return 0;
}
/*
4 1
1000000 1000000
3000000 1000000
2000000 3000000
4000000 3000000
1 4
*/
相關文章
- BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(最小生成樹)UI
- [USACO23DEC] Minimum Longest Trip G3D
- kedaOJ-#P2574. [USACO 21DEC.B] Lonely Photo
- P2854 [USACO06DEC] Cow Roller Coaster SAST
- P5836 [USACO19DEC] Milk Visits S
- P8906 [USACO22DEC] Breakdown P [最短路]
- BZOJ4742 : [Usaco2016 Dec]Team BuildingUI
- 題解:P9957 [USACO20DEC] Stuck in a Rut B
- 題解:P3113 [USACO14DEC] Marathon G
- 洛谷 P3128 [USACO15DEC] Max Flow P
- [P3065 [USACO12DEC] First! G (字典樹+判環)
- 洛谷題單指南-集合-P3405 [USACO16DEC] Cities and States S
- 洛谷 P3128 [USACO15DEC] Max Flow P 做題記錄
- BZOJ1712 : [Usaco2007 China]Summing Sums 加密加密
- C++ 洛谷 P2921 [USACO08DEC]在農場萬聖節Trick or Treat on the Farm 題解C++
- 洛谷題單指南-數學基礎問題-P2926 [USACO08DEC] Patting Heads S
- BZOJ 1628 [Usaco2007 Demo]City skyline:單調棧
- BZOJ 3391 [Usaco2004 Dec]Tree Cutting網路破壞:dfs【無根樹 節點分枝子樹大小】
- BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戲:部分列舉 位運算
- CF773D Perishable Roads3D
- BZOJ 1633 [Usaco2007 Feb]The Cow Lexicon 牛的詞典:dp【刪字元最少】字元
- POJ 1724 ROADS(優先佇列+spfa)佇列
- CF118E Bertown roads
- BZOJ 1640 [Usaco2007 Nov]Best Cow Line 佇列變換:貪心【字典序最小】佇列
- BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 護花:貪心【區域性分析法】
- USACO GCD Extreme(II)GCREM
- 【樹形dp】poj 1947 Rebuilding RoadsRebuild
- PHP7.2+ 版本,DEC-CBC加解密PHP解密
- 【CF25C】Roads in Berland Floyd深入理解
- Silverlight監測工具:Silverlight Spy
- Go 開發者進階週刊(Dec 3rd)Go
- Go 開發者進階週刊(Dec 2nd)Go
- POJ 3411 Paid Roads(搜尋的小技巧)AI
- [USACO20OPEN] Exercise P
- Silverlight Toolkit
- POJ 1947 Rebuilding Roads(基礎的樹形dp)Rebuild
- CF1149D Abandoning Roads 題解
- 2007軟體開發2.0大會課程之一:ASP.NET AJAX 與 Silverlight 的完美結合ASP.NET