poj 3723 Conscription

Conscription
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9574   Accepted: 3394

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 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, NM and R.
Then R lines followed, each contains three integers xiyi and di.
There is a blank line before each test case.

1 ≤ NM ≤ 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

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_V = 50001;    //最大顶点数
const int MAX_E = 50001;    //最大边数

//并查集
int parent[MAX_V];  //父节点数组
int rank[MAX_V];    //高度
void init_union_find(int n){    //初始化
    for(int i = 0; i < n; ++i){
        parent[i] = i;
        rank[i] = 0;
    }
}

int find(int n){    //查找集合
    if(parent[n] == n)
        return n;
    //这么做是一个优化,查找到n的各个直系祖先结点,并且将这些直系祖先结点的父节点都设置为该集合根节点
    return parent[n] = find(parent[n]);
}

bool same(int n, int m){    //判断n,m是否在同一集合
    return find(n) == find(m);
}

void unite(int n,int m){    //将n,m合并为同一个集合
    if(!same(n,m)){
        n = find(n);
        m = find(m);
        if(rank[n] > rank[m]){
            parent[m] = n;
        } else if (rank[n] < rank[m]){
            parent[n] = m;
        } else {
            parent[m] = n;
            ++rank[n];
        }
    }
}



//kruskal算法


int E, V;    //边数,顶点数

struct edge{
    int u;     //顶点u
    int v;     //顶点v
    int cost;   //权值
};
edge es[MAX_E]; //邻接边数组

bool comp(const edge& e1, const edge& e2){  //比较
    return e1.cost < e2.cost;
}

int kruskal() {
    sort(es, es + E, comp); //从小到大排序
    init_union_find(V);     //初始化并查集
    int res = 0;
    for(int i = 0; i < E; ++i){
        edge e = es[i]; //从边集中提出一条边
        if(!same(e.u, e.v)){
            res += e.cost;
            unite(e.u, e.v);
        }
    }
    return res;
}

int N, M, R;    //男兵,女兵,关系数
int x[MAX_V];   //男兵人数
int y[MAX_V];   //女兵人数
int inti[MAX_V];//亲密度
void solve(){
    V = N + M;
    E = R;
    for(int i = 0; i < R; ++i){

        //将对应关系转换
        es[i].u = x[i];
        es[i].v = N + y[i];
        es[i].cost = -inti[i];
        //同上,能实现,但POJ显示编译错误
//        es[i] = (edge){x[i], N + y[i], -inti[i]};
    }
    printf("%d\n", 10000 * (N + M) + kruskal());
}


int main(){
    int n;  //测试组数
    scanf("%d", &n);
    while(n--){
        scanf("%d%d%d", &N, &M, &R);
        for(int i = 0; i < R; ++i){
            scanf("%d%d%d", &x[i], &y[i], &inti[i]);
        }
        solve();
    }
    return 0;
}




你可能感兴趣的:(poj 3723 Conscription)