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(两个城市人口/(生成树长度-这条路的长度 ...
随机推荐
- mysql too many connection 解决办法
SHOW VARIABLES LIKE "max_connections"; SHOW VARIABLES LIKE "wait_timeout"; SET G ...
- 【APUE】fork函数
#include <unisth.h> pid_t fork(void) fork函数被调用一次,返回两次.子进程的返回值是0,父进程的返回值是子进程的进程id. fork函数调用一次却返 ...
- instancetype VS id
英文好的直接读下面链接的文章就好了: http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using- ...
- 关于Python中正则表达式的反斜杠问题
之前总是搞不明白正则表达式中的反斜杠的问题.今天经过查阅资料终于搞明白了. 其中最重要的一点就是Python自己的字符串中定义的反斜杠也是转义字符,而正则表达式中的反斜杠也是转义字符,所以正则表达式中 ...
- Python中暂未解决的问题
编写一个复杂的计算器,可以在通过GUI输出出来.参考代码http://www.cnblogs.com/BeginMan/p/3216093.html shelve模块中open()函数调用文件文件的路 ...
- 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密
读取本地json文件,转出为指定格式json 引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件StreamReader sr = ...
- oracle技术总结
http://www.cnblogs.com/jimeper/ http://blog.csdn.net/dragonxiangfu http://www.boobooke.com/bbs/threa ...
- Thinkpad升级Window10无法安装expresscache
本人有一台Thinkpad T440s,自从看了这篇帖子12秒开机!ExpressCache SSD缓存加速,就给自己的小黑加持了一块固态硬盘.使用后效果确实很明显. 问题 自从系统自动升级到wind ...
- 【bzoj2462】[BeiJing2011]矩阵模板
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> # ...
- mysql04--存储过程
过程:若干语句,调用时执行封装的体.没有返回值的函数. 函数:是一个有返回值的过程 存储过程:把若干条sql封装起来,起个名字(过程),并存储在数据库中. 也有不存储的过程,匿名过程,用完就扔(mys ...