UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)
题目大意:
Nasa应邻居们的要求,决定用一个网络把大家链接在一起。给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w。
1:如果不存在最小生成树,输出“No way”.
2:如果不存在次小生成树,输出“No second way”.
3:如果两者都存在,输出次小生成树的长度.
解题思路:
次小生成数+kruskal模板
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f;
const double Exp = 1e-;
struct node
{
int x, y, w; };
bool cmp (node a, node b)
{
return a.w < b.w;
}
node stu[maxn];
int v, e, father[maxn], path[maxn]; void init ()
{
for (int i=; i<=v; i++)
father[i] = i;
} int find (int x)
{
if (father[x] != x)
father[x] = find(father[x]);
return father[x];
}
int kruskal ()
{
int i, j, num = ;
for (i=,j=; i<e; i++)
{
int px = find (stu[i].x);
int py = find (stu[i].y);
if (px != py)
{
num += stu[i].w;
path[j++] = i;
father[px] = py;
}
}
int ans = ;
for (i=; i<=v; i++)
if (father[i] == i)
ans ++;
if (ans == )
return num;
return INF;
}
int smst (int x)
{
int i, num = ;
for (i=; i<e; i++)
{
int px = find(stu[i].x);
int py = find(stu[i].y);
if (px != py && i != x)
{
num += stu[i].w;
father[px] = py;
}
}
int ans = ;
for (i=; i<=v; i++)
if (father[i] == i)
ans ++;
if (ans == )
return num;
return INF;
}
int main ()
{
int t, l = ;
scanf ("%d", &t);
while (t --)
{
scanf ("%d %d", &v, &e);
for (int i=; i<e; i++)
scanf ("%d %d %d", &stu[i].x, &stu[i].y, &stu[i].w);
sort (stu, stu+e, cmp);
int num1 , num2;
num1 = num2 = INF;
init ();
num1 = kruskal();
for (int i=; i<v; i++)
{
init ();
num2 = min (num2, smst(path[i]));
}
if (num1 == INF)
printf ("Case #%d : No way\n", l++);
else if (num2 == INF)
printf ("Case #%d : No second way\n", l++);
else
printf ("Case #%d : %d\n", l++, num2);
}
return ;
}
UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)的更多相关文章
- UVA 10462 Is There A Second Way Left? 次小生成树
模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...
- UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】
Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently a ...
- UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树
UVA - 10462 题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好. #include <iostream> #include <cstdio ...
- UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解
思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...
- UVA - 10462 Is There A Second Way Left?
题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...
- UVA 10600 ACM Contest and Blackout 次小生成树
又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
[题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- Qin Shi Huang's National Road System UVA - 1494(次小生成树)
秦始皇统一中国之后要在全国修公路连接各个城市,皇帝只想修成最小生成树(距离最小,不考虑人力),一个道士说自己可以不花人力物力修一条路,经过两方妥协,选择max(两个城市人口/(生成树长度-这条路的长度 ...
随机推荐
- Java处理XSS漏洞的工具类代码
原文:http://www.open-open.com/code/view/1455809388308 public class AntiXSS { /** * 滤除content中的危险 HTML ...
- HDU 5280 Senior's Array
Senior's Array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- AE的Annotation学习摘记
http://xg-357.blog.163.com/blog/static/36263124201151763512894/ IFeatureWorkspaceAnno pFWSAnno = (IF ...
- Python - 多次检查后缀名(endwith)
在通过后缀名查找类型文件的时候, 多次使用endwith, 使用元组(tuple), 简化操作. 此类方式, 也能够应用于if语句多次类似检測. 代码 # 列出目录内全部代码 def list_dic ...
- 【独立开发人员er Cocos2d-x实战 008】BMFont生成位图字体工具和Cocos2dx使用载入fnt文件
1.首先我们须要下载而且安装BMFont工具,下载地址例如以下:http://download.csdn.net/detail/chenqiai0/8899353(里面还有具体的使用文档,假设使用中有 ...
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 云上领跑,快人一步:华为云抢先发布Redis5.0
12月17日,华为云在DCS2.0的基础上,快人一步,抢先推出了新的Redis 5.0产品,这是一个崭新的突破.目前国内在缓存领域的发展普遍停留在Redis4.0阶段,华为云率先发布了Redis5.0 ...
- 【JAVA】java中Future、FutureTask的使用
如今的系统基本都是分布式的,各个系统各司其职的,不可能一个系统干了全部系统的事. 所以系统之间的交互就越来越多了.那么系统之间的交互仅仅有通过网络来交互了,而网络必定会存在延时的情况. 比方A系统的一 ...
- Python 离线等价类
离线等价类的概念见离线等价类 最近在清洗数据的时候涉及到要将相似度比较高的文件夹合并,特征比对得到是1:1的对,比如: (a,b),(c,d),(a,c)...,那么合并的时候就涉及到将这些等价的对合 ...
- JavaScript基本类型与引用类型
前面已经说过,JavaScript变量是松散类型,它可以保存任何类型的值.变量的值以及数据类型可以在脚本的生命周期内发生改变.变量包含两种不同类型的值:基本类型和引用类型.基本类型值的是简单的数据段, ...