• (最小生成树 次小生成树)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;
    }
    勿忘初心
  • 相关阅读:
    使用 requests 维持会话
    使用 requests 发送 POST 请求
    使用 requests 发送 GET 请求
    requests 安装
    使用 urllib 分析 Robots 协议
    使用 urllib 解析 URL 链接
    使用 urllib 处理 HTTP 异常
    使用 urllib 处理 Cookies 信息
    使用 urllib 设置代理服务
    按单生产程序发布
  • 原文地址:https://www.cnblogs.com/YY56/p/4735124.html
Copyright © 2020-2023  润新知