码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10462 Is There A Second Way Left?(次小生成树)

时间:2015-02-14 13:43:16      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

 
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Status

Description

Problem J

Is There A Second Way Left ?

Input: standard input

Output: standard output

Time Limit: 3 seconds

 

Nasa, being the most talented programmer of his time, can‘t think things to be so simple. Recently all his neighbors have decided to connect themselves over a network (actually all of them want to share a broadband internet connection :-)). But he wants to minimize the total cost of cable required as he is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second way left. I mean, he wants to know the second best cost (if there is any which may be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer. Take the challenge (if you are brave enough)..............

 

Input

Input starts with an integer t<=1000 which denotes the number of test cases to handle. Then follows t datasets where every dataset starts with a pair of integers v(1<=v<=100) and e(0<=e<=200). v denotes the number of neighbors and e denotes the number of allowed direct connections among them. The following e lines contain the description of the allowed direct connections where each line is of the form "start end cost", where start and end are the two ends of the connection and cost is the cost for the connection. All connections are bi-directional and there may be multiple connections between two ends.


Output

There may be three cases in the output - 1. no way to complete the task, 2. There is only one way to complete the task, 3. There are more than one way. Output "No way" for the first case, "No second way" for the second case and an integer c for the third case where c is the second best cost. Output for a case should start in a new line.

 

Sample Input

4
5 4
1 2 5
3 2 5
4 2 5
5 4 5
5 3
1 2 5
3 2 5
5 4 5
5 5
1 2 5
3 2 5
4 2 5
5 4 5
4 5 6
1 0

Sample Output

Case #1 : No second way
Case #2 : No way
Case #3 : 21
Case #4 : No second way

Author : Mohammad Sajjad Hossain

The Real Programmers‘ Contest-2
 
 
裸求次小生成树,用 边的数目判是否存在生成树
 
技术分享
#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 ;
}

void Work() {
    memset( vis , false , sizeof vis ) ;        
    sort( e , e + m );
    cnt = 0 ;
    int ans = mst(-1);
    if( cnt != n - 1 ) {
        cout << "No way" << endl ;
        return ;
    }
    int tag = 0 ; ans = 1e28 ;
    for( int i = 0 ; i < m ; ++i ) if( vis[i] ) {    
        cnt = 0 ; int tmp = mst(i) ;
        if( cnt == n - 1 ) {
            ans = min( ans, tmp );
            tag = 1 ;
        }
    }
    if( !tag )cout << "No second way" << endl ;
    else cout << ans << endl ;
}

int main () {
    //freopen("in.txt","r",stdin);
    int _ , cas = 1 ; cin >> _ ;
    while( _-- ) {    
        cin >> n >> m ;
        for( int i = 0 ; i < m ; ++i ){
            cin >> e[i].u >> e[i].v >> e[i].w ;
        }
        cout << "Case #" << cas++ << " : " ;
        Work();
    }
}
View Code

 

UVA 10462 Is There A Second Way Left?(次小生成树)

标签:

原文地址:http://www.cnblogs.com/hlmark/p/4291467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!