K - The Unique MST - poj 1679
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ;
const int oo = 0x7fffffff; int G[maxn][maxn], path[maxn][maxn];//path记录两点之间的最大边值
bool use[maxn][maxn];//记录是否是最小生成树上面的边 int Prim(int N)
{
int dist[maxn], vis[maxn]={,}, pre[maxn];
int i, ans = , T = N-; for(i=; i<=N; i++)
{
pre[i] = ;
dist[i] = G[][i];
}
while(T--)
{
int k = , mini = oo; for(i=; i<=N; i++)
{
if(!vis[i] && mini > dist[i])
mini = dist[i], k = i;
}
use[ pre[k] ][k] = use[k][ pre[k] ] = true;
ans += mini; vis[k] = true; for(i=; i<=N; i++)
{
if(vis[i] && k != i)
path[k][i]=path[i][k] = max(path[pre[i]][i], mini);
if(!vis[i] && dist[i] > G[k][i])
dist[i] = G[k][i], pre[i] = k;
}
} return ans;
}
int OK(int N)//判断是否有次小生成树
{
for(int i=; i<=N; i++)
for(int j=i+; j<=N; j++)
{
//如果有边的长度与最小树上的边相等,就说明不唯一了
if(!use[i][j] && path[i][j]==G[i][j])
return ;
} return ;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, j, N, M, u, v, w; scanf("%d%d", &N, &M); for(i=; i<=N; i++)
for(j=; j<=N; j++)
{
G[i][j] = (i == j ? : oo);
path[i][j] = ;
use[i][j] = false;
} while(M--)
{
scanf("%d%d%d", &u, &v, &w);
G[u][v] = G[v][u] = w;
} int ans = Prim(N); if(OK(N) == )
printf("Not Unique!\n");
else
printf("%d\n", ans);
} }
K - The Unique MST - poj 1679的更多相关文章
- (最小生成树 次小生成树)The Unique MST -- POJ -- 1679
链接: http://poj.org/problem?id=1679 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- Day5 - G - The Unique MST POJ - 1679
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- The Unique MST POJ - 1679 (次小生成树)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- The Unique MST POJ - 1679 次小生成树prim
求次小生成树思路: 先把最小生成树求出来 用一个Max[i][j] 数组把 i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过 把没有使用过的一条边加 ...
- The Unique MST POJ - 1679 最小生成树判重
题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...
- K - The Unique MST
K - The Unique MST #include<iostream> #include<cstdio> #include<cstring> #include& ...
- K度限制MST poj 1639
/* k度限制MST:有一个点的度<=k的MST poj 1639 要求1号点的度不超过k 求MST 我们先把1号点扔掉 跑MST 假设有sum个连通分支 然后把这sum个分支连到1上 就得到了 ...
- K - The Unique MST (最小生成树的唯一性)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
随机推荐
- php-fpm 启动参数及重要配置详解<转>
原文地址 http://levi.cg.am/archives/3127 约定几个目录 /usr/local/php/sbin/php-fpm /usr/local/php/etc/php-fpm. ...
- 怎么在后台修改前台html页面的key、title、description
public void UpdateMeta(string title, string keyword, string desc) { ; i >= ; i--) { if (this.Head ...
- js异步的理解---千呼万唤始出来啊!
编译完成后(先分配给变量空间和function(){}命名的函数,var = function(){}这种函数也仅仅只是分配了个空间,还没有赋值个函数给他!),调用了若不是undefined就执行, ...
- 触发TreeView的TreeNodeCheckChanged事件
这个事件不会主动postback,需要手动写javascript触发.对网上找到的方法做了些改进,增加UpdatePanel,以免页面不停的刷.这里就不考虑性能神马的了,因为既然项目已经允许选择使用T ...
- 合理计划 dictionary cache 大小
[数据字典缓冲区(Data Dictionary Cache) ] 用于存放Oracle系统管理自身所需要的所有信息,包括登录的用户名.用户对象.权限等. 查看 data dictionary ca ...
- Objective-C基础知识点总结
一.#import 和 #include 的区别,@class代表什么?@class 和 #import 的区别?#import<> 和 #import""的区别 答: ...
- 开始学习<p>标签,添加段落
如果想在网页上显示文章,这时就需要<p>标签了,把文章的段落放到<p>标签中. 语法: <p>段落文本</p> 注意一段文字一个<p>标签, ...
- JVM调优实践-Tomcat调优
调优几个重要指标 GC频率 提升每次GC的效率 准备环节 jmeter的配置 未压测前JVM配置 工程未调优前配置 -Xms400m -Xmx400m -XX:PermSize=64m -XX:Max ...
- 用Guava辅助Throwable异常处理
Guava的 Throwables 工具常常可以让exception处理更方便. Propagation 有时候,你会想把捕获的exception抛到上一个try/catch块.对于 RuntimeE ...
- thinkphp 内置函数详解
D() 加载Model类M() 加载Model类 A() 加载Action类L() 获取语言定义C() 获取配置值 用法就是 C("这里填写在配置文件里数组的下标")S( ...