The Unique MST (判断是否存在多个最小生成树)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24058 | Accepted: 8546 |
Description
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
Output
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<stdio.h>
#include<string.h>
const int INF=0x3f3f3f3f;
const int MAXN=;
int vis[MAXN],map[MAXN][MAXN],low[MAXN];
int ans,N,answer;
void prime(){
memset(vis,,sizeof(vis));
int flot=,temp,k;
answer=;
ans=;
vis[]=;
for(int i=;i<=N;i++)low[i]=map[][i];
for(int i=;i<=N;i++){
temp=INF;
for(int j=;j<=N;j++)
if(!vis[j]&&temp>low[j])temp=low[k=j];
if(temp==INF){
if(flot!=N)ans=;//因为这题本来就是联通的,所以这句话可以省略;
// printf("flot=%d,N=%d\n",flot,N);
break;
}
int x=;
for(int j=;j<=N;j++){
if(vis[j]&&map[k][j]==temp)x++;
if(x>)break;
}//这个for循环的作用就是,当前已经找到距离这个图最小的点了,然后判断到这个最小点的距离为temp也就是最小值的点有几个,如果点数大于一则有多个最小生成树;
if(x>){
ans=;
break;
}
answer+=temp;
vis[k]=;
flot++;
for(int j=;j<=N;j++){
if(!vis[j]&&low[j]>map[k][j])low[j]=map[k][j];
}
}
}
int main(){
int T,M,a,b,c;
scanf("%d",&T);
while(T--){
memset(map,INF,sizeof(map));
scanf("%d%d",&N,&M);
while(M--){
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])map[a][b]=map[b][a]=c;
}
prime();
if(ans)printf("%d\n",answer);
else printf("Not Unique!\n");
}
return ;
}
The Unique MST (判断是否存在多个最小生成树)的更多相关文章
- POJ-1679 The Unique MST (判断最小生成树的唯一性)
<题目链接> 题目大意: 给定一张无向图,判断其最小生成树是否唯一. 解题分析: 对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边:用kruskal求出MST. 如果MST中无标 ...
- poj 1679 The Unique MST 判断最小生成树是否唯一(图论)
借用的是Kruskal的并查集,算法中的一点添加和改动. 通过判定其中有多少条可选的边,然后跟最小生成树所需边做比较,可选的边多于所选边,那么肯定方案不唯一. 如果不知道这个最小生成树的算法,还是先去 ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- POJ 1679 The Unique MST (次小生成树)题解
题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...
随机推荐
- C#多显示器转换的两种方法——SetWindowPos,Screen
原文 http://blog.csdn.net/hejialin666/article/details/6057551 实现多屏显示目的:一般情况下是一个电脑显示屏,外接一个电视显示屏.在电脑上显示的 ...
- UDP包的大小与MTU
在进行UDP编程的时候,我们最容易想到的问题就是,一次发送多少bytes好?当然,这个没有唯一答案,相对于不同的系统,不同的要求,其得到的答案是不一样的,我这里仅对像ICQ一类的发送聊天消息的情况作分 ...
- linux下so动态库一些不为人知的秘密(中)
上一篇(linux下so动态库一些不为人知的秘密(上))介绍了linux下so一些依赖问题,本篇将介绍linux的so路径搜索问题. 我们知道linux链接so有两种途径:显示和隐式.所谓显示就是程序 ...
- .net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象
原文地址:http://www.cnblogs.com/fannyatg/archive/2012/04/16/2451611.html ------------------------------- ...
- JIRA官方:JIRA源代码集成
防火墙后的Git 使用Atlassian Stash创建和管理Git存储库,设置细粒度的权限并在代码上协作.这一切—安全.快速.可靠,更重要的是,可以部署在防火墙后面.JIRA问题关键字自动将JIRA ...
- flex——将Sprite控件添加到FLEX UI中
在Flex的帮助文档里,有很多例子都是扩展Sprite类的.如果想把这些实例添加到你的s:Application中,如:addChild(DisplayObject ),肯定会出错.错误的大致意思是: ...
- Js节点属性与方法
属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型 Definition 以D ...
- GCC单独编译host/examples/ tx_waveforms.cpp
1.编译 须要链接uhd库和boost_program_options库以及boost_thread库: g++ tx_waveforms.cpp -o a -luhd -lboost_program ...
- 8QQ消息框
1 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...
- L8_2
4.留下pid为12345的那个sh进程,杀死系统中所有其它sh进程 ps –ef|grep sh |awk ‘{if($2!=”12345”) {print “kill “$2}}’ >kil ...