POJ1679The Unique MST(次小生成树)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 25203 | Accepted: 8995 |
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! 题意:问一棵最小生成树是否唯一
找次小生成树,如果相等不唯一,否则唯一 次小生成树:就是最小生成树换一条边而成的生成树;用maxd【x】【y】存储最小生成树两个节点(x,y)路径中最大的那条边的权值,也就是最小生成树中x-z-...-y中那个最大的那一个权值,然后就可以换边了,到底换哪一个边就从不在生成树中的边一个一个枚举咯,假设x-y,如果要用x-y替换x-z...-y肯定得替换x-z...-y中权值最大的那条边才能让最后得到结果只比x-z...-y小一点,也就是仅次于
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX = ;
const int INF = ;
int n,m,ans,cnt;
int edge[MAX][MAX],vis[MAX],used[MAX][MAX],dist[MAX];
int pre[MAX],maxd[MAX][MAX];
void prime()
{
memset(vis,,sizeof(vis));
memset(used,false,sizeof(used));
memset(maxd,,sizeof(maxd));
for(int i = ; i <= n; i++)
{
dist[i] = edge[][i];
pre[i] = ;
}
dist[] = ;
vis[] = ;
pre[] = -;
for(int i = ; i < n; i++)
{
int minn = INF,pos = -;
for(int j = ; j <= n; j++)
{
if(vis[j] == && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
if(minn == INF)
{
ans = INF;
return;
}
ans += minn;
vis[pos] = ;
used[pos][pre[pos]] = used[ pre[pos] ][pos] = true;
for(int j = ; j <= n; j++)
{
if(vis[j])
maxd[j][pos] = maxd[pos][j] = max(dist[pos], maxd[j][pre[pos]]);
if(vis[j] == && dist[j] > edge[pos][j])
{
dist[j] = edge[pos][j];
pre[j] = pos;
}
}
}
}
void smst()
{
cnt = INF;
for(int i = ; i <= n; i++)
{
for(int j = i + ; j <= n; j++)
{
if(used[i][j] == false && edge[i][j] != INF)
{
cnt = min(cnt, ans - maxd[i][j] + edge[i][j]);
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
for(int i = ; i <= MAX; i++)
for(int j = ; j <= MAX; j++) //初始化的时候把MAX,写成了n, orz....
{
if(i != j)
edge[i][j] = INF;
else
edge[i][i] = ;
}
scanf("%d%d", &n,&m);
for(int i = ; i < m; i++)
{
int x,y,w;
scanf("%d%d%d", &x, &y, &w);
edge[x][y] = edge[y][x] = w;
}
ans = ;
prime();
smst();
if(ans == INF)
{
printf("Not Unique!\n");
continue;
}
if(cnt == ans)
{
printf("Not Unique!\n");
}
else
{
printf("%d\n",ans);
}
}
return ;
}
POJ1679The Unique MST(次小生成树)的更多相关文章
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- 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 ...
- 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(次小生成树模板)
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条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- 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 ...
随机推荐
- The Geometry has no Z values 解决办法
from:http://dufan20086.blog.163.com/blog/static/6616452320145269343675/ 我们在创建要素时,简单的IFeatureClass.Cr ...
- w3school-CSS
1.与XHTML不同,CSS对大小写不敏感.但是,当与HTML一起工作的时候,class和id名称对大小写是敏感的. 2.body {.....};通过css继承关系,子元素将继承最高级元素(本例是b ...
- swift为UIView添加extension扩展frame
添加swift file:UIView+Extension import UIKit extension UIView { // x var x : CGFloat { get { return fr ...
- JayProxy的设置
1. mac http://pac.jayproxy.com/jayproxy/jayproxy.pac 2. wifi http://pac.jayproxy.com/jayproxy/m.pac ...
- python wordcloud
python wordcloud 对电影<我不是潘金莲>制作词云 上个星期五(16/11/18)去看了冯小刚的最新电影<我不是潘金莲>,电影很长,有点黑色幽默.看完之后我就去知 ...
- The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- 交流异步电机的Modelica模型
Modelica标准库里的异步电机模型过于复杂,为了便于学习,我用最基本的异步电机方程写了一个Modelica模型,公式参照陈伯时的<电力拖动自动控制系统--运动控制系统>第3版的190页 ...
- Google Map API key 获取方法
要想使用google map api 必须从google网站上获取key之后才有权限使用,但是要想申请key必须要有证明书,也就是所谓的MD5.下面一步一步来说明: 步骤1: 如果你使用的是eclip ...
- java heep space错误解决办法
1.双击tomcat 2.Open launch configuration 3.Argument 4. VM arguments中添加:-Xmx1024M -Xms512M -XX:MaxPermS ...
- Fiddler工具的基本功能
Fiddler是一款用于网页数据分析,抓取的工具,里面集成了对网页强大的功能外,还可以通过设置,使其对手机的数据也可以进行抓取 Fiddler的原理是: 通过在客户端和服务器之间创建一个代理服务器来对 ...