Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7756 | Accepted: 2232 |
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 (Xi,Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤Yi ≤ 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: Xi andYi
* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farmi 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
好吧,。这题比较强大,不过我还是用不变的Prim模版做的。。哈哈。
先输入n,m。下面的n行,是输入n行坐标(x,y),在就是输入m行的(i,j)//
代码:
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
double road[1010][1010];
double dist[1010];
int vis[1010];
int n,m,i,j,rec;
double d,sum,max;
struct node{
double x,y;
};
struct node data[1010];
void init(){
while(m--){
scanf("%d %d",&i,&j);
road[i][j]=road[j][i]=0;;
}
}
void solve(){
for(i=1;i<=n;i++){
for(j=i+1;j<=n;j++){
d=sqrt((data[i].x-data[j].x)*(data[i].x-data[j].x)+(data[i].y-data[j].y)*(data[i].y-data[j].y));
road[i][j]=road[j][i]=d;
}
}
}
double Prim(){
memset(vis,0,sizeof(vis));
double sum=0.0;
int k;
for(int i=1;i<=n;i++)
dist[i]=road[1][i];
vis[1]=1;
for(int i=1;i<n;i++){
double min=100000000.0;
for(int j=1;j<=n;j++)
if(!vis[j]&&dist[j]<min){
min=dist[j];
k=j;
}
sum+=min;
vis[k]=1;
for(int j=1;j<=n;j++){
if(!vis[j]&&road[k][j]<dist[j])
dist[j]=road[k][j];
}
}
return sum;
}
int main(){
while(scanf("%d %d",&n,&m)!=EOF){
for(i=1;i<=n;i++)
scanf("%lf %lf",&data[i].x,&data[i].y);
memset(road,0,sizeof(road));
solve();
init();
printf("%.2lf\n",Prim());
}
return 0;
}