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


相关推荐

  • JMM简介_英文缩写jmy啥意思

    JMM简介_英文缩写jmy啥意思Java的内存模型JMM(JavaMemoryModel)JMM主要是为了规定了线程和内存之间的一些关系。根据JMM的设计,系统存在一个主内存(MainMemory),Java中所有实例变量都储存在主存中,对于所有线程都是共享的。每条线程都有自己的工作内存(WorkingMemory),工作内存由缓存和堆栈两部分组成,缓存中保存的是主存中变量的拷贝,缓存可能并不总和主存同步,也就是缓存中变量的修改可能没有立刻写到主存中;堆栈中保存的是线程的局部变量,线程之间无法相互直接访问堆栈中的变量。JM

    2025年9月13日
    7
  • Java文件上传实例并解决跨域问题

    Java文件上传实例并解决跨域问题本文内容为Java文件上传实例并解决跨域问题,其中重点讲解了文件上传,MultipartFile接口的使用,配置nginx,以及解决文件上传跨域的问题

    2022年5月14日
    41
  • 游戏建模:3D建模的入门学习方法及技巧

    选一个你感兴趣的模型利用你感兴趣的任何物品或形象的预制模型。选一个可以激发你想象,让你知道清楚知道自己的模型该是什么样子,该怎么动的模型。你可以根据自己的喜好和需要加强现有模型。预制模型可以让你在开始建模之前,体验模型的检查和操作。从简单模型入手从复杂3D模型入手,你可能会备受打击。选一个简单的结构,然后开始学习。你不仅想要学会3D建模的基本知识,还需要慢慢学习掌握不同的工具、技巧。瓶子一样的圆柱体是一个很好的入门模型。或者你可以用更简单的立方体来熟悉所有工具技巧的用法。复杂模型可能会.

    2022年4月3日
    151
  • 经典StNet行为识别网络「建议收藏」

    经典StNet行为识别网络「建议收藏」论文题目:StNet:LocalandGlobalSpatial-TemporalModelingforActionRecognition论文链接:https://arxiv.org/abs/1811.01549代码:https://github.com/PaddlePaddle/models/blob/develop/fluid/PaddleCV/video/model…

    2022年6月21日
    39
  • jsoncpp 不能处理long类型数据

    jsoncpp 不能处理long类型数据

    2021年12月8日
    52
  • Ext4文件系统fsck后损坏修复方法-linux数据恢复案例[通俗易懂]

    Ext4文件系统fsck后损坏修复方法-linux数据恢复案例[通俗易懂]在数据恢复案例开始之前有几个概念需要了解块组:Ext4文件系统的全部空间被划分为若干个块组,每个块组内的结构都是大致相同的。块组描述符表:每个块组都对应一个块组描述符,这些块组描述符统一放在文件系统的前部,称为块组描述符表。每个块组描述符大小为32字节,其主要描述块位图、i-节点位图及i-节点表的地址等信息。超级块(Superblock):用于存储文件系统的配置参数(如块大小、总块数、i-节点数…

    2022年5月25日
    175

发表回复

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

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