最小生成树(坑爹啊) 内附kruskal算法模板和并查集算法模板

题目:https://vjudge.net/problem/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 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.

Input

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

Output

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

Sample 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

Sample Output

71071
54223


题意
给你n+m个人,共有r种关系,输入a b c
表示a和b有关系,然后如果其中有一个人先被招募的话,会省下c元钱
每个人初始招募价格为10000
然后问你 最小花费多少钱

题解
直接就连边,然后边的权值为负,然后跑一发最小生成树就好了

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#define maxn 50010
using namespace std;
//并查集板子
int par[maxn];//父亲
int rank[maxn];//树的高度
void init(int n)//初始化/874
{
    for(int i=0;i<n;i++){
        par[i]=i;//初始化父亲为自己
        rank[i]=0;//高度为0
    }
}
int find(int x)//找父亲
{
    if(par[x]==x)return x;
    else {
        return par[x]=find(par[x]);//递归找父亲(因为父亲是统一的)
    }
}
void unite(int x,int y)//合并把两个父亲不同的按要求统一父亲
{
    x=find(x);
    y=find(y);
    if(x==y)return ;//如果父亲相同就不用合并
//    if(x!=y)  //这里没有优化,,这里只是统一y树接到x上
//    {
//        par[y]=x;
//    }
    //优化了的:把树小的接到树大的上,
    if(rank[x]<rank[y])
    {
        par[x]=y;
    }
    else par[y]=x;
    if(rank[x]==rank[y])rank[x]++;
}
bool same(int x,int y)  //判断父亲是否相同
{
    return find(x)==find(y);
}

//最小生成树板子(krusjkal算法)
struct edge{     //图的结构体,,
  int from,to,cost;   //从from顶点到to顶点的边长是cost
};
bool comp(const edge& e1,const edge& e2)  //举个例子 从1-->2有两条路,这里就是保留路小的那条
{
    return e1.cost<e2.cost;
}
edge mp[maxn];  //
int V,E;    // 分别是顶点数和边数
int res;
int kruskal()
{
    sort(mp,mp+E,comp);//以边的长度来排序,从小到大排序
    init(V);//并查集初始化
    res=0;
    for(int i=0;i<E;i++)
    {
        edge s=mp[i];  //将mp【】里的东西提出来用
        if(!same(s.from,s.to)){
            unite(s.from,s.to);
        res+=s.cost; //将连在一起的边加起来
        }
    }
    return res;
}
int boy,girl,dis;
int main()
{
   int t;
   int n,m,r;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&r);
        V=n+m;
        E=r;
        for(int i=0;i<r;i++)
        {
            scanf("%d%d%d",&boy,&girl,&dis);
            mp[i].from=boy;
            mp[i].to=girl+n;
            mp[i].cost=-dis;
        }
        kruskal();
        printf("%d
",res+10000*(n+m));
    }
    return 0;
}

我真的要疯了,,这道题我交了14次   都是超时,,我改过maxn范围,,改过优化的并查集。。。。一系列措施,,都要崩溃了

特么网上看一下别人写的,试探性用scanf输入输出,,tmd过了,,想死的心都有了

我的一下午啊,,真tm有意思这题



原文地址:https://www.cnblogs.com/huangzzz/p/8352170.html