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(两个城市人口/(生成树长度-这条路的长度 ...
随机推荐
- [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
Understanding sources and subscribers makes it much easier to understand what's going on with mergeM ...
- Selenium系列之--03【转】页面元素找不到问题的分析思路
如果在测试过程中遇到了NoSuchElementException 这个异常, 说明元素查找失败. Caused by: org.openqa.selenium.NoSuchElementExcept ...
- hive计算网页停留时长
hive表结构例如以下: create table pv_user_info( session_id string, user_id string, url string, starttime big ...
- 高清接口芯片---gv7600、sii9135
http://www.travellinux.com/download/海思Hi3516%20demo单板使用指南.pdf gv7600 sdi 串行数字接口 parrlar 并行数字接口 http: ...
- 【iOS系列】- UITableView的使用技巧
[iOS系列]- UITableView的使用 UITableView的常用属性 indexpath.row:行 indexpath.section:组 separatorColor:分割线的颜色 s ...
- C++ 虚函数与纯虚函数 浅析
[摘要] 在虚函数与纯虚函数的学习中.要求理解虚函数与纯虚函数的定义,了解虚函数与纯虚函数在实例化上的差异.掌握两者在实现上的必要性.熟悉纯虚函数在子类与孙类的函数类型.本文即针对上述问题展开阐述. ...
- Oracle imp exp命令具体解释
怎样在oracle中导入dmp数据库文件? oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令能够把数据从远程数据库server导出到本地的dmp文件,imp命令能够 ...
- SQLServer导出导出单表数据
1.SQLServer导出单表数据为TXT文件 数据库--任务--导出数据
- XMU 1606 nc与滴水问题 【模拟】
1606: nc与滴水问题 Time Limit: 1000 MS Memory Limit: 64 MBSubmit: 85 Solved: 27[Submit][Status][Web Boa ...
- YTU 2553: 谁是赢家
2553: 谁是赢家 时间限制: 1 Sec 内存限制: 128 MB 提交: 94 解决: 25 题目描述 某一天,hcbbt等一群弱菜去tamara巨巨家里一起玩了一个卡牌游戏.巨 巨家里有2 ...