zoukankan      html  css  js  c++  java
  • NOIP模拟 wall

    题目大意:

    给出n个点,第i个点坐标是((x_i, y_i)),给出m条边,第i条边的权值是(w_i),求将图变成一颗树所需要删除边的最小权值和。

    题目分析:

    首先要看出坐标其实是出题人使出的障眼法,把人往计算几何引。看透问题后就知道这是求一颗最大生成树。

    code:

    #include<bits/stdc++.h>
    using namespace std;
    
    const int N = 100005, M = 200005;
    int n, m, T;
    struct node{
        int a, b, c;
        inline bool operator < (const node &b) const{
            return c > b.c;
        }
    };
    vector<node> edges;
    int anc[N];
    typedef long long ll;
    ll ans1, ans2;
    
    inline int getAnc(int x){return x == anc[x] ? x : (anc[x] = getAnc(anc[x]));}
    
    int main(){
        freopen("h.in", "r", stdin);
        ios::sync_with_stdio(false);
        cin.tie(NULL), cout.tie(NULL);
        cin >> T;
        while(T--){
            cin >> n >> m; 
            edges.clear(); ans1 = 0, ans2 = m;
            for(int i = 1; i <= n; i++) anc[i] = i;
            for(int i = 1; i <= n; i++){int x, y; cin >> x >> y;}
            for(int i = 1; i <= m; i++){
                int x, y, w; cin >> x >> y >> w;
                edges.push_back((node){x, y, w});
                ans1 += 1ll*w;
            }
            sort(edges.begin(), edges.end());
            for(int i = 0; i < edges.size(); i++){
                int f1 = getAnc(edges[i].a), f2 = getAnc(edges[i].b);
                if(f1 != f2){anc[f1] = f2, ans1 -= 1ll*edges[i].c, ans2--;}
            }
            cout << ans2 << " " << ans1 << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    一次使用布隆过滤器的经历
    从C#到Java(effective-java阅读笔记)
    从C#到Java(泛型)
    Dubbo学习-第一章
    从C#到Java(Spring拦截器HandlerInterceptor )
    从C#到Java(Aspect)
    从C#到Java(SpringBoot入门)
    从C#到Java(lambda比较)
    Redis添加List
    Three.js学习(相机,场景,渲染,形状)
  • 原文地址:https://www.cnblogs.com/CzYoL/p/7683690.html
Copyright © 2011-2022 走看看