The Unique MST

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 8
Problem 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
PKU


有时候最小生成树不止一个,举个例子,某一个点以min进入当前的集合,但是进去之后又发现他与好几个在集合中的点的距离都是min,这也就说明,他连哪一个点都是一样的,所以此时的最小生成树不止一个。代码:


#include<stdio.h>
#include<string.h>
#define INF 0xfffffff
int n,m;
int map[1010][1010],dis[1010],mark[1010];
void prim()
{
int i,min,flag,j,sum=0,k,w=1;
memset(mark,0,sizeof(mark));
mark[1]=1;
for(i=2;i<=n;i++)
{
min=INF;
flag=-1;
k=0;
for(j=1;j<=n;j++)
{
if(!mark[j]&&map[1][j]<min)
{
min=map[1][j];
flag=j;
}
}
for(j=1;j<=n;j++)
{
if(mark[j]&&map[flag][j]==min)
{/*因为mark[1]已经标记过了,所以此时如果出现两个及其以上的点,
到flog的距离都是min,当然,仅限于已经连过的点,此时最小生成树就不止一种*/
k++;
}
}
//printf("%d ",k);
if(k>=1)
{
w=0;
break;
}
mark[flag]=1;
sum+=min;
for(j=1;j<=n;j++)
{
if(!mark[j]&&map[1][j]>map[flag][j])
map[1][j]=map[flag][j];
}
}
if(w)
printf("%d\n",sum);
else
printf("Not Unique!\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j;
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
map[i][j]=INF;
//map[i][i]=0;
}
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
map[a][b]=map[b][a]=c;
}
prim();
}
}

最新文章

  1. 天气预报API(一):全国城市代码列表(“旧编码”)
  2. 响应式布局1--媒体查询和-webkit-min-device-pixel-ratio
  3. Python-类的继承
  4. java实例--海盗的最优方案
  5. 将ubuntu12.04中,gcc4.6/g++4.6版本降低到gcc4.4/g++4.4.
  6. GNOME Shell叫板Ubuntu Unity:优劣PK
  7. jni使用
  8. eval函数
  9. js解析json,js转换json成map,获取map的key,value
  10. MongoDB每64位版本下载
  11. Asp.net - The type or namespace name &#39;App_Code&#39; does not exist in the namespace &#39;xxx&#39; (are you missing an assembly reference?)
  12. GDI+: Curved Shapes
  13. Spring MVC Cookie example
  14. 配置jndi服务,javax.naming.NamingException的四种情况
  15. Node填坑教程——简易http服务器
  16. sqlDeveloper连接oracle
  17. 关于C#的一些小知识
  18. 图片的Base64编码
  19. Maven 专题
  20. PAT甲题题解-1121. Damn Single (25)-水题

热门文章

  1. 06-联系人管理(xib应用)
  2. Application windows are expected to have a root view controller at the end of application launch
  3. C# 多线程系列(二)
  4. 努比亚(nubia) V18 NX612J 解锁BootLoader 并刷入recovery ROOT
  5. mvc重定向
  6. 【剑指Offer】25、复杂链表的复制
  7. C# DataGridView 使用
  8. js setTimeout函数
  9. DJANGO之自定义模板过滤器
  10. Spring boot 前后台分离项目 怎么处理spring security 抛出的异常