zoukankan      html  css  js  c++  java
  • #operator ——“Kruskal算法求最小生成树 中的 operator” ~20.8.17

    这里是找到的资料
    mycomplex operator + (mycomplex &p1,mycomplex &p2) //重定义"+"
    {
    	 mycomplex p;
    	 p.x=p1.x+p2.x;
    	 p.y=p2.y+p1.y;
    	 return p;
     }
    
    int main()
    {
        mycomplex p1(82,p2(81;
        mycomplex p3;
        p3=p1+p2;
        std::cout<<p3<<std::endl;
        return 0;
    }
    
    原题代码
    #include<bits/stdc++.h>
    using namespace std;
    const int N = 200010;
    
    int n, m;
    int p[N];
    
    struct E{
        int a, b, w;
        bool operator < (E const &W) const{//这里
            return w < W.w;
        }
    }e[N];
    
    int find(int x){
        if(p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    
    int main(){
        cin >> n >> m;
        for(int i = 0; i <  m; i ++){
            int a, b, w;
            cin >> a >> b >> w;
            e[i] = {a, b, w};
        }
        
        sort(e, e + m);
        
        for(int i = 1; i < n; i ++) p[i] = i;
        
        int res = 0, cnt = 0;
        for(int i = 0 ; i < m; i ++){
            int a = e[i].a, b = e[i].b, w = e[i].w;
            
            a = find(a), b = find(b);
            if(a != b){
                p[a] = b;
                res += w;
                cnt ++;
            }
        }
        if(cnt < n - 1) cout << "impossible";
        else cout << res;
        
        return 0;
    }
    
    具体笔记
    bool operator < (E const &W) const{
    //这里 可以写成 bool operator < (const E & W) const{
            return w < W.w;
        }
    /*
    *1 bool operator                是    重定义 < 时返回的类型。
    *2 (E const &W) / (const E & W) 是    E中所有的元素W
    *3 return w < W.w;              是    排序的标准(从小到大)
    */
    

    建议用以下写法

    
    struct E{
        int a, b, w;
    }e[N];
    //像这样将operator放到外面去,好理解…… 
    bool operator < (E &a, E &b){
        return a.w < b.w;
    }
    
    
    #include<bits/stdc++.h>
    using namespace std;
    const int N = 200010;
    
    int n, m;
    int p[N];
    //定义数据结构E,方便对边进行操作
    struct E{
        int a, b, w;
    }e[N];
    
    bool operator < (E &a, E &b){
        return a.w < b.w;
    }
    
    int find(int x){
        if(p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    
    int main(){
        cin >> n >> m;
        for(int i = 0; i < m; i ++){
            int a, b, w;
            cin >> a >> b >> w;
            e[i] = {a, b, w};
        }
        
        sort(e, e + m);
        
        for(int i = 1; i < n; i ++) p[i] = i;
        
        int res = 0, cnt = 0;
        for(int i = 0; i < m; i ++){
            int a = e[i].a, b = e[i].b, w = e[i].w;
            
            a = find(a), b = find(b);
            if(a != b){
                p[a] = b;;
                res += w;
                cnt ++;
            }
        }
        
        if(cnt < n - 1) cout << "impossible";
        else cout << res;
        
        return 0;
    }
    
  • 相关阅读:
    自定义标签
    ssm学习的第一个demo---crm(1)
    xml文件中的${}
    Mybatis的回顾学习
    Mapper的.xml文件的delete的参数问题
    mybatis中xml文件的${}和#{}区别
    Mybatis:通过MapperScannerConfigurer进行mapper扫描
    Spring的applicationContext.xml的疑问解析
    Spring MVC 的springMVC.xml疑问解析
    【剑指 Offer】04.二维数组中的查找
  • 原文地址:https://www.cnblogs.com/yuanyulin/p/14026722.html
Copyright © 2011-2022 走看看