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;
    }
    勿忘初心
  • 相关阅读:
    iView -- TimePicker 自定义修改时间选择器选择时间面板样式
    Go语言--容器:存储和组织数据的方式--数组、切片
    php递归实现一维数组转为指定树状结构 --- 省市区处理
    Go语言--基础语法笔记
    Mongodb 安装错误汇总
    GIt -- git push 远程分支老是需要重新输入公钥密码问题处理?
    GIt -- fatal: refusing to merge unrelated histories 问题处理
    Linux -- Centos6 yum安装相关问题与处理
    Linux -- Xshell ,Xftp远程连接中文乱码怎么解决?
    Laravel 多数据库配置及查询操作
  • 原文地址:https://www.cnblogs.com/YY56/p/4735124.html
Copyright © 2011-2022 走看看