• POJ 3723 Conscription (最小生成树)


    题意:给定 n 个男人,m 个女人,和 r 个男女之间的关系,每个征募一个人要用10000元,但是如果有关系可以少花一些钱,即10000-亲密度,

    求一个最小要花多少钱。

    析:最后生成的关系肯定是一片森林,也就是最大权森林,但是我可以把权值取反,然后就是一个求最小森林了。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 10000 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    struct Node{
      int u, v, val;
      Node() { }
      Node(int uu, int vv, int va) : u(uu), v(vv), val(va) { }
      bool operator < (const Node &p) const{
        return val < p.val;
      }
    };
    Node a[maxn*5];
    int p[maxn*2];
    int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); }
    
    int solve(int r){
      sort(a, a + r);
      int ans = 0;
      for(int i = 0; i < r; ++i){
        int x = Find(a[i].u);
        int y = Find(a[i].v);
        if(x != y) p[y] = x, ans += a[i].val;
      }
      return ans;
    }
    
    int main(){
      int T;  cin >> T;
      while(T--){
        int r;
        scanf("%d %d %d", &n, &m, &r);
        for(int i = 0; i < n+m; ++i)  p[i] = i;
        for(int i = 0; i < r; ++i){
          int u, v, val;
          scanf("%d %d %d", &u, &v, &val);
          a[i] = Node(u, v+n, -val);
        }
        printf("%d
    ", 10000 * (n+m) + solve(r));
      }
      return 0;
    }
    
  • 相关阅读:
    浏览器缓存之灾
    js控制页面带参跳转
    解决 iOS7 通过tag 找不到 UITableViewCell 的子控件(转)
    Xcode工程添加第三方文件的详细分析 Create folder references for any added folders(转)
    iOS开发 frame 与 bounds 的区别与关系 转自隔叶黄莺
    关于UIGestureRecognizerState
    iOS 开发,工程中混合使用 ARC 和非ARC(转)
    iOS使用NSURLConnection发送同步和异步HTTP Request
    关于error:Cannot assign to 'self' outside of a method in the init family
    ios学习之UIWebView网页视图调整
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6555654.html
Copyright © 2020-2023  润新知