zoukankan      html  css  js  c++  java
  • HDU 3371

    最小生成树,一道边有点多的题目,需要优化。

    Description

    In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  
     

    Input

    The first line contains the number of test cases.
    Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
    To make it easy, the cities are signed from 1 to n.
    Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
    Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
     

    Output

    For each case, output the least money you need to take, if it’s impossible, just output -1.
     
     1 #include<iostream>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<math.h>
     5 #include<stdio.h>
     6 #define maxn 500+10
     7 #define maxe 25000+100
     8 using namespace std;
     9 int r[maxe];
    10 int u[maxe];
    11 int v[maxe];
    12 int w[maxe];
    13 int p[maxn];
    14 int cmp(const int i,const int j){return w[i]<w[j];}
    15 int find(int x)
    16 {
    17     return p[x]==x?x:p[x]=find(p[x]);
    18 }
    19 void merge(int x,int y)
    20 {
    21     int xx=find(x),yy=find(y);
    22     p[xx]=yy;
    23 }
    24 int main()
    25 {
    26     int cnt;
    27     int t;
    28     scanf("%d",&t);
    29     while(t--)
    30     {
    31     int n,m,k;
    32     while(~scanf("%d%d%d",&n,&m,&k))
    33     {
    34         for(int i=1;i<=n;i++)
    35         {
    36             p[i]=i;
    37         }
    38         int ans=0;
    39         for(int i=0;i<m;i++)
    40         {
    41             scanf("%d%d%d",&u[i],&v[i],&w[i]);
    42         }
    43         for(int i=0;i<k;i++)
    44         {
    45             int num;
    46             int first;
    47             scanf("%d",&num);
    48             scanf("%d",&first);
    49             for(int i=1;i<num;i++)
    50             {
    51                 int now;
    52                 scanf("%d",&now);
    53                 merge(first,now);merge(now,first);//正反两次才能保证全部连接到根节点上
    54             }
    55         }
    56         for(int i=0;i<m;i++) r[i]=i;
    57         sort(r,r+m,cmp);
    58         int edgenum=0;//edgenum是优化的关键。一是提前跳出循环,二是不用再判断图的联通性
    59         for(int i=1;i<=n;i++)
    60         if (p[i]==i) edgenum++;
    61         for(int i=0;i<m;i++ && edgenum>1)
    62         {
    63             int e=r[i];int x=find(u[e]);int y=find(v[e]);
    64             if (x!=y)
    65             {
    66                 {ans+=w[e];merge(x,y);}
    67                 edgenum--;
    68             }
    69         }
    70         if (edgenum>1)printf("-1
    ");else printf("%d
    ",ans);
    71 
    72     }
    73     }
    74     return 0;
    75 }
    View Code

    注意要用c++交,g++交会T

  • 相关阅读:
    极大/小搜索,alpha/beta剪枝
    消息系统
    渲染主线程都在干什么
    好玩的虚拟CPU执行代码
    好玩的隐藏属性
    好玩的对象存储
    透视投影矩阵
    绕任意轴旋转
    视图变换
    正交投影矩阵
  • 原文地址:https://www.cnblogs.com/little-w/p/3351579.html
Copyright © 2011-2022 走看看