zoukankan      html  css  js  c++  java
  • POJ1679 The Unique MST 【次小生成树】

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 20421   Accepted: 7183

    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!
    

    Source


    这题做得好开心,一次AC~

    题意:给定一个联通图,推断最小生成树是否唯一。

    题解:求出最小生成树后再求次小生成树。若次小生成树的长度与最小生成树相等就说明不唯一,否则唯一。

    这是我的第一道次小生成树题,在这里总结下这个算法:

    利用一个矩阵max【】【】表示最小生成树中随意两点路径上的最长边权值(关键!!),在求最小生成树时将已经选上的边标记为已用,求完后。遍历剩下未用的边,这条边若加入到最小树中必然构成回路。所以此时须要去掉原来树中那条回路中的最大值。也就是max矩阵存储的值。所以问题转换成找到一条未用的边,使得它跟相应于max矩阵里的边差值最小。遍历之后,次小生成树的值即为原最小生成树的值加上这个最小的差值。

    #include <stdio.h>
    #include <string.h>
    #include <limits.h>
    #define maxn 102
    #define maxm (maxn * maxn) >> 1
    
    int head[maxn], max[maxn][maxn];
    struct Node{
    	int u, v, cost, next;
    	bool vis;
    } E[maxm];
    bool vis[maxn];
    
    int mini(int a, int b){
    	return a < b ?

    a : b; } int prim(int n, int m) { int u, i, tmp, j, len = 0, count = 0; memset(max, 0x7f, sizeof(max)); memset(vis, 0, sizeof(vis)); vis[1] = 1; while(count < n - 1){ for(i = 1, tmp = INT_MAX; i <= n; ++i){ if(!vis[i]) continue; for(j = head[i]; j != -1; j = E[j].next){ if(vis[E[j].v]) continue; if(E[j].cost < tmp){ tmp = E[j].cost; u = j; } } } ++count; len += tmp; for(i = 1; i <= n; ++i){ if(!vis[i]) continue; max[i][E[u].v] = max[E[u].v][i] = E[u].cost; } vis[E[u].v] = 1; E[u].vis = 1; } return len; } int getSecLen(int n, int m) { int min = INT_MAX, u, v, w; for(int i = 0; i < m; ++i){ if(E[i].vis) continue; u = E[i].u; v = E[i].v; w = E[i].cost; min = mini(min, w - max[u][v]); if(min == 0) return 0; } return min; } int main() { int t, n, m, i, minLen, secLen; scanf("%d", &t); while(t--){ scanf("%d%d", &n, &m); memset(head, -1, sizeof(head)); for(i = 0; i < m; ++i){ scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost); E[i].vis = 0; E[i].next = head[E[i].u]; head[E[i].u] = i; } minLen = prim(n, m); secLen = getSecLen(n, m); if(secLen == 0) printf("Not Unique! "); else printf("%d ", minLen); } return 0; }



  • 相关阅读:
    参考vue.js实现双向绑定的方法理解双向绑定原理(:Object.defineProperty和发布-订阅模式)
    不错的站点 博文
    使用C#动态生成Word文档/Excel文档的程序测试通过后,部署到IIS服务器上,不能正常使用的问题解决方案
    详解HTML<head> 头标签元素的意义以及使用场景
    css奇特用法之 IMG添加背景图片配合显示--效果惊艳
    .net面试题-15k+左右
    微信小程序IOS真机调试发生了SSL 错误,无法建立与该服务器的安全连接
    微信小程序自定义组件-下拉框
    微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以
    微信小程序循环中点击一个元素,其他的元素不发生变化,类似点击一个循环中的语音,其他的不发生点击事件
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6780590.html
Copyright © 2011-2022 走看看