zoukankan      html  css  js  c++  java
  • (最小生成树 次小生成树)The Unique MST -- POJ -- 1679

    链接:

    http://poj.org/problem?id=1679

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/K

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 24594   Accepted: 8751

    Description

    Given a connected undirected graph, tell if its minimum spanning tree is unique. 

    Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
    1. V' = V. 
    2. T is connected and acyclic. 

    Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

    Input

    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

    Output

    For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

    Sample Input

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    

    Sample Output

    3
    Not Unique!

    代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int N = 110;
    const int INF = 0x3f3f3f3f;
    
    int J[N][N], dist[N], pre[N], Max[N][N], n, m;
    int use[N][N];
    bool vis[N];
    
    int prim()
    {
        int i, j, ans=0;
        memset(Max, 0, sizeof(Max));
        memset(use, 0, sizeof(use));
        memset(dist, 0, sizeof(dist));
        memset(vis, false, sizeof(vis));
        vis[1]=1;
    
        for(i=1; i<=n; i++)
        {
            dist[i]=J[1][i];
            pre[i]=1;
        }
    
        for(i=1; i<n; i++)
        {
            int index=1, MIN=INF;
            for(j=1; j<=n; j++)
            {
                if(!vis[j] && dist[j]<MIN)
                {
                    MIN=dist[j];
                    index=j;
                }
            }
            ans += MIN;
            vis[index]=1;
            use[index][pre[index]]=use[pre[index]][index]=1;
    
            for(j=1; j<=n; j++)
            {
                if(vis[j] && j!=index)
                {
                    Max[index][j]=Max[j][index]=max(Max[j][pre[index]], dist[index]);
                }
                if(!vis[j] && dist[j]>J[index][j])
                {
                    dist[j]=J[index][j];
                    pre[j]=index;
                }
            }
        }
        return ans;
    }
    
    int cc(int s)
    {
        int MIN=INF, i, j;
        for(i=1; i<=n; i++)
        for(j=i+1; j<=n; j++)
        {
           if(!use[i][j] && J[i][j]!=INF)
           {
               MIN = min(MIN, s-Max[i][j]+J[i][j]);
           }
        }
        return MIN;
    }
    
    int main ()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int i, j, a, b, c;
            scanf("%d%d", &n, &m);
    
            for(i=1; i<=n; i++)
            {
                J[i][i]=0;
                for(j=1; j<i; j++)
                 J[i][j]=J[j][i]=INF;
            }
    
    
            for(i=1; i<=m; i++)
            {
                scanf("%d%d%d", &a, &b, &c);
                J[a][b]=J[b][a]=c;
            }
    
            int ans1=prim();
            int ans2=cc(ans1);
            if(ans1==ans2)
                printf("Not Unique!
    ");
            else
                printf("%d
    ", ans1);
        }
        return 0;
    }
    勿忘初心
  • 相关阅读:
    微信公众平台二次开发需要配置的几个地址与参数
    Extjs4.1+desktop+SSH2 定义程序入口
    Extjs4.1+desktop+SSH2 搭建环境 项目能跑起来
    Liunx开发(Extjs4.1+desktop+SSH2超强视频教程实践)(2)
    Liunx开发(Extjs4.1+desktop+SSH2超强视频教程实践)(1)
    增量补丁打包器(我也不是想这么干的)
    部署git服务器(Windows Server 2008)
    测试发布(maven-assembly-plugin看好你哦)
    工作流性能优化(敢问activiti有扩展性?)(3)
    工作流性能优化(敢问activiti有扩展性?)(2)
  • 原文地址:https://www.cnblogs.com/YY56/p/4735124.html
Copyright © 2011-2022 走看看