zoukankan      html  css  js  c++  java
  • Matrix(类似kruskal)

    Matrix

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 2   Accepted Submission(s) : 1
    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
     

    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
    题解:这题就是一些机器人在不同城市,通过毁坏一定的路,使他们不能相互联系;毁坏路需要时间,求最短时间;
    解这个题需要先对时间从大到小排序,从大到小是为了让那些没被标记的陷进去,到最后出现被标记的路时就小了,所需时间就少了;
    注意的是vis的记录,还有要让被标记的当老大;
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 const int MAXN=100010;
     6 struct Node{
     7     int s,e,t;
     8 };
     9 Node dt[MAXN];
    10 /*int cmp(const void *a,const void *b){
    11     return (*(Node *)a).t-(*(Node *)b).t)  ;//
    12 }*/
    13 int cmp(Node a,Node b){
    14     return a.t>b.t;
    15 }
    16 int pre[MAXN];
    17 int find(int x){
    18     return pre[x]= x==pre[x]?x:find(pre[x]);
    19 }
    20 int visit[MAXN],n,m,flot;
    21 __int64 time;
    22 void initial(){
    23     memset(visit,0,sizeof(visit));
    24     memset(pre,-1,sizeof(pre));
    25     time=0;flot=0;
    26 }
    27 void merge(Node a){
    28     int f1,f2;
    29     if(pre[a.s]==-1)pre[a.s]=a.s;
    30     if(pre[a.e]==-1)pre[a.e]=a.e;
    31     f1=find(a.s);f2=find(a.e);
    32     if(f1==f2)return;
    33     if(visit[f1]&&visit[f2]){
    34         time+=a.t;
    35         flot++;
    36     //    printf("%d %d
    ",a.s,a.e);
    37     }
    38     else if(f1!=f2){
    39         if(visit[f1])pre[f2]=f1;
    40         else pre[f1]=f2;
    41     }
    42 }
    43 int main(){
    44     int T,temp;
    45     scanf("%d",&T);
    46         while(T--){
    47             initial();
    48             scanf("%d%d",&n,&m);
    49             for(int i=0;i<n-1;i++){
    50                 scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].t);
    51             }
    52             ///qsort(dt,n-1,sizeof(dt[0]),cmp);
    53             sort(dt,dt+n-1,cmp);
    54             for(int i=0;i<m;i++){
    55                 scanf("%d",&temp);
    56                 visit[temp]=1;
    57             }
    58             for(int i=0;i<n-1;i++){
    59                 merge(dt[i]);
    60                 if(flot==m-1)break;
    61             }
    62             printf("%I64d
    ",time);
    63         }
    64     return 0;
    65 }
  • 相关阅读:
    Linux之文件处理命令
    Linux基础命令
    rip实验
    Linux基础之磁盘分区
    mysql安装
    centos Apache、php、mysql默认安装路径
    You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.
    Wrong permissions on configuration file, should not be world writable!
    机器会学习么 学习总结
    实验 5 Spark SQL 编程初级实践
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4727917.html
Copyright © 2011-2022 走看看