(最小生成树 次小生成树)The Unique MST -- POJ -- 1679
链接:
http://poj.org/problem?id=1679
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/K
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24594 | Accepted: 8751 |
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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0x3f3f3f3f; int J[N][N], dist[N], pre[N], Max[N][N], n, m;
int use[N][N];
bool vis[N]; int prim()
{
int i, j, ans=;
memset(Max, , sizeof(Max));
memset(use, , sizeof(use));
memset(dist, , sizeof(dist));
memset(vis, false, sizeof(vis));
vis[]=; for(i=; i<=n; i++)
{
dist[i]=J[][i];
pre[i]=;
} for(i=; i<n; i++)
{
int index=, MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
MIN=dist[j];
index=j;
}
}
ans += MIN;
vis[index]=;
use[index][pre[index]]=use[pre[index]][index]=; for(j=; j<=n; j++)
{
if(vis[j] && j!=index)
{
Max[index][j]=Max[j][index]=max(Max[j][pre[index]], dist[index]);
}
if(!vis[j] && dist[j]>J[index][j])
{
dist[j]=J[index][j];
pre[j]=index;
}
}
}
return ans;
} int cc(int s)
{
int MIN=INF, i, j;
for(i=; i<=n; i++)
for(j=i+; j<=n; j++)
{
if(!use[i][j] && J[i][j]!=INF)
{
MIN = min(MIN, s-Max[i][j]+J[i][j]);
}
}
return MIN;
} int main ()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, j, a, b, c;
scanf("%d%d", &n, &m); for(i=; i<=n; i++)
{
J[i][i]=;
for(j=; j<i; j++)
J[i][j]=J[j][i]=INF;
} for(i=; i<=m; i++)
{
scanf("%d%d%d", &a, &b, &c);
J[a][b]=J[b][a]=c;
} int ans1=prim();
int ans2=cc(ans1);
if(ans1==ans2)
printf("Not Unique!\n");
else
printf("%d\n", ans1);
}
return ;
}
(最小生成树 次小生成树)The Unique MST -- POJ -- 1679的更多相关文章
- 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数组记录该条边有没有被最小生成树使用过 把没有使用过的一条边加 ...
- 次小生成树 判断 unique MST
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- K - The Unique MST - poj 1679
题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...
- 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 最小生成树判重
题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...
- 训练指南 UVALive - 5713(最小生成树 + 次小生成树)
layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
随机推荐
- conflicting types for ‘方法名’ 的错误
将main()的实现写在drawShapes(),drawCircle(),drawRectangle()...之前. 结果编译的时候出现了 conflicting types for " ...
- 常用类一一MATH类一一两个静态常量PI 和E,一些数学函数。
package test; public class MathTest { public static void main(String[] args) { System.out.println(Ma ...
- IE下设置body{overflow:hidden;}失效Bug
问题重现: <p>There are no scrollbars on this page in sane browsers</p> html, body, p { margi ...
- css样式占位和不占位隐藏元素的方法
不占位隐藏:display:none; 占位隐藏:visibility:hidden;
- MySql初步II
[MySql初步II] 1.Order By 你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列. 默认情况下,它是按升排列. 实例: 2.Join语法 Join不是一个关键字 ...
- np.array()
将列表list或元组tuple转换为 ndarray 数组. numpy.array(object, dtype=None, copy=True, order=None, subok=False, n ...
- 运行php网站需要安装什么
php的运行环境: 为了能够运行php,有以下两种方法: 1. 使用支持php和MySQL的web主机(): 2. 本机(自己电脑)安装web服务器,然后安装MySQL和php. web虚拟主机: 大 ...
- Django的models介绍
我们一般会在创建表的类中写一个__str__方法,就会为为了打印这个对象不会打印一大堆的对象的内存地址,而是我们想要他返回的信息,方便我们更直观的知道这个对象是谁,方便显示.比如下面的例子 from ...
- Weblogic 12c 一个domain建多个server(端口)
一.基本概念 我觉得如果刚接触Weblogic,首先应该做的是明白几个基本的概念,可以从一张图入手: 1. 域(domain) 它是一个基本管理单元: 每个域包含一个管理服务器(Administrat ...
- [udemy]WebDevelopment_HTML5
Build Your First Website 装一个subline text HTML default rule tags with opening and closing <!DOCTY ...