The Unique MST prim(次小生成树)
来源:互联网 发布:好用的c语言编译器 编辑:程序博客网 时间:2024/06/05 00:33
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 24085 Accepted: 8556
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'.
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
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not Unique!
Source
POJ Monthly--2004.06.27 srbga@POJ
#include<stdio.h>#include<stdlib.h>#include<string.h>#define INF 0x7fffffffint c[110][110];int mark[110];void prim(int n){int i,j,v,min,k,flag,max;int b[110];for(i=1;i<=n;i++){b[i]=c[1][i];}memset(mark,0,sizeof(mark));mark[1]=1;flag=0;max=0;for(i=0;i<n;i++){min=INF;for(j=1;j<=n;j++){if(!mark[j]&&min>b[j]){min=b[j];v=j; }}if(min==INF)break;max+=min;k=0;for(j=1;j<=n;j++){if(mark[j]&&c[v][j]==min)//判断是不是唯一 k++;}if(k>1){ flag=1; break;}mark[v]=1;for(j=1;j<=n;j++){if(!mark[j]&&b[j]>c[v][j])b[j]=c[v][j];}}if(flag)printf("Not Unique!\n");else printf("%d\n",max);}int main(){int n,a,b1,i,v,q,z,j;scanf("%d",&n);while(n--){scanf("%d%d",&a,&b1);for(i=1;i<=a;i++){ for(j=1;j<=a;j++) if(i!=j) c[i][j]=INF; else c[i][j]=0;}for(i=1;i<=b1;i++){scanf("%d%d%d",&q,&z,&v);c[q][z]=c[z][q]=v;}prim(a);}return 0;}
0 0
- POJ 1679 The Unique MST 次小生成树Prim
- The Unique MST prim(次小生成树)
- [次小生成树]The Unique MST
- The Unique MST-次小生成树
- poj 题目1679 The Unique MST (最小生成树,次小生成树 prim)
- poj 1679 - The Unique MST ( 次小生成树 )
- PKU 1679 The Unique MST 【图论】【 次小生成树】
- 1679 The Unique MST //次小生成树
- POJ 1679 The Unique MST 次小生成树
- Kruskal次小生成树 :The Unique MST
- POJ 1679 The Unique MST 次小生成树
- poj 1679 The Unique MST(次小生成树)
- poj 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST 次小生成树
- 【次小生成树】POJ-1679 The Unique MST
- poj 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST(次小生成树&Kruskal)
- poj 1679 The Unique MST 次小生成树算法
- C#数据库的一些基本操作 第一篇
- form中的onblur事件简单的介绍
- linux yum命令详解
- Android开源项目分类汇总
- POJ 1287 Netwoking
- The Unique MST prim(次小生成树)
- SDWebImage源码解析之SDWebImageManager的注解
- 使用 FileSystem JAVA API 对 HDFS 进行读、写、删除等操作
- HDU 5387 Clock
- iOS开发 -- iPhone手机屏幕尺寸
- android常用组件之TextView组件
- 欢迎使用CSDN-markdown编辑器
- c++类的成员变量的初始化顺序
- Robberies(2955)