zoukankan      html  css  js  c++  java
  • HDU 6187 Destroy Walls

    最大生成树。

    仔细想想会发现和坐标并没有任何关系,$king$要到达任何地点,也就是说给出的图中不能有环,因此对原图求最大生成树,不在最大生成树上的边就是要删除的,而且数量最小,费用最小。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <math.h>
    #include <set>
    #include <cmath>
    using namespace std;
    
    const int maxn = 100000 + 10;
    int n, m;
    int f[maxn];
    struct Edge {
    	int u;
    	int v;
    	int w;
    }e[2 * maxn];
    
    bool cmp(const Edge& a, const Edge& b) {
      return a.w > b.w;
    }
    
    int Find(int x) {
      if(x != f[x]) {
      	f[x] = Find(f[x]);
      }
      return f[x];
    }
    
    int main() {
      while(~scanf("%d%d", &n, &m)) {
      	int x, y;
      	for(int i = 1; i <= n; i ++) {
      	  scanf("%d%d", &x, &y);
      	}
      	for(int i = 0; i < m; i ++) {
      	  scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
      	}
      	sort(e, e + m, cmp);
      	for(int i = 1; i <= n; i ++) {
      	  f[i] = i;
      	}
      	int ans1 = 0;
      	long long ans2 = 0;
      	for(int i = 0; i < m; i ++) {
      	  int fa = Find(e[i].u);
      	  int fb = Find(e[i].v);
      	  if(fa == fb) {
      	  	ans1 ++;
      	  	ans2 += (long long)e[i].w;
      	  } else {
      	  	f[fa] = fb;
      	  }
      	}
      	printf("%d %lld
    ", ans1, ans2);
      }
      return 0;
    }
    
  • 相关阅读:
    2019年4月18日 查询功能 2
    bzoj3601
    bzoj2693
    bzoj2440
    bzoj3529
    bzoj2820
    BZOJ2813
    BZOJ4515
    AtCoder Grand Contest 001 题解
    BZOJ2757
  • 原文地址:https://www.cnblogs.com/zufezzt/p/7497075.html
Copyright © 2011-2022 走看看