最大权森林

 http://poj.org/problem?id=3723

分析:因为,图可能不连通,求所有最大生成树的总和最大,即最大权森林。

prime算法会超内存。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define _Clr(x, y) memset(x, y, sizeof(x))
 5 #define INF 0x3f3f3f3f
 6 #define N 20010
 7 using namespace std;
 8
 9 struct Edge
10 {
11     int u, v, ca;
12     bool operator < (const Edge &a) const
13     {
14         return ca > a.ca;
15     }
16 }edge[N*3];
17 int n, m;
18 int bleg[N];
19
20 int find(int x)
21 {
22     int y = x;
23     while(y != bleg[y])
24         y = bleg[y];
25     while(x != bleg[x])
26     {
27         int px = bleg[x];
28         bleg[x] = y;
29         x = px;
30     }
31     return y;
32 }
33
34 int Kruskal(int r)
35 {
36     int ans=0;
37     for(int i=0; i<N; i++)
38         bleg[i] = i;
39     for(int i=0; i<r; i++)
40     {
41         int a = find(edge[i].u), b = find(edge[i].v);
42         if(a != b)
43         {
44             ans += edge[i].ca;
45             bleg[a] = b;
46         }
47     }
48     return ans;
49 }
50 int main()
51 {
52     int T, m, r;
53     int a, b, c;
54     //freopen("date.in","r", stdin);
55     scanf("%d", &T);
56     while(T--)
57     {
58         scanf("%d%d%d", &n, &m, &r);
59         for(int i=0; i<r; i++)
60         {
61             scanf("%d%d%d", &a, &b, &c);
62             a++, b++;
63             b += n;
64             edge[i].u=a, edge[i].v=b, edge[i].ca=c;
65         }
66         n += m;
67         sort(edge, edge+r);
68         int ans = Kruskal(r);
69         ans = 10000 * n - ans;
70         printf("%d\n", ans);
71     }
72     return 0;
73 }

时间: 2024-08-25 01:47:00

最大权森林的相关文章

poj - 3723 Conscription(最大权森林)

http://poj.org/problem?id=3723 windy需要挑选N各女孩,和M各男孩作为士兵,但是雇佣每个人都需要支付10000元的费用,如果男孩x和女孩y存在亲密度为d的关系,只要他们其中有一个已经被选中,那么在选另一个人需要的费用为100000-d,给定R个关系,输出一个最低费用,每个关系只能使用一次. 把人看作顶点,关系看作边,就可以转化为无向图中的最大权森林问题,最大权森林问题可以通过把所有边权取反之后用最小生成树的算法求解. 1 #include <cstdio> 2

POJ 3723 Conscription (最大权森林 + Kruskal算法)

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8317   Accepted: 2887 Description 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

poj 3723 Conscription 【最大生成树|最大权森林】

题目:poj 3723 Conscription 题意:要征兵n个男兵和m个女兵,每个花费10000元,但是如果已经征募的男士兵中有和将要征募的女士兵关系好的,那么可以减少花费,给出关系,求最小花费. 分析:这个题目初始一个是个二分图,以为可以从这里入手,但是这个题目这个性质没用. 初始花费没人10000,那么减去其中有关系的就是当前的花费. 要是花费最少,那么减去的最大即可,又因为没人只征募一次,即最多选择一个,所以减去一个最大生成树就ok AC代码: #include <cstdio> #

Conscription POJ - 3723

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 relationshi

POJ3723(邻接表+并查集+Kruskal)

题目链接:点击打开链接 解题思路: 根据相互之间的关系,可以转化一个无向图中最大权森林的问题.也就是把边权取反,然后用最小生成树求解. 本题用邻接表存储,Kruskal求最小生成树. 完整代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <set> #include &l

POJ 3723 Conscription(最小生成树)

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 relationshi

应用运筹学基础:组合优化 (2) - 一类问题的贪心解法

这一节课讲解了被称为独立系统的一类问题,以及用贪心解决独立系统问题的近似比. 独立系统 考虑一个有限元素集合 $E$,给 $E$ 中的每个元素 $e$ 定义一个非负的费用 $c(e)$.再考虑 $\mathcal{F} \in 2^E$,那么对于 $F \in \mathcal{F}$,我们定义 $F$ 的费用 $c(F) = \sum\limits_{e \in F} c(e)$.现在我们要找出一个 $F$,使得 $c(F)$ 最大(或最小).这就是这节课我们需要考虑的一类问题. 独立系统 从

【POJ - 3723 】Conscription(最小生成树)

Conscription Descriptions 需要征募女兵N人,男兵M人. 每招募一个人需要花费10000美元. 如果已经招募的人中有一些关系亲密的人,那么可以少花一些钱. 给出若干男女之前的1 ~ 9999 之间的亲密度关系, 招募某个人的费用是 10000 - (已经招募了的人中和自己的亲密度的最大值). 要求通过适当的招募顺序使得招募所有人所花费的费用最小. Input 输入N, M, R;接下来输入R行 (x, y, d) 表示第 x 号男兵和第 y 号女兵之间的亲密度是 d Ou

决策树 随机森林 adaboost

? 熵.互信息? 决策树学习算法 ? 信息增益 ? ID3.C4.5.CART? Bagging与随机森林? 提升 ? Adaboost/GDBT ? 熵.互信息 熵是对平均不确定性的度量. 平均互信息:得知特征Y的信息而使得对标签X的信息的不确定性减少的程度.描述随机变量之间的相似程度.(条件熵.相对熵:差异性) ? 决策树 决策树学习采用的是自顶向下的递归方法,有监督学习. 其基本思想是以信息熵为度量构造一棵熵值下降最快的树,到叶子节点处的熵值为零,此时每个叶节点中的实例都属于同一类. 建立