POJ 2349 Arctic Network

Source: Internet
Author: User

It is also the minimum spanning tree, but s points can be connected among n points.

Use krusical. Record the number of edges added each time. When the number of connected edges has n-s, it jumps out.

 

 #include<iostream>   #include<cstdio>   #include<cmath>   #include<cstring>   #include<algorithm>   using namespace std;  int n,s,f[1010];  struct node  {      int x,y;      double s;  }e[250010];  bool cmp(node s, node v)  {      return s.s<v.s;  }  int find(int x)  {      if (x==f[x]) return x;      f[x]=find(f[x]);      return f[x];  }  void krusical(int p)  {      int i,t=0;      for (i=0; i<p; i++)      {          int x=find(e[i].x);          int y=find(e[i].y);          if (x!=y)          {              f[y]=f[x];              t++;          }          if (t==n-s) break;      }      printf("%.2f\n",e[i].s);  }  int main ()  {      int t,i,j;      cin>>t;      while(t--)      {          int zb[510][2],p=0;          cin>>s>>n;          for (i=1; i<=n; i++)          {              f[i]=i;              scanf("%d%d",&zb[i][0],&zb[i][1]);          }          for (i=1; i<n; i++)              for (j=i+1; j<=n; j++)              {                  e[p].x=i;                  e[p].y=j;                  e[p].s=sqrt((zb[i][0]-zb[j][0])*(zb[i][0]-zb[j][0])+(zb[i][1]-zb[j][1])*(zb[i][1]-zb[j][1]));                  p++;              }          sort(e,e+p,cmp);          krusical(p);      }      return 0;  }  #include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int n,s,f[1010];struct node{    int x,y;    double s;}e[250010];bool cmp(node s, node v){    return s.s<v.s;}int find(int x){    if (x==f[x]) return x;    f[x]=find(f[x]);    return f[x];}void krusical(int p){    int i,t=0;    for (i=0; i<p; i++)    {        int x=find(e[i].x);        int y=find(e[i].y);        if (x!=y)        {            f[y]=f[x];            t++;        }        if (t==n-s) break;    }    printf("%.2f\n",e[i].s);}int main (){    int t,i,j;    cin>>t;    while(t--)    {        int zb[510][2],p=0;        cin>>s>>n;        for (i=1; i<=n; i++)        {            f[i]=i;            scanf("%d%d",&zb[i][0],&zb[i][1]);        }        for (i=1; i<n; i++)            for (j=i+1; j<=n; j++)            {                e[p].x=i;                e[p].y=j;                e[p].s=sqrt((zb[i][0]-zb[j][0])*(zb[i][0]-zb[j][0])+(zb[i][1]-zb[j][1])*(zb[i][1]-zb[j][1]));                p++;            }        sort(e,e+p,cmp);        krusical(p);    }    return 0;}

 

Related Article

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.