POJ1679 The Unique MST(次小生成树)
可以依次枚举MST上的各条边并删去再求最小生成树,如果结果和第一次求的一样,那就是最小生成树不唯一。
用prim算法,时间复杂度O(n^3)。
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 111
#define INF (1<<30)
struct Edge{
int u,v;
}edge[MAXN];
int NE; int n,G[MAXN][MAXN];
int lowcost[MAXN],nearvex[MAXN];
int prim(bool statue){
for(int i=;i<=n;++i) lowcost[i]=INF;
lowcost[]=;
int res=;
for(int i=; i<n; ++i){
int u=-,min=INF;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]<min){
min=lowcost[v];
u=v;
}
}
if(u==-) return -;
if(statue && u!=){
edge[NE].u=nearvex[u]; edge[NE].v=u;
++NE;
}
lowcost[u]=-;
res+=min;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]>G[u][v]){
lowcost[v]=G[u][v];
nearvex[v]=u;
}
}
}
return res;
} bool isUnique(int res){
for(int i=; i<NE; ++i){
int tmp=G[edge[i].u][edge[i].v];
G[edge[i].u][edge[i].v]=G[edge[i].v][edge[i].u]=INF;
if(prim()==res) return ;
G[edge[i].u][edge[i].v]=G[edge[i].v][edge[i].u]=tmp;
}
return ;
} int main(){
int t,m,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i){
for(int j=; j<=n; ++j) G[i][j]=INF;
}
for(int i=; i<m; ++i){
scanf("%d%d%d",&a,&b,&c);
G[a][b]=G[b][a]=c;
}
NE=;
int res=prim();
if(isUnique(res)) printf("%d\n",res);
else puts("Not Unique!");
}
return ;
}
有O(n^2)的算法,详见http://www.cnblogs.com/hxsyl/p/3290832.html。
算法,需要求出MST上任意两点路径上的最长边,删除这条边会形成两个连通分支而那两点就分别在这两个连通分支里;
然后依次枚举所有不在MST上的边,删除边上两点路径上的最长边并加入MST,这样就构成另外一颗生成树了,并且是存在该边的前提下的最小生成树。
计算MST任意两点路径上的最长边,可以在prim算法的过程中求出:每当一个点加入T集合前,计算出所有T集合的点到该点的最长边。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111
#define INF (1<<30) int n,G[MAXN][MAXN];
bool vis[MAXN][MAXN];
int lowcost[MAXN],nearvex[MAXN],maxedge[MAXN][MAXN];
int T[MAXN],NT;
int prim(){
memset(vis,,sizeof(vis));
memset(maxedge,,sizeof(maxedge));
NT=;
nearvex[]=;
for(int i=; i<=n; ++i) lowcost[i]=INF;
lowcost[]=; int res=;
for(int i=; i<n; ++i){
int u=-,mincost=INF;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]<mincost){
mincost=lowcost[v];
u=v;
}
} vis[nearvex[u]][u]=vis[u][nearvex[u]]=;
for(int i=; i<NT; ++i) maxedge[T[i]][u]=maxedge[u][T[i]]=max(maxedge[T[i]][nearvex[u]],mincost);
T[NT++]=u; res+=mincost;
lowcost[u]=-;
for(int v=; v<=n; ++v){
if(lowcost[v]!=- && lowcost[v]>G[u][v]){
lowcost[v]=G[u][v];
nearvex[v]=u;
}
}
}
return res;
} int SMST(){
int res=INF;
for(int i=; i<=n; ++i){
for(int j=i+; j<=n; ++j){
if(vis[i][j] || G[i][j]==INF) continue;
res=min(res,G[i][j]-maxedge[i][j]);
}
}
return res;
} int main(){
int t,m,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i){
for(int j=; j<=n; ++j) G[i][j]=INF;
}
while(m--){
scanf("%d%d%d",&a,&b,&c);
G[a][b]=G[b][a]=c;
}
int res=prim();
if(SMST()==) puts("Not Unique!");
else printf("%d\n",res);
}
return ;
}
POJ1679 The Unique MST(次小生成树)的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ-1679 The Unique MST,次小生成树模板题
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- POJ_1679_The Unique MST(次小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- POJ_1679_The Unique MST(次小生成树模板)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23942 Accepted: 8492 D ...
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
随机推荐
- unity3d webplayer 16:9 居中显示模板
原地址:http://www.cnblogs.com/88999660/archive/2013/04/12/3016773.html <!DOCTYPE html PUBLIC "- ...
- 查看现有运行的linux服务器有多少内存条
i161 admin # ssh 192.168.5.209 dmidecode | grep 'Ending Address' -B1 -A2 Starting Address: 0x0000 ...
- 开博一周总结与随谈[thinking of writing blog for one week]
8天前,就是5月19号,突发奇想,觉得应该开个博客记录下自己的学习笔记和心得,更重要的是做个自我梳理和总结.大致看了下国内的博客,最后选定cnblogs.之所以选则cnblogs是因为平时搜到不少好文 ...
- 如何让你的scrapy爬虫不再被ban
前面用scrapy编写爬虫抓取了自己博客的内容并保存成json格式的数据(scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据)和写入数据库(scrapy爬虫成长日记之将抓取内容写入 ...
- javascript onload队列
2014年10月29日 10:40:14 function addLoadEvent(func){ var oldonload = window.onload; if (typeof window.o ...
- MySQL入门书籍和方法分享
本文罗列了一些适用于MySQL及运维入门和进阶使用的书籍. 背景:各大论坛上总是有很多同学咨询想学习数据库,或者是为入行DBA做些准备.几年来作为一个MySQL DBA的成长过程有一些积累和感悟,特此 ...
- canvas实践小实例二 —— 扇形
俗话说:发图不留种,菊花万人捅!我这里想延伸一下:教学不给例,说你是傻逼!哎呀,还挺押韵,嘻嘻,开个玩笑! 我们都讲了四期API的知识了,估计大家看的也是枯燥的很啊,前面的小实例也是太简单,简直不解渴 ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Java for LeetCode 033 Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- c++ template函数的声明和实现需要在同一个文件中
新建一个class C;生成2个文件C.h和C.cpp,在C.h中声明一个函数 template<class T> T stringTo(char* str); 直接用VAssistX的R ...