题解 | #Freckles#

Freckles

http://www.nowcoder.com/practice/41b14b4cd0e5448fb071743e504063cf

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
struct cmp{
    bool operator()(const pair<float,pair<int,int>> &a,const pair<float,pair<int,int>> &b){
        return a.first > b.first;
    }
};
float cal(pair<float,float> a,pair<float,float> b){
    return sqrt(fabs(a.first - b.first) * fabs(a.first - b.first) + fabs(a.second - b.second) * fabs(a.second - b.second));
}
int find(int x,vector<int> father){
    if(x != father[x])
        father[x] = find(father[x],father);
    return father[x];
}
void clear(priority_queue<pair<float,pair<int,int>>,vector<pair<float,pair<int,int> >>,cmp>& q) {
    priority_queue<pair<float,pair<int,int>>,vector<pair<float,pair<int,int> >>,cmp> empty;
    swap(empty, q);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,edgenum;
    float x,y,dist;
    vector<int> father;
    vector<pair<float,float>> vec;
    priority_queue<pair<float,pair<int,int>>,vector<pair<float,pair<int,int> >>,cmp> qu;
    while(cin >> n){
        father.clear();
        vec.clear();
        clear(qu);
        while(n--){
            cin >> x >> y;
            vec.push_back(pair<float,float>(x,y));
        }
        for(int i = 0;i < vec.size();++i){
            for(int j = i + 1;j < vec.size();++j){
                qu.push(pair<float,pair<int,int>>(cal(vec[i],vec[j]),pair<int,int>(i,j)));
            }
        }
        for(int i = 0;i < vec.size();++i)
            father.push_back(i);
        dist = 0;
        edgenum = 0;
        while(edgenum < vec.size() - 1 && !qu.empty()){
            auto cur = qu.top();
            qu.pop();
            int f = cur.second.first;
            int t = cur.second.second;
            f = find(f,father);
            t = find(t,father);
            if( f != t ){
                father[f] = t;
                dist += cur.first;
                edgenum++;
            }
        }
        printf("%.2f\n",dist);
    }
}
全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
06-15 02:05
已编辑
南昌航空大学 数据分析师
Eason三木:你如果想干技术岗,那几个发公众号合唱比赛的经历就去掉,优秀团员去掉,求职没用。然后CET4这种不是奖项,是技能,放到下面的专业技能里或者单独列一个英语能力。 另外好好改改你的排版,首行缩进完全没有必要,行间距好好调调,别让字和标题背景黏在一起,你下面说能做高质量PPT你得展现出来啊,你这简历排版我用PPT做的都能比你做的好。 然后自我评价,你如果要干数据工程师,抗压能力强最起码得有吧。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务