• UVA10462:Is There A Second Way Left? (判断次小生成树)


    Is There A Second Way Left?

    Description:

    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

    题意:

    看看这个输出就差不多知道了。。先看最小生成树是否存在,然后看次小生成树,如果存在,输出次小生成树的值。

    题解:

    基本上是模板题,直接看代码吧...

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int N = 405;
    int t,n,m;
    int flag1;
    struct Edge{
        int u,v,w;
        bool operator < (const Edge &A)const{
            return w<A.w;
        }
    }e[N];
    int f[N],mp[N][N];
    int d[N][N],dis[N][N];
    int check[N],vis[N],link[N][N];
    int find(int x){
        return f[x]==x?f[x]:f[x]=find(f[x]);
    }
    ll Kruskal(){
        ll ans=0,cnt=0;
        for(int i=0;i<=n+1;i++) f[i]=i;
        for(int i=1;i<=m;i++){
            int u=e[i].u,v=e[i].v;
            int fx=find(u),fy=find(v);
            if(fx==fy) continue ;
            f[fx]=fy;
            vis[i]=1;
            cnt++;
            mp[u][v]=mp[v][u]=1;
            link[u][v]=1;
            ans+=e[i].w;
            dis[u][v]=e[i].w;
        }
        if(cnt!=n-1) flag1=1;
        return ans ;
    }
    void dfs(int u,int fa){
        for(int i=1;i<=n;i++){
            if(check[i]){
                if(link[u][fa]) d[i][u]=d[u][i]=max(d[i][fa],dis[u][fa]);
                else d[i][u]=d[u][i]=max(d[i][fa],dis[fa][u]);
            }
        }
        check[u]=1;
        for(int i=1;i<=n;i++){
            if(mp[u][i] && i!=fa) dfs(i,u);
        }
    }
    int main(){
        scanf("%d",&t);
        int cnt = 0;
        while(t--){
            cnt++;
            scanf("%d%d",&n,&m);
            flag1=0;
            memset(dis,0,sizeof(dis));
            memset(mp,0,sizeof(mp));
            for(int i=1;i<=m;i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                e[i]=Edge{u,v,w};
            }
            sort(e+1,e+m+1);
            memset(d,0,sizeof(d));
            memset(vis,0,sizeof(vis));
            memset(link,0,sizeof(link));
            memset(check,0,sizeof(check));
            ll sum = Kruskal();
            printf("Case #%d : ",cnt);
            if(flag1){
                puts("No way");
                continue ;
            }
            if(m==n-1){
                puts("No second way");
                continue ;
            }
            dfs(1,-1);
            ll ans=INF;
            for(int i=1;i<=m;i++){
                int u=e[i].u,v=e[i].v,w=e[i].w;
                if(vis[i]) continue ;
                ans=min(ans,sum-d[u][v]+w);
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    AT&T不能访问公司网络
    尝鲜:windows 7 来了
    .net控件编程 资料篇
    Annual part 2009
    从Visual studio 2005移出Visual Assist
    不能在IIS 5.1增加应用程序扩展的BUG
    The problem of the user is not associated with a trusted sql server connection 混合登录选项设置的问题
    让我们难忘的那些歌曲
    分享利用VPC防止病毒软件的进入你的windows电脑
    杂读 May 12,2008
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10372319.html
Copyright © 2020-2023  润新知