UVA 10600 ACM contest and Blackout(次小生成树)

UVA 10600 ACM contest and Blackout(次小生成树)

ACM Contest and Blackout Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit
Status

Description

 

In order to prepare the “The First National ACM School Contest”(in 20??) the major of the city decided to provide all the schools with a reliable source of power. (The major is really afraid of blackoutsJ). So, in order to do that, power station “Future” and one school (doesn’t matter which one) must be connected; in addition, some schools must be connected as well.

 

You may assume that a school has a reliable source of power if it’s connected directly to “Future”, or to any other school that has a reliable source of power. You are given the cost of connection between some schools. The major has decided to pick out two the cheapest connection plans – the cost of the connection is equal to the sum of the connections between the schools. Your task is to help the major – find the cost of the two cheapest connection plans.

 

Input

The Input starts with the number of test cases, T (1£T£15) on a line. Then T test cases follow. The first line of every test case contains two numbers, which are separated by a space, N (3£N£100) the number of schools in the city, and M the number of possible connections among them. Next M lines contain three numbers Ai, Bi, Ci , where Ci  is the cost of the connection (1£Ci£300) between schools Ai  and Bi. The schools are numbered with integers in the range 1 to N.

 

Output

For every test case print only one line of output. This line should contain two numbers separated by a single space – the cost of two the cheapest connection plans. Let S1 be the cheapest cost and S2 the next cheapest cost. It’s important, that S1=S2 if and only if there are two cheapest plans, otherwise S1£S2. You can assume that it is always possible to find the costs S1 and S2..

 

Sample Input

Sample Output

2

5 8

1 3 75

3 4 51

2 4 19

3 2 95

2 5 42

5 4 31

1 2 9

3 5 66

9 14

1 2 4

1 8 8

2 8 11

3 2 8

8 9 7

8 7 1

7 9 6

9 3 2

3 4 7

3 6 4

7 6 2

4 6 14

4 5 9

5 6 10

110 121

37 37

 

裸的次小生成树 , 要注意下求次小生成树的时候 要符合 :边数 = 点数 – 1

UVA 10600 ACM contest and Blackout(次小生成树)
UVA 10600 ACM contest and Blackout(次小生成树)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>

using namespace std ;
typedef long long LL ;
const int N = 1010;
const int M = 1000010;
struct edge {
    int u , v ,w ;
    bool operator < ( const edge &a ) const {
        return w < a.w ;
    }
}e[M];
int n , m , fa[N] , cnt ;
bool vis[M] ;

int find( int k ) { return k == fa[k] ? k : find(fa[k]); }
int mst( int ban ) {
    int ans = 0 ;
    for( int i = 0 ; i <= n ; ++i ) fa[i] = i ;
    for( int i = 0 ; i <m ; ++i ) {
        int fu = find( e[i].u ) , fv = find( e[i].v );
        if( fu == fv || i == ban ) continue ;
        fa[fv] = fu;
        ans += e[i].w ;
        if( ban == -1 ) vis[i] = true ;
        cnt++ ;
    }
    return ans ;
}

int main () {
    //freopen("in.txt","r",stdin);
    int _ ; cin >> _ ;
    while( _-- ) {    
        cin >> n >> m ;
        for( int i = 0 ; i < m ; ++i ){
            cin >> e[i].u >> e[i].v >> e[i].w ;
        }
        memset( vis , false , sizeof vis ) ;
        sort( e , e + m );
        cout << mst(-1) << ' ' ;
        int ans = (1<<28) ;
        for( int i = 0 ; i < m ; ++i ) if( vis[i] ) {
            cnt = 0 ;
            int tmp = mst(i) ;
            if( cnt == n - 1 ) ans = min( ans , tmp ) ;
        }
        cout << ans << endl ;
    }
}

View Code

 

转载于:https://www.cnblogs.com/hlmark/p/4291464.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109547.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • OpenCV繪圖的實作-cvCircle,cvEllipse,cvEllipseBox

    OpenCV繪圖的實作-cvCircle,cvEllipse,cvEllipseBox转自:http://www.90in.net/space-4-do-blog-id-10218.htmlOpenCV繪圖的實作-cvCircle,cvEllipse,cvEllipseBox圓的製作,要給他目標圖片,圓心座標,半徑,其他參數等.cvCircle()函式的實作#include&lt;cv.h&gt;#include&lt;highgui.h&gt;IplImag…

    2022年7月19日
    15
  • PyQt5布局管理之QVBoxLayout(二)[通俗易懂]

    PyQt5布局管理之QVBoxLayout(二)[通俗易懂]QVBoxLayout前言采用QVBoxLayout类,按照从上到下的顺序添加控件本节内容较少,演示两个实例,便于明白QVBoxLayout(垂直布局)的使用实例:QVBoxLayout的正常使用importsysfromPyQt5.QtWidgetsimportQApplication,QWidget,QVBoxLayout,QPushButt…

    2022年6月16日
    67
  • Mac 安装Yarn「建议收藏」

    Mac 安装Yarn「建议收藏」前言近期学习ant-design-vue-pro,需要使用Yarn安装相关依赖包,以下说说在Mac上安装Yarn的尝试几种安装方式使用homebrew安装brewinstallyarn等待许久后,得到如下错误,尝试无数次后放弃,建议在良好的网络环境下尝试脚本安装curl-o–Lhttps://yarnpkg.com/install.sh|bash安装截图:简单快速,强烈建议尝试,尤其适用在国内网络环境下适用。总结homebrew适宜在网络环境良好的情况下使

    2022年5月26日
    34
  • C/C++之makefile写法

    C/C++之makefile写法参考:https://www.cnblogs.com/owlman/p/5514724.html什么是makefileMakefile文件描述了整个工程的编译、连接等规则。其中包括:工程中的哪些源文件需要编译以及如何编译、需要创建那些库文件以及如何创建这些库文件、如何最后产生我们想要的可执行文件。尽管看起来可能是很复杂的事情,但是为工程编写Makefile的好处是能够使用一行命令来完成…

    2022年5月1日
    40
  • mysql面试必会6题经典_经典sql面试题及答案第7期

    mysql面试必会6题经典_经典sql面试题及答案第7期deletefromsalarywherenotexists(selectwidfromworkerwhereworker.wid=salary.wid)a.直接创建索引和间接创建索引直接创建:使用sql语句创建间接创建:定义主键约束或者唯一性键约束,可以间接创建索引,主键默认为唯一索引。b.普通索引和唯一性索引普通索引:CREATEINDEXmycolumn_i…

    2022年6月28日
    24
  • 机器学习两种方法——监督学习和无监督学习(通俗理解)「建议收藏」

    机器学习两种方法——监督学习和无监督学习(通俗理解)「建议收藏」监督学习与无监督学习

    2022年5月28日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号