zoukankan      html  css  js  c++  java
  • poj 1679(次小生成树)

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 26454   Accepted: 9457

    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!
    题意:判断最小生成树是否唯一
    题解:直接求次小生成树。
    次小生成树求法戳这里:http://blog.csdn.net/niushuai666/article/details/6925258
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    const int N = 505;
    const int INF = 99999999;
    int graph[N][N];
    int n,m;
    int path[N][N],pre[N],low[N]; ///path[i][j]用于记录i到j路径上的权值最大的边
    bool vis[N],used[N][N];
    int prim(int pos,int n){
        memset(used,false,sizeof(used));
        memset(vis,false,sizeof(vis));
        memset(path,0,sizeof(path));
        vis[pos]=true;
        int cost = 0;
        for(int i=1;i<=n;i++){
            low[i]= graph[pos][i];
            pre[i]=1;
        }
        low[pos]=1;
        for(int i=1;i<n;i++){
            int Min = INF;
            for(int j=1;j<=n;j++){
                if(!vis[j]&&low[j]<Min){
                    pos = j;
                    Min = low[j];
                }
            }
            used[pre[pos]][pos] = used[pos][pre[pos]] = true;
            cost+= Min;
            vis[pos] = true;
            for(int j=1;j<=n;j++){
                if(vis[j]&&j!=pos){ ///求从pos - j路径上最大权的边
                    path[pos][j] = path[j][pos] = max(low[pos],path[j][pre[pos]]);
                }
                if(!vis[j]&&low[j]>graph[pos][j]){
                    low[j]=graph[pos][j];
                    pre[j] = pos;
                }
            }
        }
        return cost;
    }
    int main()
    {
        int tcase;
        scanf("%d",&tcase);
        while(tcase--){
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++) {
                    if(i==j) graph[i][j]=0;
                    else graph[i][j] = INF;
                }
            }
            for(int i=0;i<m;i++){
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                graph[a][b] = graph[b][a] = c;
            }
            int cost = prim(1,n);
            int res = INF;
            for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){
                    if(!used[i][j]) res = min(res,cost+graph[i][j]-path[i][j]);
                }
            }
            if(res==cost) printf("Not Unique!
    ");
            else printf("%d
    ",cost);
        }
    }
  • 相关阅读:
    VBA的程序终止、退出语句块、分支及错误处理(End, exit, on error go to in VBA
    关于session中的cookie提取
    Python爬虫 —— 知乎之selenium模拟登陆获取cookies+requests.Session()访问+session序列化-转
    tar 解压命令出现如下错误
    操作系统 ----- 段错误(核心转储)
    c++游戏入门基础
    数据结构算法
    point(指针)
    《淘宝网》质量属性简析
    《架构漫谈》读后感
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5478600.html
Copyright © 2011-2022 走看看