zoukankan      html  css  js  c++  java
  • POJ 3723 Conscription 最小生成树

    题目链接:

    题目

    Conscription
    Time Limit: 1000MS
    Memory Limit: 65536K

    问题描述

    Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

    输入

    The first line of input is the number of test case.
    The first line of each test case contains three integers, N, M and R.
    Then R lines followed, each contains three integers xi, yi and di.
    There is a blank line before each test case.

    1 ≤ N, M ≤ 10000
    0 ≤ R ≤ 50,000
    0 ≤ xi < N
    0 ≤ yi < M
    0 < di < 10000

    输出

    For each test case output the answer in a single line.

    样例

    input
    2

    5 5 8
    4 3 6831
    1 3 4583
    0 0 6592
    0 1 3063
    3 3 4975
    1 3 2049
    4 2 2104
    2 2 781

    5 5 10
    2 4 9820
    3 2 6236
    3 1 8864
    2 4 8326
    2 0 5156
    2 0 1463
    4 1 2439
    0 4 4373
    3 4 8889
    2 4 3133

    output
    71071
    54223

    题意

    现在选N个男生和M个女生进入部队,如果男生u和女生v有关系,那么如果有一个已经在部队里面了,那另一个的费用只需10000-p(关系系数)。并且每个人进入部队时他只能使用和最多一个人的关系。
    问最少的花费招到所有的人。

    题解

    如果使用的关系之间出现了环,那么就不必有至少一个人同时使用了两个关系,所以题目就转化成了求最大生成树了。

    代码

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 21111;
    
    struct Edge {
    	int u, v, w;
    	Edge(int u, int v, int w) :u(u), v(v), w(w) {}
    	bool operator < (const Edge& tmp) const {
    		return w > tmp.w;
    	}
    };
    
    int n, m, r;
    int fa[maxn];
    vector<Edge> egs;
    
    int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]); }
    
    void init() {
    	for (int i = 0; i <= n + m; i++) fa[i] = i;
    	egs.clear();
    }
    
    int main() {
    	int tc;
    	scanf("%d", &tc);
    	while (tc--) {
    		scanf("%d%d%d", &n, &m,&r);
    		init();
    		while (r--) {
    			int u, v, w;
    			scanf("%d%d%d", &u, &v, &w);
    			egs.push_back(Edge(u,v+n,w));
    		}
    		sort(egs.begin(), egs.end());
    		int cnt = 0;
    		for (int i = 0; i < egs.size(); i++) {
    			Edge& e = egs[i];
    			int pu = find(e.u);
    			int pv = find(e.v);
    			if (pu != pv) {
    				cnt += e.w;
    				fa[pv] = pu;
    			}
    		}
    		printf("%d
    ", (n + m) * 10000 - cnt);
    	}
    	return 0;
    }
  • 相关阅读:
    Unity中获取移动设备GPS信息
    Unity打包APP控制顶部状态栏
    Hololens概览
    webdriver中的定位模拟及实例
    selenium等待机制
    seleniumChrom无头浏览器
    selenium规避网站监测
    百度api识别验证码登录
    Pyecharts-数据可视化模块
    Selenium-浏览器兼容性测试自动化
  • 原文地址:https://www.cnblogs.com/fenice/p/5641222.html
Copyright © 2011-2022 走看看