Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include <iostream>
- using namespace std;
- const int inf=0x3f3f3f3f;
- const int maxn=1010;
- double x[maxn],y[maxn];
- double cost[maxn][maxn];
- double lowcost[maxn];
- bool vis[maxn];
- double dis(double x1,double y1,double x2,double y2) {
- return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
- }
- int n,m;
- int main() {
- while(~scanf("%d%d",&n,&m)) {
- for(int i=0; i<n; i++) {
- scanf("%lf%lf",&x[i],&y[i]);
- }
- for(int i=0; i<n; i++) {
- for(int j=0; j<n; j++) {
- cost[i][j]=dis(x[i],y[i],x[j],y[j]);
- }
- }
- for(int i=0; i<m; i++) {
- int u,v;
- scanf("%d%d",&u,&v);
- u--,v--;
- cost[u][v]=cost[v][u]=0;
- }
- memset(vis,false,sizeof(vis));
- for(int i=1; i<n; i++)lowcost[i]=cost[0][i];
- vis[0]=true;
- double ans=0;
- for(int i=1; i<n; i++) {
- double minc = 1000000000.0;
- int p=-1;
- for(int j=0; j<n; j++) {
- if(!vis[j]&&lowcost[j]<minc) {
- minc=lowcost[j],p=j;
- }
- }
- ans+=minc;
- vis[p]=true;
- for(int j=0; j<n; j++) {
- if(!vis[j]&&lowcost[j]>cost[p][j]) {
- lowcost[j]=cost[p][j];
- }
- }
- }
- printf("%.2f\n",ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment