Kruskal: Matrix

Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 441    Accepted Submission(s): 162


Problem Description
Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such that there is a
unique path between any pair of cities.

Morpheus has the news that K Machines are planning to destroy the whole kingdom. These Machines are initially living in K different cities of the kingdom and
anytime from now they can plan and launch an attack. So he has asked Neo to destroy some of the roads to disrupt the connection among Machines. i.e after destroying those roads there should not be any path between any two Machines.

Since the attack can be at any time from now, Neo has to do this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they
can be destroyed only one at a time.

You need to write a program that tells Neo the minimum amount of time he will require to disrupt the connection among machines.
 

Input
The first line is an integer T represents there are T test cases. (0<T <=10)
For each test case the first line input contains two, space-separated integers, N and K. Cities are numbered 0 to N-1. Then follow N-1 lines, each containing three, space-separated integers, x y z, which means there is a bidirectional road connecting city x and city y, and to destroy this road it takes z units of time.Then follow K lines each containing an integer. The ith integer is the id of city in which ith Machine is currently located.
2 <= N <= 100,000
2 <= K <= N
1 <= time to destroy a road <= 1000,000
 

Output
For each test case print the minimum time required to disrupt the connection among Machines.
 

Sample Input
  
  
1 5 3 2 1 8 1 0 5 2 4 5 1 3 4 2 4 0
 

Sample Output
 
 
10
Hint
Neo can destroy the road connecting city 2 and city 4 of weight 5 , and the road connecting city 0 and city 1 of weight 5. As only one road can be destroyed at a time, the total minimum time taken is 10 units of time. After destroying these roads none of the Machines can reach other Machine via any path.


杭电的主页上说用贪心算法:类似kruskal最小生成树的过程,不过此处将边按权值从大到小排列,每次将边加进来时要判断是否会使两个危险的点连通,是的话这条边就是需要被删除的,否则将它加到树上。

另外要注意的是:

我们说一个城市如果有machine则是邪恶的,那么最初只有那k个城市是邪恶的。

还有要注意的是判断一条边加进来是否会使两个危险点联通时,需要判断它的两个端点的祖先是否是邪恶的,如果不是两个同时邪恶则这条边可加入。

加入的边的两个端点的祖先如果有至少一个是邪恶的,那么这两个祖先都得变为邪恶的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

#define M 100010;
int n, k;
int flag[100010];
int p[100010];
long long ans;
long long sum;
int flag1, flag2;

struct edge{
    int u;
    int v;
    long long w;
}e[100010];

int find(int x){
    return p[x] == x ? x : p[x] = find(p[x]);
}

int cmp(edge a, edge b){    //在这里边的权值要按从大到小排序
    return a.w > b.w;
}

int Kruscal(){
    int i;
    ans = 0;
    sort(e, e+n-1, cmp);
    for(i = 0; i < n-1; i ++){
        int x = find(e[i].u);
        int y = find(e[i].v);
        if(x != y && !(flag[x] && flag[y])){
            if(flag[x] || flag[y]){
                flag[x] = 1;
                flag[y] = 1;
            }
            ans += e[i].w;
            p[x] = y;
        }
    }
    return ans;
}

int main(){
    int t;
    int i, tmp;
    cin >> t;
    while(t --){
        sum = 0;
        scanf("%d %d", &n, &k);//k个怪物则要删除k-1条边,保留n-k条边
        //cout << "n=" << n  << endl;
        for(i = 0; i < n-1; i ++){
            //cout << "i=" << i << endl;
            scanf("%d %d %I64d",&e[i].u, &e[i].v, &e[i].w);
            sum += e[i].w;
        }
        for(i = 0; i < n; i ++){
            flag[i] = 0;
            p[i] = i;
        }
        for(i = 0; i < k; i ++){
            scanf("%d", &tmp);
            flag[tmp] = 1;
        }
        Kruscal();
        cout << sum - ans << endl;
    }
    return 0;
}

/*
2
5 3
2 1 8
1 0 5
2 4 5
1 3 4
2
4
0
5 3
2 1 8
1 0 5
2 4 5
1 3 4
2
4
0
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值