zoukankan      html  css  js  c++  java
  • Matrix 并查集&&kruskal的思想(贪心思想)

    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
    ***************************************************************************************************************************
    并查集&&贪心思想
    ***************************************************************************************************************************
     1 /*
     2 有k节点的一棵树,求k个个子树,求需要去掉的边的权值和最小
     3 就是最大生成树问题,
     4 用kruskal的思想,先排序,
     5 贪心算法
     6 */
     7 #include<iostream>
     8 #include<string>
     9 #include<cstring>
    10 #include<cmath>
    11 #include<cstdio>
    12 #include<algorithm>
    13 using namespace std;
    14 bool gs[100001];
    15 int fa[100001];
    16 int n,m,i,j,k;
    17 int cas;
    18 struct node
    19 {
    20     int v,u;
    21     int value;
    22 }e[100001];
    23 bool cmp(node a,node b)
    24 {
    25     return a.value>b.value;
    26 }
    27 int find(int x)
    28 {
    29     if(x!=fa[x])
    30      return fa[x]=find(fa[x]);
    31     return fa[x];
    32 }
    33 void Unon(int a,int b)
    34 {
    35     if(gs[a])
    36        fa[b]=a;
    37     else
    38        fa[a]=b;
    39 }
    40 int main()
    41 {
    42     int a;
    43     scanf("%d",&cas);
    44     while(cas--)
    45     {
    46         scanf("%d%d",&n,&k);
    47         for(i=0;i<n-1;i++)
    48         {
    49             scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].value);
    50         }
    51         sort(e,e+n-1,cmp);
    52         memset(gs,false,sizeof(gs));
    53         for(i=0;i<=n;i++)
    54         {
    55             fa[i]=i;
    56         }
    57         while(k--)
    58         {
    59             scanf("%d",&a);
    60             gs[a]=true;
    61         }
    62         long long  ans=0;
    63         for(i=0;i<n-1;i++)
    64         {
    65             int x=find(e[i].u);
    66             int y=find(e[i].v);
    67             if(gs[x]&&gs[y])
    68               ans+=e[i].value;
    69             else
    70                Unon(x,y);
    71         }
    72         printf("%I64d
    ",ans);
    73     }
    74 }
    View Code
  • 相关阅读:
    局部 与 整体 修正 逐渐逼近
    en-zh(社会问题)social problems
    单调性 [1 + 1 / (n)]^n
    en-zh(科学技术)science and technology
    mysql函数之截取字符串
    看数据库的文件大小 MySQL Binlog日志的生成和清理规则
    Brouwer不动点
    布尔巴基学派
    量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html
    COMSOL
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3403446.html
Copyright © 2011-2022 走看看