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;
    }
    勿忘初心
  • 相关阅读:
    一句话解释各种虚拟币的用途
    php 网站301重定向设置代码实战案例
    seo网页加速技术,预加载 DNS Prefetching 详解
    AI赌神称霸德扑的秘密,刚刚被《科学》“曝光”了
    java实现 HTTP/HTTPS请求绕过证书检测代码实现
    pyspider源码解读--调度器scheduler.py
    pyspider操作千万级库,pyspider在对接量级较大库的策略
    尼克《人工智能简史》谈人工智能的历史、现实与未来
    CentOS7使用yum命令安装Java1.8
    php ci nginx 伪静态rewrite配置方法
  • 原文地址:https://www.cnblogs.com/YY56/p/4735124.html
Copyright © 2011-2022 走看看