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)
上一篇 2021年9月4日 上午7:00
下一篇 2021年9月4日 上午8:00


相关推荐

  • mysql截取前几个字符串_MySQL 截取字符串函数的sql语句

    mysql截取前几个字符串_MySQL 截取字符串函数的sql语句1、left(name,4)截取左边的4个字符列:SELECTLEFT(201809,4)年结果:20182、right(name,2)截取右边的2个字符SELECTRIGHT(201809,2)月份结果:093、SUBSTRING(name,5,3)截取name这个字段从第五个字符开始只截取之后的3个字符SELECTSUBSTRING(‘成都融资事业部’,5,3)结果:事业部4、…

    2022年6月1日
    42
  • java arraylist遍历_java 遍历arrayList的四种方法

    java arraylist遍历_java 遍历arrayList的四种方法importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;publicclassArrayListDemo{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(“luojiahui”);list.add(“…

    2022年7月22日
    14
  • 【Python 多进制转换】——数值多进制转换bin、oct、int、hex(2进制、4进制、8进制、10进制、16进制、32进制)

    【Python 多进制转换】——数值多进制转换bin、oct、int、hex(2进制、4进制、8进制、10进制、16进制、32进制)点个赞留个关注吧 学习资料下载链接 百度网盘提取码 tha8 进制转换计算工具含源文件导入模块 importtkinte importtkinte ttkimport 整个框架的主结构 root Tk root title 贱工坊 进制转换计算 程序的标题名称 root geometry 580×400 512 288 窗口的大小及页面的显

    2026年3月20日
    2
  • MATLAB 矢量图(风场、电场等)标明矢量大小的方法——箭头比例尺及风矢杆图的绘制

    MATLAB 矢量图(风场、电场等)标明矢量大小的方法——箭头比例尺及风矢杆图的绘制作者:中国科学院大气物理研究所律成林MATLAB中标明矢量图中矢量大小的方法:绘制箭头比例尺,或绘制风矢杆图。m_vec函数绘制的箭头长度仅与矢量大小本身有关。本人基于m_vec绘制结果,开发了一个可以在Figure内任意位置为指定的矢量图绘制箭头比例尺的函数——m_arrow_scale2,本文已包含该函数的代码,该函数考虑了方方面面,如文本标注、位置、字体等参数,且预设了很多参数供使用者选择,选择余地非常多,使用起来非常方便,功能也较为强大。本着“授人以渔”的原则,倾注了本人对MATLAB深刻理解。

    2022年6月28日
    122
  • java发送邮件-模板

    java发送邮件-模板今天写完了一个关于使用模板发送邮件的代码,作为例子保存着,希望以后用得着,也希望能够帮助到需要帮助的人以163网易邮箱为例,使用java发送邮件,发送以邮件时使用模板(.ftl文件转换为html)发送邮件内容,并附带上附件,可抄送给多个人。项目的结构目录如下邮箱配置文件mail.properties参数如下#mailsendersettings#forexample:smtp.1

    2022年5月15日
    34
  • myeclipse导入项目jsp报错

    myeclipse导入项目jsp报错js 标识在 jsp 中不能正确识别 myeclipse 在进行 jsp 页面检查时 找不到标识 就报错了 解决办法如下 1 在项目上右键 gt MyEclipse gt ExcludeFromV window gt preferences gt myeclipse gt validation gt JavaScriptva

    2026年3月18日
    2

发表回复

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

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