[Usaco2007 DEC] building roads for road construction [Minimum Spanning Tree]

Source: Internet
Author: User
Description

Farmer John recently got some new farms. He wants to build new roads so that all his farms can communicate with each other through the original or new roads (that is, from any farm, you can go through some first-and-tail roads to all the remaining farms ). Some farms are originally connected by roads. All N (1 <= n <= 1,000) farms (1 .. N sequential number) on the map is expressed as the coordinate (x_ I, y_ I) Point (0 <= x_ I <= 1,000,000; 0 <= y_ I <= 1,000,000 ), the length of the roads between two farms naturally represents the distance between their points. Now Farmer John also tells you which two farms are connected by the original M (1 <= m <= 1,000) route between the farms, and he wants you to calculate it, the minimum total length of the road he needs to build to connect all farms.

Input

* Row 1st: two integers separated by spaces: N and m

* 2nd .. n + 1 rows: I + 1 Act 2 integers separated by spaces: X_ I, y_ I * n + 2 .. N + m + 2 rows: each line uses two integers separated by spaces. I and j describe an existing road. This road connects farm I and farm J.

Output

* Row 1st: output the minimum total length of the road to be built to connect all farms. Keep two decimal places without any extra rounding operation. To avoid precision errors, use 64-bit real variables when calculating the distance between farms and the answer

Sample input4 1
1 1
3 1
2 3
4 3
1 4

Input description:

FJ has four farms with coordinates. Farm 1 and farm
4. There was originally a road connection between them.


Sample output4.00

Output description:

FJ chooses to build a 2.00-length road between farm 1 and farm 2, and between farm 3 and farm 4
A road with a length of 2.00. In this way, the total length of the road to be built is 4.00, and this is the road in all schemes
The smallest total length.

 

Originally, I was about to finish the question = As a result, when I updated Windows 7, I restarted the Qaq blog Park and didn't save it... I wanted to cry.

Forget it .. Write another copy.

 

Question:

When we see that all vertices are connected and the total value is minimized, we should think of the Minimum Spanning Tree. Should this be easy?

 

I first obtain the distance from any vertex to all vertices except itself (that is, edge weight), and assign the edge weight between the two connected nodes to 0.

Then, write the data according to the bare minimum spanning tree;

 

Mark the details:

At the beginning of the test, we had three points wa. After debugging, we found that some of the DIST values were-nan0cx00000.

I asked the senior student, it is said that this strange value is possible only by dividing by 0 or the explosive range.

After each breakpoint, it is found that the distance between two points is obtained.

Double D (INT X1, int Y1, int X2, int Y2 ){
Return SQRT (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2 ));
}

 

(X1-x2) * (x1-x2) has exploded int

Then I changed int to long.

 

I think we should be very careful about the scope of this explosion. The last time I wrote a question mod in CF, I had an int, which is a pitfall ....

 

Code attached:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#include <string>using namespace std;const int maxn=1001;int n,m;struct node{int x,y;}a[maxn];int fa[maxn*maxn];int x,y;struct kru{int num1,num2;double dist;}f[maxn*maxn];int tot=0;double ans=0;bool cmp(const kru &a,const kru &b){return a.dist<b.dist?1:0;}double d(long long x1,long long y1,long long  x2,long long y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}int find(int x){if(fa[x]==x) return x; else return fa[x]=find(fa[x]);}int main(){freopen("roads.in","r",stdin);freopen("roads.out","w",stdout);    //freopen("data.txt","r",stdin);scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) fa[i]=i;for(int i=1;i<=n;i++){scanf("%d%d",&a[i].x,&a[i].y);}for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);f[++tot].num1=x;f[tot].num2=y;f[tot].dist=0;}for(int i=1;i<=n;i++)   for(int j=i+1;j<=n;j++){     f[++tot].num1=i;     f[tot].num2=j;     f[tot].dist=d(a[i].x,a[i].y,a[j].x,a[j].y);   }sort(f+1,f+tot+1,cmp);int k=0;for(int i=1;i<=tot;i++){int u=f[i].num1;int v=f[i].num2;if(find(u)!=find(v)){ans+=f[i].dist;fa[find(u)]=find(v);k++;}if(k==n-1) break;}printf("%.2f",ans);return 0;}

 

[Usaco2007 DEC] building roads for road construction [Minimum Spanning Tree]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.