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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • fprintf函数的用法_itoa函数

    fprintf函数的用法_itoa函数fprintf()用于文件操作#includeintfprintf(FILE*stream,constchar*format,…);fprintf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件.

    2022年10月19日
    2
  • java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject解决办法

    java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject解决办法java.lang.ClassCastException:java.lang.Stringcannotbecasttocom.alibaba.fastjson.JSONObjectatcom.alibaba.fastjson.JSONObject.getJSONObject(JSONObject.java:109)ResultModel>rs=

    2022年7月16日
    89
  • wince屏保时钟_下载机械时钟动态壁纸

    wince屏保时钟_下载机械时钟动态壁纸今天我们为大家分享一下最近某音非常火的电子时钟屏保,让你的电脑屏保动起来,而且随着时间流逝而变化(作为一个时间观念强的人,一定会看着屏保更加惜时如金),让你的电脑锁屏与众不同,瞬间黑科技感十足!今天的主角就是WordClock,Windows系统和Mac都有的!适用系统:Windows系统、Mac系统演示电脑:联想ThinkPadT440Windows10系统软件名称:W…

    2022年9月29日
    3
  • 查看服务器外网ip

    查看服务器外网ip如果是桌面系统,想知道自己电脑的外网IP比较容易,用浏览器访问www.ip138.com,就可以了。而服务器放在机房,没有浏览器这号东西,就比较麻烦了。用traceroute又看不出来。偶然间,找到了一个方法可以查看服务器的外网IP。[javascript] viewplaincopy[zhou@localhost ~]$ wget htt

    2022年6月2日
    28
  • Vue substring截取字符串报错

    Vue substring截取字符串报错这是我查出来的订单信息对象,当我只需要显示用户电话的前三位和后四位时,就需要对订单进行截取。可是当我这样截取之后,效果是出来了,只是控制台依然报错;原因:因为数据是异步加载的,当数据还没出来的时候,数据是空的,所以会报错解决办法:这是我一开始用来保存查出来的数据对象。但是这样依然会报错。我们只需要把这个对象需要截取的那个属性一开始赋值为空,这样就不会报错了。…

    2022年5月23日
    36
  • 华为服务器重装操作系统,华为服务器安装操作系统[通俗易懂]

    华为服务器重装操作系统,华为服务器安装操作系统[通俗易懂]《华为服务器安装操作系统》由会员分享,可在线阅读,更多相关《华为服务器安装操作系统(24页珍藏版)》请在人人文库网上搜索。1、华为服务器安装操作系统1.把网线接到服务器管理口上,2288服务器管理口在服务器背后中下部位置,有Mgmt的指示字样。5885服务器管理口在服务器背后右中部位置,有Mgmt的指示字样。2.另一端连接到电脑上,并配上ip地址,192.168.2.1段地址。3.在浏览器输入管理…

    2022年9月1日
    6

发表回复

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

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