栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1203 Swordfish

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1203 Swordfish

#include<iostream>#include<queue>#include<math.h>using namespace std;int father[101];int fine(int x){int r=x,j;while(father[r]>0)r=father[r];while(r!=x){ j=father[x]; father[x]=r; x=j;}return r;}void weightmerge(int x,int y){int temp;int ix,iy;ix=fine(x);iy=fine(y);temp=father[ix]+father[iy];if(ix!=iy)if(father[ix]>father[iy]){father[iy]=temp;father[ix]=iy;}else{father[ix]=temp;father[iy]=ix;}}struct edge{double st,end;double weight;friend bool operator < (edge a,edge b){return a.weight>b.weight;}};priority_queue<edge> que;double Kruskal();int num;int main(){bool flag=false;int icase=1;edge city[101];while(cin>>num){int i,j,ci=1;if(!num)break;for(i=1;i<=num;i++){cin>>city[ci].st>>city[ci].end;ci++;}for(i=1;i<=num;i++)father[i]=-1;edge aedge;while(!que.empty())que.pop();for(i=1;i<=num;i++)for(j=i+1;j<=num;j++){aedge.st=i;aedge.end=j;aedge.weight=(city[i].st-city[j].st)*(city[i].st-city[j].st)+(city[i].end-city[j].end)*(city[i].end-city[j].end);que.push(aedge);}cout.setf(ios::fixed);cout.precision(2);if(flag)cout<<endl;flag=true;cout<<"Case #"<<icase++<<":"<<endl<<"The minimal distance is: "<<Kruskal()<<endl;}}double Kruskal(){int Account=0;double sum=0;edge temp;while(!que.empty()&&Account!=num){temp=que.top();que.pop();if(fine(temp.st)!=fine(temp.end)){weightmerge(temp.st,temp.end);if((-father[fine(temp.st)])>Account)Account=0-father[fine(temp.st)];sum+=sqrt(temp.weight);}}return sum;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372186.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号